Printing MSWord document to PDF from MSAccess
Posted: Sat Apr 21, 2012 8:45 am
I have dozens of Microsoft Word Documents that I want to print to pdf. I loop through a directory and configure bullzip pdf printer to print to pdf for each word doc. This works pretty well although slow. In testing, I found that after about 4 documents, the print dialog box pops up and prompts me where to save the file. I want unattended printing without dialog popup. Any suggestions? Here's my code.
Sub Print2PDF(ByVal docFileName As String, ByVal pdfFileName As String)
Dim iPdfPrinterIndex As Integer
Dim sCurrentPrinterName As String
Dim iCurrentPrinterIndex As Integer
Dim i As Integer
Dim sCurrentDir As String
Dim oPrinterSettings As Object
Dim oPrinterUtil As Object
Dim sPrinterName As String
Dim objWord As Word.Application
Dim strPDFName As String
Dim strDirectory As String
DoEvents
Set oPrinterSettings = CreateObject("bullzip.PdfSettings")
Set oPrinterUtil = CreateObject("bullzip.PdfUtil")
Rem -- Get default printer name
sPrinterName = "Bullzip PDF Printer"
Rem -- Find the index of the printer that we want to use
iPdfPrinterIndex = -1
For i = 0 To Application.Printers.Count - 1
If Application.Printers.Item(i).DeviceName = sPrinterName Then
iPdfPrinterIndex = i
End If
Next
Rem -- Exit here if the pdf printer was not found
If iPdfPrinterIndex = -1 Then
MsgBox "The printer '" & sPrinterName & "' was not found on this computer."
Exit Sub
End If
Rem -- Configure the PDF printer
With oPrinterSettings
.printerName = sPrinterName
Rem -- Set the destination file name of the PDF document
.SetValue "output", pdfFileName
Rem -- Control the dialogs when printing
.SetValue "ConfirmOverwrite", "no"
.SetValue "ShowSaveAS", "never"
.SetValue "ShowSettings", "never"
.SetValue "ShowPDF", "no"
Rem -- Set document properties
.SetValue "Target", "printer"
.SetValue "Title", strPDFName
.SetValue "Subject", " Document generated at " & Now
Rem -- Display page thumbs when the document is opened
.SetValue "UseThumbs", "no"
Rem -- Set the zoom factor to 50%
.SetValue "Zoom", "100"
Rem -- Place a stamp in the lower right corner
' .SetValue "WatermarkText", "ACCESS DEMO"
' .SetValue "WatermarkVerticalPosition", "bottom"
' .SetValue "WatermarkHorizontalPosition", "right"
' .SetValue "WatermarkVerticalAdjustment", "3"
' .SetValue "WatermarkHorizontalAdjustment", "1"
' .SetValue "WatermarkRotation", "90"
' .SetValue "WatermarkColor", "#ff0000"
' .SetValue "WatermarkOutlineWidth", "1"
Rem -- Write the settings to the runonce.ini file
.WriteSettings True
End With
'Dim worddoc As Word.Document
Set objWord = OpenWordApp
With objWord
.Documents.Open FileName:=docFileName
.ActivePrinter = sPrinterName
.PrintOut
.Documents.Close SaveChanges:=False
.Quit
End With
Set objWord = Nothing
Set oPrinterSettings = Nothing
Set oPrinterUtil = Nothing
DoEvents
End Sub
Sub Print2PDF(ByVal docFileName As String, ByVal pdfFileName As String)
Dim iPdfPrinterIndex As Integer
Dim sCurrentPrinterName As String
Dim iCurrentPrinterIndex As Integer
Dim i As Integer
Dim sCurrentDir As String
Dim oPrinterSettings As Object
Dim oPrinterUtil As Object
Dim sPrinterName As String
Dim objWord As Word.Application
Dim strPDFName As String
Dim strDirectory As String
DoEvents
Set oPrinterSettings = CreateObject("bullzip.PdfSettings")
Set oPrinterUtil = CreateObject("bullzip.PdfUtil")
Rem -- Get default printer name
sPrinterName = "Bullzip PDF Printer"
Rem -- Find the index of the printer that we want to use
iPdfPrinterIndex = -1
For i = 0 To Application.Printers.Count - 1
If Application.Printers.Item(i).DeviceName = sPrinterName Then
iPdfPrinterIndex = i
End If
Next
Rem -- Exit here if the pdf printer was not found
If iPdfPrinterIndex = -1 Then
MsgBox "The printer '" & sPrinterName & "' was not found on this computer."
Exit Sub
End If
Rem -- Configure the PDF printer
With oPrinterSettings
.printerName = sPrinterName
Rem -- Set the destination file name of the PDF document
.SetValue "output", pdfFileName
Rem -- Control the dialogs when printing
.SetValue "ConfirmOverwrite", "no"
.SetValue "ShowSaveAS", "never"
.SetValue "ShowSettings", "never"
.SetValue "ShowPDF", "no"
Rem -- Set document properties
.SetValue "Target", "printer"
.SetValue "Title", strPDFName
.SetValue "Subject", " Document generated at " & Now
Rem -- Display page thumbs when the document is opened
.SetValue "UseThumbs", "no"
Rem -- Set the zoom factor to 50%
.SetValue "Zoom", "100"
Rem -- Place a stamp in the lower right corner
' .SetValue "WatermarkText", "ACCESS DEMO"
' .SetValue "WatermarkVerticalPosition", "bottom"
' .SetValue "WatermarkHorizontalPosition", "right"
' .SetValue "WatermarkVerticalAdjustment", "3"
' .SetValue "WatermarkHorizontalAdjustment", "1"
' .SetValue "WatermarkRotation", "90"
' .SetValue "WatermarkColor", "#ff0000"
' .SetValue "WatermarkOutlineWidth", "1"
Rem -- Write the settings to the runonce.ini file
.WriteSettings True
End With
'Dim worddoc As Word.Document
Set objWord = OpenWordApp
With objWord
.Documents.Open FileName:=docFileName
.ActivePrinter = sPrinterName
.PrintOut
.Documents.Close SaveChanges:=False
.Quit
End With
Set objWord = Nothing
Set oPrinterSettings = Nothing
Set oPrinterUtil = Nothing
DoEvents
End Sub