Code: Alles auswählen.
*--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
*--Setting font attributes
*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
DEFINE WordCreate.
*--Creating OLE object handle variable
*CREATE OBJECT gs_word 'WORD.APPLICATION' .
*IF sy-subrc NE 0 .
*MESSAGE s000(su) WITH 'Error while creating OLE object!'.
*LEAVE PROGRAM .
*ENDIF .
CREATE OBJECT gs_word 'Word.Basic'.
CALL METHOD OF gs_word 'FileNew'
EXPORTING #1 = 'normal.dot'.
END-OF-DEFINITION.
DEFINE WordFree.
FREE OBJECT gs_word .
END-OF-DEFINITION.
*--Setting object's visibility property
define WordVisible.
if &1 = '0'.
CALL METHOD OF gs_word 'AppHide'.
else.
CALL METHOD OF gs_word 'AppShow'.
endif.
END-OF-DEFINITION.
DEFINE WordFontName.
CALL METHOD OF gs_word 'Font' EXPORTING #01 = &1.
END-OF-DEFINITION.
DEFINE WordFontSize.
CALL METHOD OF gs_word 'Size' EXPORTING #01 = &1.
END-OF-DEFINITION.
DEFINE WordInsert.
CALL METHOD OF gs_word 'Insert' EXPORTING #01 = &1.
END-OF-DEFINITION.
DEFINE create_new_instance.
create object gs_word 'Word.Basic.8'.
END-OF-DEFINITION.
DEFINE appshow.
call method of gs_word 'AppShow'.
END-OF-DEFINITION.
DEFINE apphide.
call method of gs_word 'AppHide'.
END-OF-DEFINITION.
DEFINE filenew.
call method of gs_word 'FileNew'
exporting
#1 = 'normal.dot'.
END-OF-DEFINITION.
DEFINE fileopen.
call method of gs_word 'FileOpen'
exporting
#1 = &1.
END-OF-DEFINITION.
DEFINE filesaveas.
call method of gs_word 'FileSaveAs'
exporting
#1 = &1.
END-OF-DEFINITION.
DEFINE filecloseall.
call method of gs_word 'FileCloseAll'
exporting
#1 = 2.
END-OF-DEFINITION.
DEFINE editreplace.
*EditReplace [.Find = text] [.Replace = text]
*[.Direction = number] [.MatchCase = number]
*[.WholeWord = number]
*[.PatternMatch = number]
*[.SoundsLike = number] [.FindNext]
*[.ReplaceOne] [.ReplaceAll]
*[.Format = number] [.Wrap = number]
call method of gs_word 'EditReplace'
exporting
#01 = &1
#02 = &2
#03 = 0
#04 = 0
#05 = 0
#06 = 0
#07 = 0
#08 = 0
#09 = 0
#10 = 1
#11 = 0
#12 = 1.
END-OF-DEFINITION.
DEFINE font.
call method of gs_word 'Font' exporting #01 = &1.
call method of gs_word 'FontSize' exporting #1 = &2.
END-OF-DEFINITION.
DEFINE insert1.
call method of gs_word 'Insert' exporting #1 = &1.
END-OF-DEFINITION.
DEFINE viewheader.
call method of gs_word 'ViewHeader'.
END-OF-DEFINITION.
DEFINE closeviewheaderfooter.
*** Kopffenster wieder schließen
call method of gs_word 'CloseViewHeaderFooter'.
END-OF-DEFINITION.
DEFINE CharCol.
*Color Explanation
*0 (zero) Auto
*1 Black
*2 Blue
*3 Cyan
*4 Green
*5 Magenta
*6 Red
*7 Yellow
*8 White
*9 Dark Blue
*10 Dark Cyan
*11 Dark Green
*12 Dark Magenta
*13 Dark Red
*14 Dark Yellow
*15 Dark Gray
*16 Light Gray
* &1 = integer !!!
call method of gs_word 'CharColor'
exporting
#01 = &1.
END-OF-DEFINITION.
*FORM write_text USING text font size look.
*
* CALL METHOD OF gs_word 'Font' EXPORTING #01 = font.
* CALL METHOD OF gs_word 'FontSize' EXPORTING #1 = size.
*
* IF look NE ''.
* CALL METHOD OF gs_word look.
* ENDIF.
*
* CALL METHOD OF gs_word 'Insert' EXPORTING #1 = text.
*
*ENDFORM. "write_text
Code: Alles auswählen.
* Eröffne Instanz
create_new_instance.
appshow.
fileopen 'c:\temp\000.doc'.
filesaveas 'c:\temp\001.doc'.
filecloseall.
FREE OBJECT gs_word.
Code: Alles auswählen.
*&---------------------------------------------------------------------*
*& Report ZTEST9
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*
REPORT ztest9.
* Makro include
include Y_WORD_OLE.
parameters : text(20).
WordCreate.
appshow.
do 20 times.
CALL METHOD OF gs_word 'Insert' EXPORTING #1 = text.
enddo.
filesaveas 'C:\abs.doc'.
filecloseall.
FREE OBJECT gs_word .
Code: Alles auswählen.
* Nützliche Konstanten
CONSTANTS crlf(2) VALUE %_cr_lf.
CONSTANTS htab(1) VALUE %_horizontal_tab.
* Gebe eine Textzeile aus
PERFORM write_text USING crlf 'Calligraphic' 18 ''.
PERFORM write_text USING 'Hallo' 'Arial' 13 'Bold'.
PERFORM write_text USING crlf 'Calligraphic' 18 ''.
*oder
PERFORM write_text USING crlf 'Arial' 10 ''.
PERFORM write_text USING htab 'Arial' 10 ''.
PERFORM write_text USING '? Kerstin' 'Arial' 10 ''.
PERFORM write_text USING crlf 'Arial' 10 ''.
* Routine für Textausgabe in Word
FORM write_text USING text font size look.
CALL METHOD OF gs_word 'Font' EXPORTING #01 = font.
CALL METHOD OF gs_word 'FontSize' EXPORTING #1 = size.
IF look NE ''.
CALL METHOD OF gs_word look.
ENDIF.
CALL METHOD OF gs_word 'Insert' EXPORTING #1 = text.
ENDFORM. "write_text
* gehe zum Ende des dokuments
* CALL METHOD OF gs_word 'EndOfDocument'.
* Neue Seite
* CALL METHOD OF gs_word 'InsertPageBreak'.
* Setze Tabulator in der aktuellen Zeile
* CALL METHOD OF gs_word 'FormatTabs'
* EXPORTING
* #01 = '14' "Tab position
* #02 = '1.25' "Default Tabposition
* #03 = '0' "CONSTANT ??
* #04 = '2' "Alignment 0=left/1=center/2=right
* #05 = '1' "/2=minus/3=continous line
* #06 = '0' "Fill with 0=space/1=points
* #07 = '0' "CONSTANT ??
* #08 = '3'. "Tabstop 0=set/1=clear/3=clear all
Code: Alles auswählen.
data htab type c.
data crlf(2) type c.
*
CLASS cl_abap_char_utilities DEFINITION LOAD.
crlf = cl_abap_char_utilities=>cr_lf.
htab = cl_abap_char_utilities=>HORIZONTAL_TAB.
Code: Alles auswählen.
Definition aus der WORD-Help Datei (=12 Parameter)
*EditReplace [.Find = text] [.Replace = text]
*[.Direction = number] [.MatchCase = number]
*[.WholeWord = number]
*[.PatternMatch = number]
*[.SoundsLike = number] [.FindNext]
*[.ReplaceOne] [.ReplaceAll]
*[.Format = number] [.Wrap = number]
Umsetzung in ABAP-Methoden-Aufruf (= 12 Parameter)
call method of gs_word 'EditReplace'
exporting
#01 = &1
#02 = &2
#03 = 0
#04 = 0
#05 = 0
#06 = 0
#07 = 0
#08 = 0
#09 = 0
#10 = 1
#11 = 0
#12 = 1.
Code: Alles auswählen.
Sehr geehrte $0001.
Code: Alles auswählen.
*&---------------------------------------------------------------------*
*& Report ZTEST1 *
*& *
*&---------------------------------------------------------------------*
*& *
*& *
*&---------------------------------------------------------------------*
REPORT ztest1 .
INCLUDE y_word_ole.
DATA htab TYPE c.
DATA crlf(2) TYPE c.
DATA ltab TYPE TABLE OF tab512 WITH HEADER LINE.
DATA options TYPE TABLE OF rfc_db_opt.
DATA fields TYPE TABLE OF rfc_db_fld WITH HEADER LINE.
* in 4.7 gibt es Konstanten dafür
CLASS cl_abap_char_utilities DEFINITION LOAD.
crlf = cl_abap_char_utilities=>cr_lf.
htab = cl_abap_char_utilities=>horizontal_tab.
fields-fieldname = 'CARRID'. append fields.
fields-fieldname = 'CONNID'. append fields.
fields-fieldname = 'FLDATE'. append fields.
fields-fieldname = 'PRICE'. append fields.
fields-fieldname = 'CURRENCY'. append fields.
fields-fieldname = 'PLANETYPE'. append fields.
CALL FUNCTION 'RFC_READ_TABLE'
EXPORTING
query_table = 'SFLIGHT'
delimiter = htab
* NO_DATA = ' '
* ROWSKIPS = 0
* ROWCOUNT = 10
TABLES
options = options
fields = fields
data = ltab
EXCEPTIONS
table_not_available = 1
table_without_data = 2
option_not_valid = 3
field_not_valid = 4
not_authorized = 5
data_buffer_exceeded = 6
OTHERS = 7.
IF sy-subrc <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
* WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.
* Erzeuge WORD - Instanz
create_new_instance.
* Neue Datei
filenew.
* WORD sichtbar (unsichtbar geht schneller) für Test
CALL METHOD OF gs_word 'AppShow'.
* Daten aus ltab in Word einfügen
LOOP AT ltab.
* BEISPIEL TABULATOR SETZEN
*FormatTabs [.Position = text] [, .DefTabs = number or text]
*[, .Align = number] [, .Leader = number]
*[, .Set] [, .Clear] [, .ClearAll]
*Argument Explanation
*
*.Position Position of the tab stop in a text measurement
*.DefTabs Position for default tab stops
* in the document in points or a text measurement
*
*Argument Explanation
*.Align Alignment of the tab stop:
*0 (zero) Left
*1 Center
*2 Right
*3 Decimal
*4 Bar
*
*.Leader The leader character for the tab stop:
*0 (zero) None
*1 Period
*2 Hyphen
*3 Underscore
*
*.Set Sets the specified custom tab stop.
*.Clear Clears the specified custom tab stop.
*.ClearAll Clears all custom tab stops.
CALL METHOD OF gs_word 'FormatTabs'
EXPORTING
#01 = '0.5' "Tab position
#02 = '1' "Default Tabposition
#03 = 0 "leader character
#04 = 0 "Alignment 0=left/1=center/2=right
#05 = 1 "Tabstop set
#06 = 0 "Tabstop clear
#07 = 0. "Tabstop clear all
*Textzeile ausgeben
PERFORM write_text USING ltab 'Courier New' 8 ''.
*Return ausgeben
insert1 crlf.
ENDLOOP.
* Abspeichern
filesaveas 'C:\temp\Test.doc'.
* Word schliessen
CALL METHOD OF gs_word 'AppClose'.
* Objekt freigeben
wordfree.
* ..und Tschüss
EXIT.
*_______________________________________________________
* Textzeile ausgeben
FORM write_text USING text font size look.
CALL METHOD OF gs_word 'Font' EXPORTING #01 = font.
CALL METHOD OF gs_word 'FontSize' EXPORTING #1 = size.
IF look NE ''.
CALL METHOD OF gs_word look.
ENDIF.
CALL METHOD OF gs_word 'Insert' EXPORTING #1 = text.
ENDFORM. "write_text
Code: Alles auswählen.
*InsertBreak [.Type = number]
*
*0 (zero) or omitted Page break
*1 Column break
*2 Next Page section break
*3 Continuous section break
*4 Even Page section break
*5 Odd Page section break
*6 Line break (newline character)
* TESTET in 4.6
CALL METHOD OF gs_word 'InsertBreak'
EXPORTING
#01 = 6.
Code: Alles auswählen.
*&---------------------------------------------------------------------*
*& Report ZTEST1 *
*& *
*&---------------------------------------------------------------------*
*& *
*& *
*&---------------------------------------------------------------------*
REPORT ztest1 .
INCLUDE y_word_ole.
DATA htab TYPE c.
DATA crlf(2) TYPE c.
DATA tabpos(4) TYPE p DECIMALS 1.
DATA pos(4).
DATA: BEGIN OF sf OCCURS 0,
carrid TYPE s_carr_id,
connid TYPE s_conn_id,
fldate TYPE s_date,
price TYPE s_price,
currency TYPE s_currcode,
planetype TYPE s_planetye,
END OF sf.
DATA temp(10).
* in 4.7 gibt es Konstanten dafür
CLASS cl_abap_char_utilities DEFINITION LOAD.
crlf = cl_abap_char_utilities=>cr_lf.
htab = cl_abap_char_utilities=>horizontal_tab.
* Erzeuge WORD - Instanz
create_new_instance.
* Neue Datei
filenew.
* WORD sichtbar (unsichtbar geht schneller) für Test
CALL METHOD OF gs_word 'AppShow'.
SELECT * FROM sflight INTO CORRESPONDING FIELDS OF sf.
*up to 20 rows.
APPEND sf.
ENDSELECT.
LOOP AT sf.
*Textzeile ausgeben
charcol 1.
PERFORM write_text USING sf-carrid 'Verdana' 10 'b'.
insert1 htab.
*Textzeile ausgeben
charcol 2.
PERFORM write_text USING sf-connid 'Verdana' 10 'i'.
insert1 htab.
*Textzeile ausgeben
charcol 3.
PERFORM write_text USING sf-fldate 'Verdana' 10 'u'.
insert1 htab.
*Textzeile ausgeben
WRITE sf-price TO temp.
charcol 4.
PERFORM write_text USING temp 'Verdana' 10 ''.
insert1 htab.
*Textzeile ausgeben
charcol 5.
PERFORM write_text USING sf-currency 'Verdana' 10 'bu'.
insert1 htab.
*Textzeile ausgeben
charcol 6.
PERFORM write_text USING sf-planetype 'Verdana' 10 'iu'.
insert1 htab.
*InsertBreak [.Type = number]
*
*0 (zero) or omitted Page break
*1 Column break
*2 Next Page section break
*3 Continuous section break
*4 Even Page section break
*5 Odd Page section break
*6 Line break (newline character)
CALL METHOD OF gs_word 'InsertBreak'
EXPORTING
#01 = 6.
ENDLOOP.
* Abspeichern
filesaveas 'C:\temp\Test.doc'.
* Word schliessen
CALL METHOD OF gs_word 'AppClose'.
* Objekt freigeben
wordfree.
* ..uns Tschüss
EXIT.
*_______________________________________________________
* Text ausgeben
FORM write_text USING text font size look.
CALL METHOD OF gs_word 'Font' EXPORTING #01 = font.
CALL METHOD OF gs_word 'FontSize' EXPORTING #1 = size.
IF look CA 'b'.
CALL METHOD OF gs_word 'Bold' EXPORTING #01 = 1.
ENDIF.
IF look CA 'i'.
CALL METHOD OF gs_word 'Italic' EXPORTING #01 = 1.
ENDIF.
IF look CA 'u'.
CALL METHOD OF gs_word 'Underline' EXPORTING #01 = 1.
ENDIF.
CALL METHOD OF gs_word 'Insert' EXPORTING #1 = text.
IF NOT look IS INITIAL.
CALL METHOD OF gs_word 'Bold' EXPORTING #01 = 0.
CALL METHOD OF gs_word 'Italic' EXPORTING #01 = 0.
CALL METHOD OF gs_word 'Underline' EXPORTING #01 = 0.
ENDIF.
ENDFORM. "write_text