Application Development and Automation 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: 
Read only

Problem with OLE and Excel

Former Member
0 Likes
966

Hello.

I'm having a problem using OLE automation with Excel.

I'm creating my Excel sheet, if I open another Excel file, my program starts to write in the recently opened file.

Theres a way to control that??.

Thanks in advance.

Hello.

I'm having a problem using OLE automation with Excel.

I'm creating my Excel sheet, if I open another Excel file, my program starts to write in the recently opened file.

Theres a way to control that??.

Thanks in advance.

6 REPLIES 6
Read only

RichHeilman
Developer Advocate
Developer Advocate
0 Likes
887

Are you saying that you have an excel sheet open, then you run your program which creates a new excel document, but it is writing in the previously opened excel document?

Please clarify.

By the way, this senario is not happending for me.

Regards,

RIch Heilman

Read only

RichHeilman
Developer Advocate
Developer Advocate
0 Likes
887

Are you saying that you have an excel sheet open, then you run your program which creates a new excel document, but it is writing in the previously opened excel document?

Please clarify.

By the way, this senario is not happending for me.

Regards,

RIch Heilman

Read only

0 Likes
887

No.

I'm saying that I run the program (with Excel closed), but during the execution I open Excel and the program writes in the sheet I open.

With Excel open previously I'm not having problems.

Greetings.

Joaquin.

Read only

Former Member
0 Likes
887

Hi Silva,

While the program is writing to the excel sheet, if you open another excel sheet, that <b>new one will become the current active worksheet</b> and program continues to write onto it.

Regards,

Raj

Read only

0 Likes
887

And there´s no way to link my program with the specific excel sheet I want to write??.

Greetings.

Joaquin.

Read only

0 Likes
887

Hi,

Check this below code.

  • Excel Attributes

DATA: V_EXCEL TYPE OLE2_OBJECT,

V_APP TYPE OLE2_OBJECT,

V_BOOKS TYPE OLE2_OBJECT,

V_BOOK TYPE OLE2_OBJECT,

V_SHEET TYPE OLE2_OBJECT,

V_CELL TYPE OLE2_OBJECT,

V_COLUMN TYPE OLE2_OBJECT,

V_ROW TYPE I,

V_NO TYPE I,

V_NAME(20).

  • Open the Excel Application in visible mode

CREATE OBJECT V_EXCEL 'EXCEL.SHEET'.

GET PROPERTY OF V_EXCEL 'Application' = V_APP.

SET PROPERTY OF V_APP 'Visible' = 1.

CALL METHOD OF V_APP 'Workbooks' = V_BOOKS.

CALL METHOD OF V_BOOKS 'Add' = V_BOOK.

V_NO = V_NO + 1.

V_NAME = 'DOMESTIC'.

PERFORM FILL_WORKSHEET USING V_NO V_NAME.

----


FORM FILL_WORKSHEET USING P_NO P_NAME.

CALL METHOD OF V_BOOK 'worksheets' = V_SHEET NO FLUSH

EXPORTING #1 = P_NO.

SET PROPERTY OF V_SHEET 'Name' = P_NAME NO FLUSH.

PERFORM FILL_HEADER.

IF P_NAME = 'DOMESTIC'.

LOOP AT T_DOMESTIC.

V_ROW = V_ROW + 1.

PERFORM FILL_CELLS USING V_ROW 1 T_DOMESTIC-TR_NAME.

PERFORM FILL_CELLS USING V_ROW 2 T_DOMESTIC-KUNNR.

PERFORM FILL_CELLS USING V_ROW 3 T_DOMESTIC-FD.

PERFORM FILL_CELLS USING V_ROW 4 T_DOMESTIC-MATNR.

PERFORM FILL_CELLS USING V_ROW 5 T_DOMESTIC-VTEXT.

PERFORM FILL_CELLS USING V_ROW 6 T_DOMESTIC-MSTDV.

PERFORM FILL_CELLS USING V_ROW 7 T_DOMESTIC-QTR1.

PERFORM FILL_CELLS USING V_ROW 8 T_DOMESTIC-QTR2.

PERFORM FILL_CELLS USING V_ROW 9 T_DOMESTIC-QTR3.

PERFORM FILL_CELLS USING V_ROW 10 T_DOMESTIC-QTR4.

PERFORM FILL_CELLS USING V_ROW 11 T_DOMESTIC-OUT.

PERFORM FILL_CELLS USING V_ROW 12 T_DOMESTIC-YEAR.

ENDLOOP.

ENDIF.

CALL METHOD OF V_SHEET 'Columns' = V_COLUMN NO FLUSH.

FREE OBJECT V_SHEET NO FLUSH.

CALL METHOD OF V_COLUMN 'Autofit' NO FLUSH.

FREE OBJECT V_COLUMN NO FLUSH.

CALL FUNCTION 'FLUSH'.

----


FORM FILL_CELLS USING P_ROW

P_COL

P_VAL.

CALL METHOD OF V_SHEET 'cells' = V_CELL NO FLUSH

EXPORTING #1 = P_ROW #2 = P_COL.

SET PROPERTY OF V_CELL 'value' = P_VAL.

FREE OBJECT V_CELL NO FLUSH.

ENDFORM. " FILL_CELLS

----


FORM FILL_HEADER.

CLEAR V_ROW.

V_ROW = V_ROW + 1.

PERFORM FILL_CELLS USING V_ROW 1 'TR AGENT NAME'.

PERFORM FILL_CELLS USING V_ROW 2 'COMPANY'.

PERFORM FILL_CELLS USING V_ROW 3 'F/D'.

PERFORM FILL_CELLS USING V_ROW 4 'ISSUE'.

PERFORM FILL_CELLS USING V_ROW 5 'ISSUE TYPE'.

PERFORM FILL_CELLS USING V_ROW 6 'LISTING DATE'.

PERFORM FILL_CELLS USING V_ROW 7 'Q1 SHARES'.

PERFORM FILL_CELLS USING V_ROW 8 'Q2 SHARES'.

PERFORM FILL_CELLS USING V_ROW 9 'Q3 SHARES'.

PERFORM FILL_CELLS USING V_ROW 10 'Q4 SHARES'.

PERFORM FILL_CELLS USING V_ROW 11 'OUTSTANDING'.

PERFORM FILL_CELLS USING V_ROW 12 'YEAR'.

ENDFORM. " FILL_HEADER

Regards,

Vara