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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Error printing a word document 1

Status
Not open for further replies.

dongal

Technical User
Feb 18, 2002
10
GB
Hi All,

I am using the following code to print out a word document from a VB Program.
All machines tested on have the same setup (Windows 2000 and Office 2000) and administrator rights.
On some machines this works okay and on others the document gets opened an spooled to the printer, but never actually gets printed.
The vb program locks up, it can be ended using the task manager upon which the word document gets printed.

Set wrd = CreateObject("Word.Application")
wrd.Visible = False
Set doc = wrd.Documents.Open(Path & File & ".DOC")
doc.PrintOut
Do While wrd.BackgroundPrintingStatus <> 0
Loop
doc.Close
wrd.Quit
MsgBox &quot;Questionnaire has been sent to your Default Printer, &quot; & objPrinter.DeviceName, vbOKOnly, &quot;File Printed&quot;

Set doc = Nothing
Set wrd = Nothing

Has anybody else encountered this problem or have a solution?

regards

Dongal
 
Try putting a DoEvents in your do-while loop. It may not solve your printing problem, but at least you'll be able to provide a way to close your app without resorting to the Task Manager.

What happens if you take that loop out altogether? Does the .PrintOut method not block until the document has been spooled?
 
Hi,

Thanks for the reply, I will add the DoEvents and see what happens.

If the loop is removed then it appears that Word is closed to quickly and an error message appears, saying that closing Word will stop all printing and the option to click yes, no or cancel.
Adding the loop stops this error message.

Regards

Dongal
 
I've had similar behaviour. If you use

doc.PrintOut Copies:=1, Background:=False

and miss out the do while ... loop you should find the document being spat out of the printer.



 
Thanks Andy, you have saved me headbutting the monitor a few more times
 
Dongal,

I am looking for a function to do the same thing. Mind posting the entire function?

Thanks!
 
This is the complete function operated by a clicking a button on the screen. The program uses a dat file (bomb.dat)to store the name of the file and directory or you could just omit this and enter the name and path within the code.

All the best

Donald Lane


Private Sub PrintCmd_Click()
On Error GoTo Err_PrintCmd

Dim wrd As Word.Application
Dim doc As Word.Document
Dim Path As Variant
Dim File As Variant
Dim objPrinter As Printer

Set objPrinter = GetDefaultPrinter()
Open &quot;Bomb.dat&quot; For Input As #1
Input #1, Path
Input #1, File
Close #1

Set wrd = CreateObject(&quot;Word.Application&quot;)
wrd.Visible = False
Set doc = wrd.Documents.Open(Path & File & &quot;.DOC&quot;)
doc.PrintOut Copies:=1, Background:=False
'Do While wrd.BackgroundPrintingStatus <> 0
'Loop
doc.Close
wrd.Quit
MsgBox &quot;Questionnaire has been sent to your Default Printer, &quot; & objPrinter.DeviceName, vbOKOnly, &quot;File Printed&quot;

Set doc = Nothing
Set wrd = Nothing
Set objPrinter = Nothing

Err_PrintCmd:
Select Case Err.Number
Case Is = 0
Exit Sub
Case Is = 5174
MsgBox &quot;The Questionnaire cannot be found, please contact your System Administrator&quot;, vbCritical, &quot;Program Error&quot;
Exit Sub
Case Is = 53
MsgBox &quot;Please Enter a Path and File Name for the Questionnaire in the Options Menu&quot;, vbInformation, &quot;File Missing&quot;
Exit Sub
Case Is = 4198
Exit Sub
Case Is = 91
Exit Sub
Case Is = 5142
Exit Sub
Case Else
MsgBox Err.Number, vbOKOnly
Exit Sub
End Select
End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top