process class to print word document and pass in printer

R

Rick

Can anyone tell me how to set the printer using the process class in VB.net,
I've tried everything, it always goes to the default printer.

My Code:

Dim pathToExecutable As String = "WINWORD.EXE"

Dim SPrinter = \\PrinterPath\PrinterName 'Name Of printer

Dim sReport = "C:\WordDoc1.doc" 'Complete name/path of PDF file

Dim starter As New ProcessStartInfo(pathToExecutable, "/t " + sReport + " "
+ SPrinter + "")

Try

Dim MyProcess As New Process

'MyProcess.StartInfo.Arguments = "/t " + sReport + " " + SPrinter + ""

MyProcess.StartInfo.CreateNoWindow = False

MyProcess.StartInfo.Verb = "print"

MyProcess.StartInfo.FileName = sReport

'MyProcess.StartInfo = starter

MyProcess.Start()

Dim iloop As Int16 = 0

If MyProcess.HasExited = False Then

'if not then loop for 100 time to try and close the process'with 10 seconds
delay

While Not MyProcess.HasExited

System.Threading.Thread.Sleep(10000)

MyProcess.CloseMainWindow()

iloop = CShort(iloop + 1)

If iloop >= 100 Then

Exit While

End If

End While

End If

MyProcess.WaitForExit(100000)

MyProcess.CloseMainWindow()

MyProcess.Close()

Thanks in advance,

Rick
 
R

Rick

I figured it out:
Dim objStartInfo As New ProcessStartInfo

Dim objProcess As New System.Diagnostics.Process

Dim sreport as string = "c:\reportname.doc"

Dim sPrinter as string \\printerpath\printname

Try

objProcess.StartInfo.CreateNoWindow = True

objProcess.StartInfo.UseShellExecute = True

objProcess.StartInfo.FileName = sreport

objProcess.StartInfo.Arguments = """" & sPrinter & """"

objProcess.StartInfo.Verb = "PrintTo"

objProcess.StartInfo.CreateNoWindow = True

objProcess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden

objProcess.Start()

Dim iloop As Int16 = 0

If objProcess.HasExited = False Then

'if not then loop for 100 time to try and close the process'with 10 seconds
delay

While Not objProcess.HasExited

System.Threading.Thread.Sleep(10000)

iloop = CShort(iloop + 1)

If iloop >= 100 Then

Exit While

End If

End While

End If

objProcess.Close()

objProcess.Dispose()

objProcess = Nothing

Catch ex As Exception

'MsgBox(ex.Message)

End Try
 

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