Batch file to print Word documents

creative-2008

Weaksauce
Joined
Dec 23, 2008
Messages
100
I have forty Word documents that are all two pages long. I need to print just the second page of all of these documents. Is this possible with a small batch file?

I've had no luck with Google and a quick question here is my last resort. If anybody knows off the top of their head that would be fantastic. However, if not I'll set about printing them all manually, as I don't really have the time to look into it properly.

I'd be very grateful if anyone knows how this an be done, as it's obviously going to save me a fair bit of time, and a lot of frustration!
 
I'm not sure of any way to print a specific page directly from a batch file.

http://support.microsoft.com/kb/210565

/mmacroname
Starts Word and then runs a specific macro. The /m switch also prevents Word from running any AutoExec macros.

So if you setup a macro to print page 2 and exit, you could then create a batch file to launch word using this /m switch.

Code:
for /f %x in ('dir c:\temp\*.docx') do (start winword.exe  %x /mMacroName)

Not really tested, but should work in theory.

I toyed with using the Word COM interface via powershell, but trying to get the "printout" method to only print page 2 gave me a headache.

...but if I had to do this on a regular basis, I'd want the script done in VBS or Powershell.
 
Word Macro:

Sub PrintOnly_PAGE2()
'
' PrintOnly_PAGE2 Macro
'
'
Dim sMyDir As String
Dim sDocName As String
' The path to obtain the files.
sMyDir = "C:\temp\docs\"
sDocName = Dir(sMyDir & "*.DOCX")
While sDocName <> ""
' Print the file.
Application.PrintOut Range:=wdPrintRangeOfPages, Pages:="2", FileName:=sMyDir & sDocName
' Get next file name.
sDocName = Dir()
Wend
End Sub
 
I used the Word Macro, and this code in the batch file:

Code:
start winword.exe  "TestFile" /mPrintOnly_PAGE2

'TestFile' is the actual name of a file in that directory. The combination of the batch file and the Word Macro caused page 2 of every document in that directory to be printed.

Thanks very much for this because it worked perfectly and saved me a significant amount of time. One last question though, how could I tidy up the batch file? I'm not sure that my editing has left it looking quite right, particularly with reference to the document name. The scripts will only run correctly if 'TestFile' exists. Is there a variable I could put in there that will just select the first Word document in that directory?
 
remove the While loop and it'll only run once.
or you can specify the sDocName instead of the entire Directory
 
Back
Top