Skip to Content
Former Member
Jul 26, 2005 at 08:01 AM




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.


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.