wscript.sleep

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:pwd")
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:printerIPAddress").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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top