Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Automatically Print Word MailMerge Label 1

Status
Not open for further replies.

BJZeak

Programmer
May 3, 2008
230
CA
There appears to be some solutions including a FAQ referring to this topic ... really not wanting to reinvent the wheel ... what I have done so far seems so simple and yet I haven't found a way to make it work ... wonder if there is a solution: All I want to do is use the Word merge template to print a defined Label. (ACCESS's REPORT BUILDER isn't working properly with the Printer/Label combination ... it continually wants to Form Feed half the Labels away where as I have Word working properly for one label at a time.)

Word 2K3 setup for one Label MailMerge with 5 fields linked to an MDB record.

I can manually right click on this word file and select print ... word starts up, automatically gets the data, prints, then shuts down which is exactly what I would like it to do from a button in ACCESS 2K3.

Looking at word command line there doesn't appear to be a way to just say docmd.runcommand "word.exe /p c:\temp\some.doc" (was expecting something like /p for print)

I created a word object from the class word.document but it also doesn't accept an objWord.Print either even though it compiles without an error it gives a runtime error.

Is there a simple way to do this given that Word already knows what to do?
 
Well after poking around a bit more I finally found a list of methods for the word.document class object on MSDN

PRINTOUT is the command/method I needed ... the only caveate is that the document must be active first

I tried to close the document and then release the object but this caused errors ... hopefully the built in destructors in VBA objects will take care of this ... the document isn't left open so perhaps not an issue?


Private Sub frmPrint_Click()
On Error GoTo Err_frmPrint_Click
Dim objWord As Object
Set objWord = GetObject("r:\PartLabel.doc", "word.document")

objWord.Application.Visible = False
objWord.Activate
objWord.PrintOut
'objWord.Close --- causes an error

'objWord = Nothing ---- also causes and error

Exit_frmPrint_Click:
Exit Sub

Err_frmPrint_Click:
MsgBox Err.Description
Resume Exit_frmPrint_Click

End Sub
 
Set objWord = GetObject("r:\PartLabel.doc", "word.document")
objWord.Application.Visible = False
objWord.Activate
objWord.PrintOut [!]False[/!]
objWord.Close [!]0[/!] 'wdDoNotSaveChanges
[!]Set[/!] objWord = Nothing

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
OK now running into another issue.

Office 2k3 doesn't appear to provide a way to save the printer choice with the Word Document ... everytime any document is loaded Word picks up the default printer for "THIS" machine

Went back to MSDN and in a round about way found that I could set the application.activeprinter property with:

objword.application.activeprinter = "\\network\labelprinter on LPT1:"

(I only clued in to the "on LPT1:" after reviewing the property in the immediate window and doing some fiddling ... without this you just get printer errors)

Issue: setting this property affects the WINDOWS Printer Active Printer Setting as well as the applications property (the default printer setting is cleared leaving no default printer) ... as a work around I saved the property before changing it then set it back after the PRINTOUT method is called.

I guess I was expecting a cleaner way to do this ... my concern is if someone else attempts to print something while this label is being processed their print job may go to the wrong printer.

Being that objword is in my expectation an object containing only the WORD document I wouldn't expect changing the property of that object to affect a SYSTEM/OS property???

Anyone have another possible suggestion? ie is there another "hidden" property/method of the word object that might allow me to choose the printer without affecting the OS settings?
 
Still struggling with this process ... it works to a point but has some side effects that I haven't been able to resolve.

The main problem is handling Word's close behavior:

1) If Word is not running Word starts up is hidden, loads the print document, selects the printer and prints ... then it should close the document/application? ... What appears to happen instead is Word stays hidden and doesn't close.

2) If Word is already running with another document, it is hidden, loads up the print document, select the printer and prints ... again word remains hidden and any document open prior to the vba run is in limbo

So I added an objword.application.visible = true just prior to the close statement

This resolves issue 2 but for issue 1 a Blank Word Instance remains on the machine ... so apparently the Close is just closing the document

not sure if I need to use objword.application.close
but not wanting to close any documents already open

Any ideas?
 
msdn.com word application and document methods reveal:

objword.application.documents.count holds the value of number of documents open

and

objword.application.quit False will close the application without saving

so

if objword.application.documents.count > 1 then
objword.application.visible = True
objword.close False ' closes the print document
else
objword.application.quit false ' closes Word
endif
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top