Vs.net Print file

gooden

Active member
Joined
May 18, 2007
Messages
32
Programming Experience
Beginner
WEll i have a problem in printing files (always have problems with this :| )

well my problem is that im trying to print a file (in this case a pdf)

so the real problem is the code i have:

VB.NET:
        Dim MyProcess As New Process
        MyProcess.StartInfo.CreateNoWindow = False
        MyProcess.StartInfo.Verb = "print"
        MyProcess.StartInfo.FileName = "C:\Invoice.pdf"
        MyProcess.Start()
        MyProcess.CloseMainWindow()
        MyProcess.Close()


well in first place this code works :) but i dont know how to select the printer name :S because i have 2 printers.... and in 1 button is going to print with the 1º printer in the second is going to print with other printer :|

i tryied to search comands like:
myprocess.startinfo.printername="Epson"

but that comand is just imaginary x)


Glad if someone could Help
Gooden
 
Thread moved to Printing forum, VS forum is for topics regarding Visual Studio itself.

You have to set default printer before calling that print. WMI Win32_Printer can do that by executing the SetDefaultPrinter method. First you have to get all printers by query for Name and Default property, this also give you the information to reset back the default printer prior to your change. Play with WMI Code Creator, it will give you most of the code needed.
 
i tryed to do that but dont worked :(

the code im using is this ....

VB.NET:
    Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
        Dim MyProcess As New Process
        Dim strOldPrinter As String

        Dim WshNetwork As Object

        Dim pd As New PrintDocument
        Dim strprinterName = "Epson"


        Try

            strOldPrinter = pd.PrinterSettings.PrinterName

            WshNetwork = Microsoft.VisualBasic.CreateObject("WScript.Network")

            WshNetwork.SetDefaultPrinter(strprinterName)

            pd.PrinterSettings.PrinterName = strprinterName

            If pd.PrinterSettings.IsValid Then

                Return

            Else

                WshNetwork.SetDefaultPrinter(strOldPrinter)

                Return

            End If

        Catch exptd As Exception

            WshNetwork.SetDefaultPrinter(strOldPrinter)

            Return

        Finally

            WshNetwork = Nothing

            pd = Nothing

        End Try
        MyProcess.StartInfo.CreateNoWindow = False
        MyProcess.StartInfo.Verb = "print"
        MyProcess.StartInfo.FileName = "C:\Invoice.pdf"
        MyProcess.Start()
        MyProcess.CloseMainWindow()
        MyProcess.Close()
    End Sub

dont print _:(
 
WSH script object can be used also, yes, I've used that myself and it works. But why do you stop the Sub method if printer name IsValid? 'Return' statement will end the Sub (ie equivalent to 'Exit Sub').
 
Yes, do as I said in post 4, remove the 'Return' when printer is valid, it will make the code continue after the Try block (well Finally actually, but what's there doesn't matter). I haven't test-run it, but the rest look allright, bit messy logic but doesn't look faulty.
 
Back
Top