Tipp 9.3 - Word zum Drucken öffnen

Wie kann ich Word zum Drucken eines Dokuments benutzen?

Mit dieser Funktion können Sie Word zum Drucken 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 WordPrintDocument( _
                sOpenFile As String, _
                Optional bAppShow As Boolean = False) _
                As Boolean
  '// -----------------------------------------------------
  '// Methode:   | Druckt mit Word ein bestimmtes Dokument
  '// -----------------------------------------------------
  '// Parameter: | sOpenFile - gültiger Pfad zum Dokument
  '//            | bAppShow - Flag, ob Word angezeigt
  '//            |            werden soll
  '// -----------------------------------------------------
  '// 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.FileOpen sOpenFile
    objWord.FilePrintDefault
    If bAppShow Then
      objWord.AppRestore
      objWord.AppShow
    Else
      objWord.FileClose 2
      objWord.Quit
      Set objWord = Nothing
    End If
    WordPrintDocument = True
  End If
 
ExitMethod:
  Err.Clear
  Exit Function
 
NoWordFound:
  Resume ExitMethod
 
End Function