cancel
Showing results for 
Search instead for 
Did you mean: 

OLE2 WORD

Former Member
0 Kudos

Hello,

I have record the macro below in word. Now i want to execute this statements in my ABAP program. Can somebody help me with the correct statement? I tried several combination but none are working. This looks like a very specific thing.

Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:= _

"MERGEFIELD mmf1", PreserveFormatting:=True

The macro above inserts a mailmerge field into the document with the name mmf1.

In my ABAP program i want to generate a word document with the mail merge fields added to this document. This mail merge fields can be different each time the program runs, so i want to add them in a flexible way to the word document.

This is how i came so far:

REPORT zpval_test004 .

INCLUDE ole2incl.

DATA: ls_word TYPE ole2_object,

ls_documents TYPE ole2_object,

ls_activedoc TYPE ole2_object,

ls_application TYPE ole2_object,

ls_selection TYPE ole2_object,

ls_range TYPE ole2_object,

ls_fields TYPE ole2_object.

DATA: selection TYPE ole2_object.

DATA: fields1 TYPE ole2_object.

CREATE OBJECT ls_word 'WORD.APPLICATION'.

SET PROPERTY OF ls_word 'Visible' = 1. "0 = hidden, 1 = visible

GET PROPERTY OF ls_word 'Documents' = ls_documents.

CALL METHOD OF ls_documents 'Add'.

*-Get handle for active document

GET PROPERTY OF ls_word 'ActiveDocument' = ls_activedoc.

*-Getting application handle

GET PROPERTY OF ls_activedoc 'Application' = ls_application.

*-Cursor position

GET PROPERTY OF ls_application 'Selection' = ls_selection.

*-Get range

GET PROPERTY OF ls_activedoc 'Range' = ls_range.

*-Get fields

GET PROPERTY OF ls_activedoc 'Fields' = ls_fields.

CALL METHOD OF ls_fields 'Add' EXPORTING #1 = ls_range

  • #2 = 'wdFieldEmpty'

#3 = 'MERGEFIELD mmf1 '.

  • #4 = 'True'.

the result of the program above is that word is started and a new document is created but w/o the mailmerge field.

Any help would be appreciated.

Thanks,

Peter

Accepted Solutions (1)

Accepted Solutions (1)

jayanthi_jayaraman
Active Contributor
0 Kudos

Hi,

Check this.This is discussing about Mail merge.

http://homepage.swissonline.ch/cindymeister/mergfaq1.htm

http://www.kbalertz.com/kb_Q181926.aspx

Message was edited by: Jayanthi Jayaraman

Answers (1)

Answers (1)

Former Member
0 Kudos

Hello,

There is a program provided by sap SAPRDEMOWORD97INTEGRATION

check for this code for calling macro

  • Document shall also be available in ITAB for respective operations:

CALL METHOD document->save_document_to_table

IMPORTING retcode = retcode

CHANGING document_table = data_table

document_size = data_size.

CALL METHOD c_oi_errors=>show_message EXPORTING type = 'E'.

first_open = false.

open_document = true.

CALL METHOD document->execute_macro

EXPORTING macro_string = 'R3UpdateTables' "#EC NOTEXT

IMPORTING retcode = retcode.

ELSE.

MESSAGE e010.

ENDIF.

ENDIF.

it has defined a macro called swap color and it works.

Regards,

Shekhar Kulkarni

Former Member
0 Kudos

thanks for the quick replies. But i think i did not mention very clear where the (my) problem is.

The statement in the record macro is a method (Add) from an object (Fields) from an object (Selection).

When i want to call a method from an object i do

call method add of fields.

but how do i call a method of an object which is an attribute of another object?

So i do not want to call a macro in word, but i want to execute the statement directly from my ABAP program.

Former Member
0 Kudos

does someone have a clue?

I tried it further but now i get a sy-subrc 2 when executing the method? Does someone know how to find out what was wrong with the statement?

thx

Former Member
0 Kudos

I have found the solution, i just want it to share.

source below will add a mailmerge field for each entry in the itab

DATA: ls_word TYPE ole2_object,

ls_documents TYPE ole2_object,

ls_selection TYPE ole2_object,

ls_range TYPE ole2_object,

ls_mailmerge TYPE ole2_object,

ls_fields TYPE ole2_object,

ls_activedoc TYPE ole2_object.

CREATE OBJECT ls_word 'WORD.APPLICATION'.

GET PROPERTY OF ls_word 'Documents' = ls_documents.

SET PROPERTY OF ls_word 'Visible' = 1. "0 = hidden, 1 = visible

*-Add new document

CALL METHOD OF ls_documents 'Add'.

*-Get handle for active document

GET PROPERTY OF ls_word 'ActiveDocument' = ls_activedoc.

GET PROPERTY OF ls_word 'Selection' = ls_selection.

CALL METHOD OF ls_activedoc 'Range' = ls_range.

GET PROPERTY OF ls_activedoc 'MailMerge' = ls_mailmerge.

GET PROPERTY OF ls_mailmerge 'Fields' = ls_fields.

LOOP AT lt_<ITAB> INTO ls_<ITAB_LINE>.

CALL METHOD OF ls_selection 'EndKey'.

CALL METHOD OF ls_selection 'TypeParagraph'.

GET PROPERTY OF ls_selection 'range' = ls_range.

CALL METHOD OF ls_fields 'Add'

EXPORTING

#1 = ls_range

#2 = ls_<ITAB_LINE>-field.

ENDLOOP.