J
Jason Buckner
We have some code processing on_click for a button. Part of the code
connects to a network printer, sets it to default printer, prints the form,
disconnects the printer, and creates a msgbox for theuser to close the form.
The issue we are having is that sometimes the code completes prior to the job
actually printing, so the job is deleted ffrom the print server. We thought
of tossing in something like a wscript.sleep, but it bombs out.
The code is below but I I was just wondering if there was a method to ensure
the job printed successfully before closing the form.
Sub btnSubmit_OnClick(eventObj)
'set form to read only
Dim readonlytxt,usrtxt,pwdtxt
Set readonlytxt = XDocument.DOM.selectSingleNode("//my:readonly")
readonlytxt.Text = "true"
'remove username and password
Set usrtxt = XDocument.DOM.selectSingleNode("//my:username")
usrtxt.Text = "******"
Set pwdtxt = XDocument.DOM.selectSingleNode("//my
wd")
pwdtxt.Text = "******"
'submit the form
XDocument.Submit()
'print the form
' Get current default printer name
Set objWMIService = GetObject(
"winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
Set colInstalledPrinters = objWMIService.ExecQuery ("Select * from
Win32_Printer")
strDefault = ""
For Each objPrinter in colInstalledPrinters
If objPrinter.Default = "True" Then
strDefault = objPrinter.Name
End If
Next
Dim net
Set net = CreateObject("WScript.Network")
PrinterPath = XDocument.DOM.selectSingleNode("//my
rinterIPAddress").text
net.AddWindowsPrinterConnection PrinterPath
net.SetDefaultPrinter PrinterPath
XDocument.PrintOut()
net.RemovePrinterConnection PrinterPath
net.SetDefaultPrinter strDefault
'Pop-up dialogue to allow print job to finish.
varSubmitDiag = MsgBox ("The form was printed to: " & PrinterPath & " Click
OK to close the form.",vbOKOnly,"Close the form?")
'close infopath without saving current form
Application.Quit(true)
End Sub
connects to a network printer, sets it to default printer, prints the form,
disconnects the printer, and creates a msgbox for theuser to close the form.
The issue we are having is that sometimes the code completes prior to the job
actually printing, so the job is deleted ffrom the print server. We thought
of tossing in something like a wscript.sleep, but it bombs out.
The code is below but I I was just wondering if there was a method to ensure
the job printed successfully before closing the form.
Sub btnSubmit_OnClick(eventObj)
'set form to read only
Dim readonlytxt,usrtxt,pwdtxt
Set readonlytxt = XDocument.DOM.selectSingleNode("//my:readonly")
readonlytxt.Text = "true"
'remove username and password
Set usrtxt = XDocument.DOM.selectSingleNode("//my:username")
usrtxt.Text = "******"
Set pwdtxt = XDocument.DOM.selectSingleNode("//my
pwdtxt.Text = "******"
'submit the form
XDocument.Submit()
'print the form
' Get current default printer name
Set objWMIService = GetObject(
"winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
Set colInstalledPrinters = objWMIService.ExecQuery ("Select * from
Win32_Printer")
strDefault = ""
For Each objPrinter in colInstalledPrinters
If objPrinter.Default = "True" Then
strDefault = objPrinter.Name
End If
Next
Dim net
Set net = CreateObject("WScript.Network")
PrinterPath = XDocument.DOM.selectSingleNode("//my
net.AddWindowsPrinterConnection PrinterPath
net.SetDefaultPrinter PrinterPath
XDocument.PrintOut()
net.RemovePrinterConnection PrinterPath
net.SetDefaultPrinter strDefault
'Pop-up dialogue to allow print job to finish.
varSubmitDiag = MsgBox ("The form was printed to: " & PrinterPath & " Click
OK to close the form.",vbOKOnly,"Close the form?")
'close infopath without saving current form
Application.Quit(true)
End Sub