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: 

Call Microsoft Word from ABAP

Former Member
0 Kudos

Hi,

How can we Call Microsoft Word from ABAP

Thanks

1 ACCEPTED SOLUTION

Former Member
0 Kudos

Hi,

check this program, it may help you.

JUst execute it and see.

&----


*& Report YTEST_ABINASH *

*& *

&----


*& *

*& *

&----


REPORT YTEST_ABINASH .

*--Include for OLE-enabling definitions

INCLUDE OLE2INCL .

*--Global variables

*--Variables to hold OLE object and entity handles

DATA GS_WORD TYPE OLE2_OBJECT . "OLE object handle

DATA GS_DOCUMENTS TYPE OLE2_OBJECT . "Documents

DATA GS_ACTDOC TYPE OLE2_OBJECT . "Active document

DATA GS_APPLICATION TYPE OLE2_OBJECT . "Application

DATA GS_OPTIONS TYPE OLE2_OBJECT . "Application options

DATA GS_ACTWIN TYPE OLE2_OBJECT . "Active window

DATA GS_ACTPAN TYPE OLE2_OBJECT . "Active pane

DATA GS_VIEW TYPE OLE2_OBJECT . "View

DATA GS_SELECTION TYPE OLE2_OBJECT . "Selection

DATA GS_FONT TYPE OLE2_OBJECT . "Font

DATA GS_PARFORMAT TYPE OLE2_OBJECT . "Paragraph format

DATA GS_TABLES TYPE OLE2_OBJECT . "Tables

DATA GS_RANGE TYPE OLE2_OBJECT . "Range handle for various ranges

DATA GS_TABLE TYPE OLE2_OBJECT . "One table

DATA GS_TABLE_BORDER TYPE OLE2_OBJECT . "Table border

DATA GS_CELL TYPE OLE2_OBJECT . "One cell of a table

DATA GS_PARAGRAPH TYPE OLE2_OBJECT . "Paragraph

DATA GV_POS(5) TYPE N . "Position information for table

CREATE OBJECT GS_WORD 'WORD.APPLICATION' .

IF SY-SUBRC NE 0.

MESSAGE S000(SU) WITH 'Error while creating OLE object!'.

LEAVE PROGRAM .

ENDIF .

*--Setting object's visibility property

SET PROPERTY OF GS_WORD 'Visible' = '1' .

*--Opening a new document

GET PROPERTY OF GS_WORD 'Documents' = GS_DOCUMENTS .

CALL METHOD OF GS_DOCUMENTS 'Add' .

*--Getting active document handle

GET PROPERTY OF GS_WORD 'ActiveDocument' = GS_ACTDOC .

*--Getting applications handle

GET PROPERTY OF GS_ACTDOC 'Application' = GS_APPLICATION .

*--Setting the measurement unit

GET PROPERTY OF GS_APPLICATION 'Options' = GS_OPTIONS .

SET PROPERTY OF GS_OPTIONS 'MeasurementUnit' = '1' . "CM

*--Getting handle for the selection which is here the character at the

*--cursor position

GET PROPERTY OF GS_APPLICATION 'Selection' = GS_SELECTION .

GET PROPERTY OF GS_SELECTION 'Font' = GS_FONT .

GET PROPERTY OF GS_SELECTION 'ParagraphFormat' = GS_PARFORMAT .

*--Setting font attributes

SET PROPERTY OF GS_FONT 'Name' = 'Arial' .

SET PROPERTY OF GS_FONT 'Size' = '10' .

SET PROPERTY OF GS_FONT 'Bold' = '0' . "Not bold

SET PROPERTY OF GS_FONT 'Italic' = '1' . "Italic

SET PROPERTY OF GS_FONT 'Underline' = '0' . "Not underlined

*--Setting paragraph format attribute

SET PROPERTY OF GS_PARFORMAT 'Alignment' = '2' . "Right-justified

CALL METHOD OF GS_SELECTION 'TypeText'

EXPORTING

#1 = 'This is an OLE example!'.

*--Setting the view to the main document again

SET PROPERTY OF GS_VIEW 'SeekView' = '0' . "Main document view

*--Reseting font attributes for the title

SET PROPERTY OF GS_FONT 'Name' = 'Times New Roman' .

SET PROPERTY OF GS_FONT 'Size' = '16' .

SET PROPERTY OF GS_FONT 'Bold' = '1' . "Bold

SET PROPERTY OF GS_FONT 'Italic' = '0' . "Not Italic

SET PROPERTY OF GS_FONT 'Underline' = '0' . "Not underlined

*--Setting paragraph format attribute

SET PROPERTY OF GS_PARFORMAT 'Alignment' = '1' . "Centered

CALL METHOD OF GS_SELECTION 'TypeText'

EXPORTING

#1 = TEXT-000.

*--Advancing cursor to the new line

CALL METHOD OF GS_SELECTION 'TypeParagraph' .

*--Getting entity handles for the entities on the way

GET PROPERTY OF GS_ACTDOC 'Tables' = GS_TABLES .

GET PROPERTY OF GS_SELECTION 'Range' = GS_RANGE .

*--Adding a table with 3 rows and 2 columns

CALL METHOD OF GS_TABLES 'Add' = GS_TABLE

EXPORTING

#1 = GS_RANGE " Handle for range entity

#2 = '3' "Number of rows

#3 = '2'. "Number of columns

*--Setting border attribute for the table

GET PROPERTY OF GS_TABLE 'Borders' = GS_TABLE_BORDER .

SET PROPERTY OF GS_TABLE_BORDER 'Enable' = '1' . "With border

*--Filling the table with dummy data

*--Reseting font attributes for table content

SET PROPERTY OF GS_FONT 'Name' = 'Garamond' .

SET PROPERTY OF GS_FONT 'Size' = '11' .

SET PROPERTY OF GS_FONT 'Bold' = '0' . "Not bold

SET PROPERTY OF GS_FONT 'Italic' = '0' . "Not Italic

SET PROPERTY OF GS_FONT 'Underline' = '0' . "Not underlined

*--Getting cell coordinates

CALL METHOD OF GS_TABLE 'Cell'

EXPORTING

#1 = '1' "first row

#2 = '1'. "first column

*--Getting the range handle to write the text

GET PROPERTY OF GS_CELL 'Range' = GS_RANGE .

*--Filling the cell

SET PROPERTY OF GS_RANGE 'Text' = 'abinash' .

*--Getting cell coordinates

CALL METHOD OF GS_TABLE 'Cell' = GS_CELL

EXPORTING

#1 = '3' "third row

#2 = '2'. "second column

*--Getting the range handle to write the text

GET PROPERTY OF GS_CELL 'Range' = GS_RANGE .

*--Filling the cell

SET PROPERTY OF GS_RANGE 'Text' = 'OLE' .

*--Advancing the cursor to the end of the table

GET PROPERTY OF GS_TABLE 'Range' = GS_RANGE .

GET PROPERTY OF GS_RANGE 'End' = GV_POS .

SET PROPERTY OF GS_RANGE 'Start' = GV_POS .

CALL METHOD OF GS_RANGE 'Select' .

*--Skip some lines

DO 3 TIMES .

CALL METHOD OF GS_SELECTION 'TypeParagraph' .

ENDDO.

*--Reseting font attributes for ordinary text

SET PROPERTY OF GS_FONT 'Name' = 'Times New Roman' .

SET PROPERTY OF GS_FONT 'Size' = '12' .

SET PROPERTY OF GS_FONT 'Bold' = '0' . "Not bold

SET PROPERTY OF GS_FONT 'Italic' = '0' . "Not Italic

SET PROPERTY OF GS_FONT 'Underline' = '0' . "Not underlined

*--Setting paragraph format attribute

SET PROPERTY OF GS_PARFORMAT 'Alignment' = '3' . "Justified

*--Indent the paragraph once

GET PROPERTY OF GS_SELECTION 'Paragraphs' = GS_PARAGRAPH .

CALL METHOD OF GS_PARAGRAPH 'Indent' .

CALL METHOD OF GS_SELECTION 'TypeText'

EXPORTING

#1 = TEXT-002.

FREE OBJECT GS_WORD .

Regards,

sasi

4 REPLIES 4

Former Member
0 Kudos

Hi,

check this program, it may help you.

JUst execute it and see.

&----


*& Report YTEST_ABINASH *

*& *

&----


*& *

*& *

&----


REPORT YTEST_ABINASH .

*--Include for OLE-enabling definitions

INCLUDE OLE2INCL .

*--Global variables

*--Variables to hold OLE object and entity handles

DATA GS_WORD TYPE OLE2_OBJECT . "OLE object handle

DATA GS_DOCUMENTS TYPE OLE2_OBJECT . "Documents

DATA GS_ACTDOC TYPE OLE2_OBJECT . "Active document

DATA GS_APPLICATION TYPE OLE2_OBJECT . "Application

DATA GS_OPTIONS TYPE OLE2_OBJECT . "Application options

DATA GS_ACTWIN TYPE OLE2_OBJECT . "Active window

DATA GS_ACTPAN TYPE OLE2_OBJECT . "Active pane

DATA GS_VIEW TYPE OLE2_OBJECT . "View

DATA GS_SELECTION TYPE OLE2_OBJECT . "Selection

DATA GS_FONT TYPE OLE2_OBJECT . "Font

DATA GS_PARFORMAT TYPE OLE2_OBJECT . "Paragraph format

DATA GS_TABLES TYPE OLE2_OBJECT . "Tables

DATA GS_RANGE TYPE OLE2_OBJECT . "Range handle for various ranges

DATA GS_TABLE TYPE OLE2_OBJECT . "One table

DATA GS_TABLE_BORDER TYPE OLE2_OBJECT . "Table border

DATA GS_CELL TYPE OLE2_OBJECT . "One cell of a table

DATA GS_PARAGRAPH TYPE OLE2_OBJECT . "Paragraph

DATA GV_POS(5) TYPE N . "Position information for table

CREATE OBJECT GS_WORD 'WORD.APPLICATION' .

IF SY-SUBRC NE 0.

MESSAGE S000(SU) WITH 'Error while creating OLE object!'.

LEAVE PROGRAM .

ENDIF .

*--Setting object's visibility property

SET PROPERTY OF GS_WORD 'Visible' = '1' .

*--Opening a new document

GET PROPERTY OF GS_WORD 'Documents' = GS_DOCUMENTS .

CALL METHOD OF GS_DOCUMENTS 'Add' .

*--Getting active document handle

GET PROPERTY OF GS_WORD 'ActiveDocument' = GS_ACTDOC .

*--Getting applications handle

GET PROPERTY OF GS_ACTDOC 'Application' = GS_APPLICATION .

*--Setting the measurement unit

GET PROPERTY OF GS_APPLICATION 'Options' = GS_OPTIONS .

SET PROPERTY OF GS_OPTIONS 'MeasurementUnit' = '1' . "CM

*--Getting handle for the selection which is here the character at the

*--cursor position

GET PROPERTY OF GS_APPLICATION 'Selection' = GS_SELECTION .

GET PROPERTY OF GS_SELECTION 'Font' = GS_FONT .

GET PROPERTY OF GS_SELECTION 'ParagraphFormat' = GS_PARFORMAT .

*--Setting font attributes

SET PROPERTY OF GS_FONT 'Name' = 'Arial' .

SET PROPERTY OF GS_FONT 'Size' = '10' .

SET PROPERTY OF GS_FONT 'Bold' = '0' . "Not bold

SET PROPERTY OF GS_FONT 'Italic' = '1' . "Italic

SET PROPERTY OF GS_FONT 'Underline' = '0' . "Not underlined

*--Setting paragraph format attribute

SET PROPERTY OF GS_PARFORMAT 'Alignment' = '2' . "Right-justified

CALL METHOD OF GS_SELECTION 'TypeText'

EXPORTING

#1 = 'This is an OLE example!'.

*--Setting the view to the main document again

SET PROPERTY OF GS_VIEW 'SeekView' = '0' . "Main document view

*--Reseting font attributes for the title

SET PROPERTY OF GS_FONT 'Name' = 'Times New Roman' .

SET PROPERTY OF GS_FONT 'Size' = '16' .

SET PROPERTY OF GS_FONT 'Bold' = '1' . "Bold

SET PROPERTY OF GS_FONT 'Italic' = '0' . "Not Italic

SET PROPERTY OF GS_FONT 'Underline' = '0' . "Not underlined

*--Setting paragraph format attribute

SET PROPERTY OF GS_PARFORMAT 'Alignment' = '1' . "Centered

CALL METHOD OF GS_SELECTION 'TypeText'

EXPORTING

#1 = TEXT-000.

*--Advancing cursor to the new line

CALL METHOD OF GS_SELECTION 'TypeParagraph' .

*--Getting entity handles for the entities on the way

GET PROPERTY OF GS_ACTDOC 'Tables' = GS_TABLES .

GET PROPERTY OF GS_SELECTION 'Range' = GS_RANGE .

*--Adding a table with 3 rows and 2 columns

CALL METHOD OF GS_TABLES 'Add' = GS_TABLE

EXPORTING

#1 = GS_RANGE " Handle for range entity

#2 = '3' "Number of rows

#3 = '2'. "Number of columns

*--Setting border attribute for the table

GET PROPERTY OF GS_TABLE 'Borders' = GS_TABLE_BORDER .

SET PROPERTY OF GS_TABLE_BORDER 'Enable' = '1' . "With border

*--Filling the table with dummy data

*--Reseting font attributes for table content

SET PROPERTY OF GS_FONT 'Name' = 'Garamond' .

SET PROPERTY OF GS_FONT 'Size' = '11' .

SET PROPERTY OF GS_FONT 'Bold' = '0' . "Not bold

SET PROPERTY OF GS_FONT 'Italic' = '0' . "Not Italic

SET PROPERTY OF GS_FONT 'Underline' = '0' . "Not underlined

*--Getting cell coordinates

CALL METHOD OF GS_TABLE 'Cell'

EXPORTING

#1 = '1' "first row

#2 = '1'. "first column

*--Getting the range handle to write the text

GET PROPERTY OF GS_CELL 'Range' = GS_RANGE .

*--Filling the cell

SET PROPERTY OF GS_RANGE 'Text' = 'abinash' .

*--Getting cell coordinates

CALL METHOD OF GS_TABLE 'Cell' = GS_CELL

EXPORTING

#1 = '3' "third row

#2 = '2'. "second column

*--Getting the range handle to write the text

GET PROPERTY OF GS_CELL 'Range' = GS_RANGE .

*--Filling the cell

SET PROPERTY OF GS_RANGE 'Text' = 'OLE' .

*--Advancing the cursor to the end of the table

GET PROPERTY OF GS_TABLE 'Range' = GS_RANGE .

GET PROPERTY OF GS_RANGE 'End' = GV_POS .

SET PROPERTY OF GS_RANGE 'Start' = GV_POS .

CALL METHOD OF GS_RANGE 'Select' .

*--Skip some lines

DO 3 TIMES .

CALL METHOD OF GS_SELECTION 'TypeParagraph' .

ENDDO.

*--Reseting font attributes for ordinary text

SET PROPERTY OF GS_FONT 'Name' = 'Times New Roman' .

SET PROPERTY OF GS_FONT 'Size' = '12' .

SET PROPERTY OF GS_FONT 'Bold' = '0' . "Not bold

SET PROPERTY OF GS_FONT 'Italic' = '0' . "Not Italic

SET PROPERTY OF GS_FONT 'Underline' = '0' . "Not underlined

*--Setting paragraph format attribute

SET PROPERTY OF GS_PARFORMAT 'Alignment' = '3' . "Justified

*--Indent the paragraph once

GET PROPERTY OF GS_SELECTION 'Paragraphs' = GS_PARAGRAPH .

CALL METHOD OF GS_PARAGRAPH 'Indent' .

CALL METHOD OF GS_SELECTION 'TypeText'

EXPORTING

#1 = TEXT-002.

FREE OBJECT GS_WORD .

Regards,

sasi

0 Kudos

hi, why is it in some system, the table is workable while in other system it does not work?

Best regards,

Former Member
0 Kudos

hi,

simply use FM GUI_EXEC and provide the MS word path.

Thanks,

Gaurav

Former Member
0 Kudos

Hi,

REPORT ZWSEXECUTE.

DATA: BEGIN OF ITAB OCCURS 3,

LINE(50),

END OF ITAB.

PARAMETERS: PROG(70) DEFAULT

'C:\Program Files\Microsoft Office\Office\WINWORD.EXE'.

PARAMETERS: FILE1(70) DEFAULT 'C:\TEMP\TEST.TXT'.

  • Tick to print the Text file after saving from MS WORDS

PARAMETERS: S_UP AS CHECKBOX.

  • Tick to create new or overwrite Text file

PARAMETERS: S_NEW AS CHECKBOX.

IF S_UP = 'X'.

CALL FUNCTION 'GUI_UPLOAD'

EXPORTING

FILENAME = 'FILE1'

TABLES

DATA_TAB = ITAB

EXCEPTIONS

FILE_OPEN_ERROR = 1.

IF SY-SUBRC = 0.

LOOP AT ITAB.

WRITE: / ITAB.

ENDLOOP.

ELSE.

WRITE: / 'File open error.'.

ENDIF.

ELSE.

IF S_NEW = 'X'.

CALL FUNCTION 'GUI_DOWNLOAD'

EXPORTING

FILENAME = 'FILE1'

TABLES

DATA_TAB = ITAB

EXCEPTIONS

FILE_WRITE_ERROR = 1

NO_BATCH = 2

GUI_REFUSE_FILETRANSFER = 3

INVALID_TYPE = 4

OTHERS = 5.

ENDIF.

CASE SY-SUBRC.

WHEN 1.

WRITE: / 'GUI DOWNLOAD FILE WRITE ERROR'.

WHEN 2.

WRITE: / 'GUI DOWNLOAD NO BATCH'.

WHEN 3.

WRITE: / 'GUI DOWNLOAD GUI REFUSE FILETRANSFER'.

WHEN 4.

WRITE: / 'GUI DOWNLOAD INVALID TYPE'.

WHEN 5.

WRITE: / 'GUI DOWNLOAD OTHERS'.

ENDCASE.

CALL FUNCTION 'WS_EXECUTE'

EXPORTING

PROGRAM = PROG

COMMANDLINE = 'FILE1'

INFORM = ' '

EXCEPTIONS

FRONTEND_ERROR = 1

NO_BATCH = 2

PROG_NOT_FOUND = 3

ILLEGAL_OPTION = 4

GUI_REFUSE_EXECUTE = 5

OTHERS = 6.

CASE SY-SUBRC.

WHEN 1.

WRITE: / 'FRONTEND ERROR'.

WHEN 2.

WRITE: / 'NO BATCH'.

WHEN 3.

WRITE: / 'PROGRAM NOT FOUND'.

WHEN 4.

WRITE: / 'ILLEGA OPTION'.

WHEN 5.

WRITE: / 'GUI REFUSE EXECUTE'.

WHEN 6.

WRITE: / 'OTHERS'.

ENDCASE.

ENDIF.

Reward if helpful...

Madhavi.