Tipp 9.4 - Dokument in Word erstellen
Wie kann ich Word zum Erstellen eines Dokuments benutzen?
Mit dieser Funktion können Sie Word zum Erstellen eines Dokuments benutzen. Kopieren Sie einfach nachfolgenden Quellcode in die Zwischenablage und fügen Sie anschließend den Inhalt der Zwischenablage in ein neues Modul ein. Die Aufrufparameter finden Sie im Quellcode beschrieben.
Beispiel:
Option Explicit Public Function WordCreateDocument( _ sBuffer As String, _ Optional sSaveAs As String = vbNullString, _ Optional bPrintAndQuit As Boolean = False) _ As Boolean '// ----------------------------------------------------- '// Methode: | Erstellt mit Word ein neues Dokument '// ----------------------------------------------------- '// Parameter: | sBuffer - Daten für das neue Dokument '// | sSaveAs - neuer Dateipfad '// | bPrintAndQuit - Drucken & Schließen-Flag '// ----------------------------------------------------- '// Rückgabe: | True bei Erfolg '// ----------------------------------------------------- '// Autor: | Stefan Kulpa '// | EDV Innovation & Consulting - Dormagen '// ----------------------------------------------------- On Error Resume Next Dim objWord As Object Set objWord = GetObject(, "Word.Basic") If Err.Number <> 0 Then Err.Clear Set objWord = GetObject("", "Word.Basic") If Err.Number <> 0 Then On Error GoTo NoWordFound Err.Clear Set objWord = CreateObject("Word.Basic") End If End If If Not objWord Is Nothing Then objWord.FileNew objWord.Insert sBuffer If Len(sSaveAs) > 0 Then objWord.FileSaveAs sSaveAs If bPrintAndQuit Then objWord.FilePrintDefault objWord.FileClose 2 objWord.Quit Set objWord = Nothing Else objWord.AppRestore objWord.AppShow End If WordCreateDocument = True End If ExitMethod: Err.Clear Exit Function NoWordFound: Resume ExitMethod End Function