Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

Excel cell comments via ole object - get comments in excel sheet

0 Kudos

Hi, is it possible to get a comment text of a cell using abap ole object?

I already read this:

https://archive.sap.com/discussions/thread/3777521

but it's addcomment. I want read or get comment.

I tried with

DATA: obj_erange TYPE ole2_object,
obj_ecomment TYPE ole2_object.

* CALL METHOD OF application 'Range' = obj_erange
* EXPORTING
* #1 = 'A1'
* #2 = 'A1'.

CALL METHOD OF worksheet 'Cells' = h_cell
EXPORTING #1 = 1 #2 = 1.
m_message.
CALL METHOD OF worksheet 'Cells' = h_cell1
EXPORTING #1 = 1 #2 = 1.
m_message.

CALL METHOD OF worksheet 'RANGE' = obj_erange
EXPORTING #1 = h_cell #2 = h_cell1.
m_message.

CALL METHOD OF obj_erange 'Select'.

CALL METHOD OF obj_erange 'Comment' = obj_ecomment.

CALL METHOD OF obj_ecomment 'COPY'.

CALL METHOD cl_gui_frontend_services=>clipboard_import
IMPORTING
data = excel_tab
EXCEPTIONS
cntl_error = 1
error_no_gui = 2
not_supported_by_gui = 3
OTHERS = 4.

But, it was not work.

Please, let me know.

I searched at

https://docs.microsoft.com/

but, I couldn't find solution.

CALL METHOD OF obj_erange 'Comment' = obj_ecomment.

This is failed, too.

How can I get comments in worksheet ???

Somebody help me, please !!!!!! OTL

1 ACCEPTED SOLUTION

Sandra_Rossi
Active Contributor

You should explain what means "it doesn't work" (which line of code returns the error code...)

By using the Excel macros (alt+F11), this VBA code works, just translate it to ABAP:

Set cell = Cells(1, 2)
Text = cell.Comment.Text

So, I guess you have 2 main errors :

1) the worksheet object doesn't have any RANGE method. Anyway you don't need it because CELLS is a range object !

2) you forget the TEXT property : don't copy the COMMENT object, do :

DATA comment_text TYPE c LENGTH 255.
GET PROPERTY OF obj_ecomment 'text' = comment_text.

PS: you don't need the clipboard too.

8 REPLIES 8

Sandra_Rossi
Active Contributor

You should explain what means "it doesn't work" (which line of code returns the error code...)

By using the Excel macros (alt+F11), this VBA code works, just translate it to ABAP:

Set cell = Cells(1, 2)
Text = cell.Comment.Text

So, I guess you have 2 main errors :

1) the worksheet object doesn't have any RANGE method. Anyway you don't need it because CELLS is a range object !

2) you forget the TEXT property : don't copy the COMMENT object, do :

DATA comment_text TYPE c LENGTH 255.
GET PROPERTY OF obj_ecomment 'text' = comment_text.

PS: you don't need the clipboard too.

0 Kudos

Thank you so much about your answer, Sandra.

Okay, I changed my source code by your advise.

DATA: application   TYPE  ole2_object,
      workbook      TYPE  ole2_object,
      worksheet     TYPE  ole2_object,
      obj_ecomment TYPE ole2_object,
      lv_comment_text(255),
      h_cell        TYPE  ole2_object.

CREATE OBJECT application 'Excel.Application'.

CALL METHOD OF application 'Workbooks' = workbook.

CALL METHOD OF workbook 'Open' EXPORTING #1 = filename.

GET PROPERTY OF  application 'ACTIVESHEET' = worksheet.

CALL METHOD OF worksheet 'Cells' = h_cell
  EXPORTING #1 = 1 #2 = 1.

CALL METHOD OF h_cell 'Comment' = obj_ecomment.

GET PROPERTY OF obj_ecomment 'Text' = lv_comment_text.

FREE OBJECT: application, workbook, worksheet, h_cell, obj_ecomment.

CELLS succeed.

But, TEXT property returns null. So lv_commen_text is initial.

I don't know why it can not get a comment.

Do you have any idea???

0 Kudos

The following code works for me. Make sure all Excel sessions are closed via the Task Manager :

parameters filename type string LOWER CASE DEFAULT 'C:\Users\myself\Downloads\Book1.xlsx'.

DATA: application   TYPE  ole2_object,
      workbooks     TYPE  ole2_object,
      workbook      TYPE  ole2_object,
      worksheet     TYPE  ole2_object,
      obj_ecomment TYPE ole2_object,
      lv_comment_text(255),
      h_cell        TYPE  ole2_object.

CREATE OBJECT application 'Excel.Application'.

CALL METHOD OF application 'WorkbookS' = workbooks.

CALL METHOD OF workbooks 'Open' = workbook EXPORTING #1 = filename.

CALL METHOD OF workbook 'ACTIVESHEET' = worksheet.

CALL METHOD OF worksheet 'Cells' = h_cell
  EXPORTING #1 = 1 #2 = 1.

GET PROPERTY OF h_cell 'Comment' = obj_ecomment.

GET PROPERTY OF obj_ecomment 'Text' = lv_comment_text.

SET PROPERTY OF application 'DisplayAlerts' = 0.

CALL METHOD OF WORKBOOK 'close'.

CALL METHOD OF application 'quit'.

FREE OBJECT: application, workbook, worksheet, h_cell, obj_ecomment.

WRITE lv_comment_text.

Thank you!! My problem is solved.

After FREE OBJECT, lv_comment_text get a comment.

I thought lv_comment_text will get a comment after GET PROPERTY TEXT.

I was so stupid.

I'm really appreciate to you, sincerely.

Have a nice day, Sandra !!!! Thank you !!!

0 Kudos

It's weird because it should "flush" immediately at the end of each OLE statement (provided it doesn't mention "NO FLUSH"), but it seems that the flush is done at the beginning of each OLE statement !!?? I had not seen that in previous SAP GUI or ABAP releases (I currently use ABAP 7.52 + SAP GUI 7.50). The workaround is to do an explicit CALL FUNCTION 'FLUSH' to force the flush.

0 Kudos

Yes, you're right. I currently use ABAP 7.40 + SAP GUI 7.30

It's really old version. So sometimes weird.

My customer doesn't like update to SAP GUI 7.50

0 Kudos

And which ABAP release?