Printing MSWord document to PDF from MSAccess

About the programming interface for the virtual PDF printer driver.

Moderator: jr

Printing MSWord document to PDF from MSAccess

Postby frigby » 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

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

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
.Documents.Close SaveChanges:=False
End With
Set objWord = Nothing
Set oPrinterSettings = Nothing
Set oPrinterUtil = Nothing

End Sub
Posts: 1
Joined: Sat Apr 21, 2012 8:28 am

Re: Printing MSWord document to PDF from MSAccess

Postby admin » Thu Apr 26, 2012 8:23 am

Your print jobs are overwriting the settings from a previous print job in your loop. This happens if you save the settings for the next print job before the current job has picked up the runonce.ini setting file.

Take a look at this.
Posts: 58
Joined: Fri Oct 28, 2011 8:09 pm

Return to Programming

Who is online

Users browsing this forum: No registered users and 2 guests