View Full Version : vba batch printing
jbliss503
09-30-2011, 08:42 AM
All,
Ok... so, I am trying to perform a rather complex batch print procedure and I need some advice. I have a word document (1200 actually) and the same number of PDF documents with the same name. I am trying to construct a VBA macro that will recursively loop through the directory and send both the .docx and .PDF as a single batch print job. I have been able to successfully get the word documents to print, but am unable to get the PDF to print.
Question 1 - is this even possible
Question 2 - how do you group documents together as a single print job
Thanks in advance for the help
Jay Freedman
10-01-2011, 03:33 PM
I don't think that what you propose is literally possible, because Word can't open and print PDF documents. What you can do is fake it, by telling Word to open and print a Word document and then immediately afterward telling Adobe Acrobat Reader (or another PDF program) to print the PDF file with the same base name.
This demo will print doc1.doc immediately followed by doc1.pdf:
Sub PrintWordDoc(strDocName As String)
Dim strDocNameWithExt As String
Dim aDoc As Document
On Error Resume Next
strDocNameWithExt = strDocName & ".doc"
Set aDoc = Documents.Open(FileName:=strDocNameWithExt)
If Err.Number > 0 Then
Err.Clear
strDocNameWithExt = strDocName & ".docx"
Set aDoc = Documents.Open(FileName:=strDocNameWithExt)
If Err.Number > 0 Then
MsgBox "Could not open " & strDocName & " as .doc or .docx"
Exit Sub
End If
End If
If Not aDoc Is Nothing Then
aDoc.PrintOut Background:=False
aDoc.Close
End If
End Sub
Sub PrintPDF(strDocName As String)
Dim strDocNameWithExt As String
Dim adobePath As String
adobePath = "C:\Program Files (x86)\Adobe\Reader 10.0\Reader\acrord32.exe"
strDocNameWithExt = strDocName & ".pdf"
On Error Resume Next
Shell pathname:=adobePath & " /N /T " & strDocNameWithExt
If Err.Number > 0 Then
MsgBox "Could not print " & strDocNameWithExt
Exit Sub
End If
End Sub
Sub PrintWordAndPdf()
Dim DocName As String
' test/debug
DocName = "C:\Users\Jay\Documents\doc1"
PrintWordDoc DocName
PrintPDF DocName
End Sub
Careful: the values for adobePath and DocName are specific to my computer (using Windows 7 64-bit) and will have to be adjusted for yours.
To generalize this for your purpose, you'll have to add code to the PrintWordAndPdf macro -- use the Dir function in a loop to step through all the document names in the specified folder. Sample code may be found at http://word.mvps.org/faqs/macrosvba/ReadFilesIntoArray.htm.
awald
11-02-2011, 07:29 AM
What does the /N /T in your code do?
I am interested in manipulating this code so it prints only certain pages rather than the entire document or pdf.
jbliss503
11-02-2011, 07:36 AM
Jay, thank you for your response.
I ended up proceeding similarly to what you proposed. I called acrobat as a shell program to print the PDF. The problem that I was still running into was getting the word doc and PDF to print as 1 job and staple using the finisher on the printer. I was able to solve this problem by downloading a utility that worked off a counter to hold the set number of documents at the printer before printing. The utility works off a D word registry key value which I had to progmatically reset at the end of each loop.
Thanks for your input though!
Jay Freedman
11-02-2011, 07:06 PM
Awald,
The /N switch causes Adobe Reader to open a new instance. The /T switch followed by the filename causes it to print the file. References:
http://stackoverflow.com/questions/619158/adobe-reader-command-line-reference
http://www.robvanderwoude.com/commandlineswitches.php#Acrobat
I didn't see anything that implied that you could specify particular pages to print. If you try working with different PDF programs, you may find one that gives that capability -- I don't know of any offhand.
Powered by vBulletin® Version 4.2.5 Copyright © 2025 vBulletin Solutions Inc. All rights reserved.