Hi,
I am using MS WORD to create reports through VB. The report is about 15 pages long. When I run my print function (see code below)I get a message box that informs me that MS Word is still sending the document to the printer spooler. It then asks if I wish to continue. If I select Yes then the print job will be canceled. If I select No then the document is printed however word does not close, so when I go to print a second report the computer tells me that MS word is currently in use. What I need to know is how to print reports without having this message box appear and at the same time having MS Word close once the report has been printed.
MY CODE IS AS FOLLOWS:
Public Sub PrintReport(TempMrn As String)
On Error GoTo ErrorHandler
Set ObjWord = GetObject(, "Word.Application"
ObjWord.Documents.Open (WordTemplateDocumentPath)
ObjWord.ActiveDocument.SaveAs (WordSaveLocation + FileName)
Dim Ans As String
Ans = MsgBox("Would you like to print a copy of this report?", vbInformation + vbYesNo, "Report"
If Ans = vbYes Then
ObjWord.ActiveDocument.PrintOut
MsgBox "Report has been sent to the printer for " & TempFname & " " & TempLname & "." + vbCrLf + vbCrLf + "A copy of the report has been saved as " & FileName & ".", vbInformation + vbOKOnly, "MOSES - Report"
Else
MsgBox "Report has been generated for " & TempFname & " " & TempLname & "." + vbCrLf + vbCrLf + "A copy of the report has been saved as " & FileName & ".", vbInformation + vbOKOnly, "MOSES - Report"
End If
ObjWord.ActiveWindow.Close wdDoNotSaveChanges
ObjWord.Quit
ErrorHandlerExit:
ResultsPrintReport.Enabled = True
Exit Sub
ErrorHandler:
If Err = 429 Then
Set ObjWord = CreateObject("Word.Application"
Resume Next
Else
'MsgBox "Error No: " & Err.Number & "; Description: " & Err.Description
Resume Next 'ErrorHandlerExit
End If
End Sub
I am using MS WORD to create reports through VB. The report is about 15 pages long. When I run my print function (see code below)I get a message box that informs me that MS Word is still sending the document to the printer spooler. It then asks if I wish to continue. If I select Yes then the print job will be canceled. If I select No then the document is printed however word does not close, so when I go to print a second report the computer tells me that MS word is currently in use. What I need to know is how to print reports without having this message box appear and at the same time having MS Word close once the report has been printed.
MY CODE IS AS FOLLOWS:
Public Sub PrintReport(TempMrn As String)
On Error GoTo ErrorHandler
Set ObjWord = GetObject(, "Word.Application"
ObjWord.Documents.Open (WordTemplateDocumentPath)
ObjWord.ActiveDocument.SaveAs (WordSaveLocation + FileName)
Dim Ans As String
Ans = MsgBox("Would you like to print a copy of this report?", vbInformation + vbYesNo, "Report"
If Ans = vbYes Then
ObjWord.ActiveDocument.PrintOut
MsgBox "Report has been sent to the printer for " & TempFname & " " & TempLname & "." + vbCrLf + vbCrLf + "A copy of the report has been saved as " & FileName & ".", vbInformation + vbOKOnly, "MOSES - Report"
Else
MsgBox "Report has been generated for " & TempFname & " " & TempLname & "." + vbCrLf + vbCrLf + "A copy of the report has been saved as " & FileName & ".", vbInformation + vbOKOnly, "MOSES - Report"
End If
ObjWord.ActiveWindow.Close wdDoNotSaveChanges
ObjWord.Quit
ErrorHandlerExit:
ResultsPrintReport.Enabled = True
Exit Sub
ErrorHandler:
If Err = 429 Then
Set ObjWord = CreateObject("Word.Application"
Resume Next
Else
'MsgBox "Error No: " & Err.Number & "; Description: " & Err.Description
Resume Next 'ErrorHandlerExit
End If
End Sub