Code: Alles auswählen.
* Dieser Report demonstriert die Fähigkeiten von OLE2 Automation
* von SAP zu MS Word.
* In der Tabelle OLELOAD stehen alle verfügbaren Parameter zur Verfügung
* die in der OLE Schnittstelle angewendet werden können
* OLE2 Automation global Variables
INCLUDE ole2incl.
INCLUDE docsincl.
* Wordobject
DATA: h_word TYPE ole2_object.
DATA: h_excel TYPE ole2_object.
*** Declarations for Excel ***
DATA: books TYPE ole2_object. " Sammlung Arbeitsmappen
DATA: book TYPE ole2_object. " Arbeitsmappe
DATA: cell TYPE ole2_object. " Zelle
*** END-OF-Declarations for Excel ***
SELECTION-SCREEN BEGIN OF BLOCK ole_demo WITH FRAME TITLE text-001.
* Wordselection
PARAMETERS: r1 RADIOBUTTON GROUP rad1 DEFAULT 'X'.
SELECTION-SCREEN BEGIN OF BLOCK alpha WITH FRAME.
PARAMETERS: p_text TYPE char255 DEFAULT 'Das ist ein Text',
p_bmp LIKE rlgrap-filename
DEFAULT 'h:grafikalpirsbacher1.jpg'.
SELECTION-SCREEN END OF BLOCK alpha.
* Excelselection
PARAMETERS: r2 RADIOBUTTON GROUP rad1.
SELECTION-SCREEN BEGIN OF BLOCK beta WITH FRAME.
PARAMETERS: p_table LIKE dd02l-tabname DEFAULT 'YTSCHRIFTV'.
SELECTION-SCREEN END OF BLOCK beta.
SELECTION-SCREEN END OF BLOCK ole_demo.
**************************
*** START-OF-SELECTION ***
**************************
START-OF-SELECTION.
* Word Beispiel
IF r1 = 'X'.
PERFORM bsp_ole_with_word.
ENDIF.
* Excel Beispiel
IF r2 = 'X'.
PERFORM bsp_ole_with_excel.
ENDIF.
END-OF-SELECTION.
************************
*** END-OF-SELECTION ***
************************
*&---------------------------------------------------------------------*
*& Form bsp_ole_with_word
*&---------------------------------------------------------------------*
FORM bsp_ole_with_word.
IF h_word-header = space OR h_word-handle EQ -1.
CREATE OBJECT h_word 'WORD.BASIC'.
IF sy-subrc NE 0.
MESSAGE i002 WITH sy-msgli.
ELSE.
CALL METHOD OF h_word 'AppShow'.
ENDIF.
* Worddokument öffnen
CALL METHOD OF h_word 'FILEOPEN'
EXPORTING #1 = 'c:markus32 est.doc'.
CALL METHOD OF h_word 'EDITGOTO' EXPORTING #1 = 'mark'.
DATA: char(10).
DO 5 TIMES.
MOVE sy-index TO char.
* Sprung zu Tabelle
CALL METHOD OF h_word 'STARTOFLINE'.
CALL METHOD OF h_word 'INSERT'
EXPORTING #1 = 'TEXT'.
CALL METHOD OF h_word 'NEXTCELL'.
CALL METHOD OF h_word 'INSERT'
EXPORTING #1 = sy-datum.
CALL METHOD OF h_word 'ToolsMacro'
EXPORTING #1 = 'INSERTROWBELOW' " <-- Makroname
#2 = 1. " <-- Run
ENDDO.
call method of h_word 'TABLEDELETEROW'.
STOP.
** Neues Worddokument öffnen
* CALL METHOD OF h_word 'FILENEW'.
* Normales Dokument in ein Serienbriefdoc umwandeln
CALL METHOD OF h_word 'MAILMERGEMAINDOCUMENTTYPE'
EXPORTING #1 = '1'.
** Zu normalen Worddoc umwandeln
* CALL METHOD OF h_word 'MAILMERGERESET'.
* Datenquelle mitgeben
CALL METHOD OF h_word 'MAILMERGEOPENDATASOURCE'
EXPORTING #1 = 'c: empSchriftVDaten.xls'
#2 = '0'
#3 = '0'
#4 = '1'
#5 = '1'
#6 = ''
#7 = ''
#8 = ''
#9 = ''
#10 = ''
#11 = ''
#12 = ''
#13 = 'SELECT * FROM [range1]'.
CALL METHOD OF h_word 'MailMergeToDoc'.
CALL METHOD OF h_word 'FILESAVEAS'
EXPORTING #1 = 'c: emp emp.doc'.
CALL METHOD OF h_word 'PREVWINDOW'.
* Close the Doc and do not ask for saving!!!
CALL METHOD OF h_word 'FILECLOSE' EXPORTING #1 = '2'.
* Zu einer Textmarke springen
CALL METHOD OF h_word 'EDITGOTO' EXPORTING #1 = 'HERE'.
* Textausrichtung
* LEFTPARA --> (links)
* CENTERPARA --> (zentriert)
* RIGHTPARA --> (rechts)
* JUSTIFYPARA --> (Blocksatz)
CALL METHOD OF h_word 'CENTERPARA'.
* Zwischenablage einfügen
CALL METHOD OF h_word 'EDITPASTE'.
* 5 Leerzeilen erzeugen
DO 5 TIMES.
CALL METHOD OF h_word 'INSERTPARA'.
ENDDO.
* Navigieren ('LINEDOWN' & 'LINEUP')
* Schriftgröße festlegen
CALL METHOD OF h_word 'FONTSIZE' EXPORTING #1 = '24'.
* Schriftart festlegen
CALL METHOD OF h_word 'FONT' EXPORTING #1 = 'Comic Sans MS'.
* Style festlegen
* - 'Bold' = Fett
* - 'UNDERLINE' = Unterstrichen
* - 'DOUBLEUNDERLINE' = Doppelt unterstrichen
* - 'ITALIC' = Kursiv
CALL METHOD OF h_word 'BOLD'.
* Schriftfarbe festlegen (rot)
CALL METHOD OF h_word 'CHARCOLOR' EXPORTING #1 = '6'.
* Text einfügen
CALL METHOD OF h_word 'INSERT' EXPORTING #1 = p_text.
* Zeilenumbruch
CALL METHOD OF h_word 'INSERTPARA'.
* Wörter ersetzen ('WW2_EDITREPLACE')
* CALL METHOD OF H_WORD 'WW2_EDITREPLACE'
* EXPORTING #1 = '%NAME1' #2 = CUSTOMER.
** Vorlagenpfad ändern auf normal
* CALL METHOD OF h_word 'FILETEMPLATES' exporting #1 = ''
* #2 = 'normal'.
* Neue Seite beginnen (= 'INSERTBREAK')
* Bitmapdatei einfügen
IF p_bmp CS 'JPG' OR p_bmp CS 'BMP'.
CALL METHOD OF h_word 'INSERTPICTURE' EXPORTING #1 = p_bmp.
ENDIF.
ELSE.
MESSAGE i208(00) WITH 'Interner Fehler'.
ENDIF.
* Textbox einfügen
* CALL METHOD OF h_word 'DRAWTEXTBOX '
* Ausschneiden ('EditCut')
* Kopieren ('EditCopy')
* Einfügen ('EditPaste')
* Löschen ('EditDelete')
* Alles markieren ('EDITSELECTALL')
* Aufzählung ('FORMATBULLETSANDNUMBERING', 'FORMATNUMBER')
DO 10 TIMES.
* Zeilenumbruch
CALL METHOD OF h_word 'INSERTPARA'.
* Nummerierung
CALL METHOD OF h_word 'FORMATNUMBER'.
CALL METHOD OF h_word 'INSERT' EXPORTING #1 = 'TEXTpassage'.
ENDDO.
* Aktuelle Zeile markieren
CALL METHOD OF h_word 'STARTOFLINE'.
CALL METHOD OF h_word 'SELECTCURALIGNMENT'.
* Schriftart festlegen (für die ganze Zeile)
CALL METHOD OF h_word 'FONT' EXPORTING #1 = 'Verdana'.
* Springt zum Dokumentanfang
CALL METHOD OF h_word 'STARTOFDOCUMENT'.
*** MARKIERUNGEN ***
* 1 LOOP - Begin selection
* 2 LOOPS - Select word
* 3 LOOPS - Select sentence
* 4 LOOPS - Select paragraph
* 5 LOOPS - Select current section
* 6 LOOPS - Select whole document (if multiselection)
*do x times.
* CALL METHOD OF h_word 'EXTENDSELECTION'.
*enddo.
********************
** Nummerierung aufheben
* CALL METHOD OF h_word 'RESETPARA'.
ENDFORM. " bsp_ole_with_word
*&---------------------------------------------------------------------*
*& Form bsp_ole_with_excel
*&---------------------------------------------------------------------*
FORM bsp_ole_with_excel.
TABLES: dd02l.
DATA: lt_dd02l TYPE TABLE OF dd02l.
* Excelobjekt erzeugen
IF h_excel-header = space OR h_excel-handle EQ -1.
CREATE OBJECT h_excel 'EXCEL.APPLICATION'.
IF sy-subrc NE 0.
MESSAGE i002 WITH sy-msgli.
ELSE.
CALL METHOD OF h_excel 'WORKBOOKS' = books.
ENDIF.
* Vorhandenes Workbook File
* CALL METHOD OF books 'Open'
* EXPORTING #1 = 'C:TempSchriftVDaten.xls'.
CALL METHOD OF books 'ADD' = book.
* Neues Fenster öffnen
* CALL METHOD OF h_excel 'NEWWINDOW'.
SELECT * FROM dd02l INTO TABLE lt_dd02l UP TO 100 ROWS.
LOOP AT lt_dd02l INTO dd02l.
WRITE: / sy-tabix.
* Daten in die Zellen übergeben
PERFORM fill_cell USING sy-tabix 1 dd02l-tabname.
PERFORM fill_cell USING sy-tabix 2 dd02l-as4local.
PERFORM fill_cell USING sy-tabix 3 dd02l-as4vers.
PERFORM fill_cell USING sy-tabix 4 dd02l-sqltab.
PERFORM fill_cell USING sy-tabix 5 dd02l-datmin.
PERFORM fill_cell USING sy-tabix 6 dd02l-datmax.
PERFORM fill_cell USING sy-tabix 7 dd02l-as4user.
ENDLOOP.
* Excelsheet sichtbar machen
SET PROPERTY OF h_excel 'VISIBLE' = 1.
* Exel beenden
* call method of h_excel 'QUIT'.
ELSE.
MESSAGE i208(00) WITH 'Interner Fehler'.
ENDIF.
ENDFORM. " bsp_ole_with_excel
*&---------------------------------------------------------------------*
*& Form bsp_ole_with_excel
*&---------------------------------------------------------------------*
FORM fill_cell USING value(x) value(y) value(wert).
CALL METHOD OF h_excel 'CELLS' = cell EXPORTING #1 = x
#2 = y.
SET PROPERTY OF cell 'VALUE' = wert.
ENDFORM.
Code: Alles auswählen.
Sub Makro1()
'
' Makro1 Makro
' Makro aufgezeichnet am 21.02.2007 von cheffe
'
ActivePrinter = "Easy PDF Creator"
Application.PrintOut FileName:="", Range:=wdPrintAllDocument, Item:= _
wdPrintDocumentContent, Copies:=1, Pages:="", PageType:=wdPrintAllPages, _
ManualDuplexPrint:=False, Collate:=True, Background:=True, PrintToFile:= _
False, PrintZoomColumn:=0, PrintZoomRow:=0, PrintZoomPaperWidth:=0, _
PrintZoomPaperHeight:=0
End Sub
Code: Alles auswählen.
CALL METHOD OF WORDOBJ 'ToolsMacro'
EXPORTING #1 = 'MAKRO1' " <-- Makroname
#2 = 1. " <-- Run