שתף באמצעות


Programmatically change a user's default printer

Question

Monday, July 26, 2010 5:30 PM

I have a VB.NET module that prints out a series of Crystal and PDF documents as a packet.  I need a way to temporarily change the user's default printer to the network printer for the PDF report (the Crystal reports are already redirected).  Once the module has done its job, I want the module to return the default printer back to its original setting.  Since Adobe Reader only works with the default printer, and there does not appear to be a way to redirect that, I need to change the deault for the duration of the module.  I tried the following code, but it still sends the PDF output to the local printer.

Dim strFile As String = My.Resources.JobDocuments & _
    lblJobDoc.Text & ".pdf"
Dim psi As New ProcessStartInfo("AcroRd32.exe", _
    "/t " + strFile + " " + lblPrinter.Text + "")
report.StartInfo = psi
report.Start()
report.WaitForInputIdle()

I am assuming I will have to do a Windows API call to accomplish this, but all my searches have been fruitless.  Any help you all can provide would be greatly appreciated!  Thanks!!

 

LDC

All replies (6)

Friday, July 30, 2010 12:21 PM ✅Answered

OK, what I was missing was the SIMPLE solution -- using the System.Drawing class:

  Private Function DefaultPrinterName() As String
    Dim oPS As New System.Drawing.Printing.PrinterSettings
    Try
      DefaultPrinterName = oPS.PrinterName
    Catch ex As Exception
      MessageBox.Show(ex.Message, "Capturing Default Printer", MessageBoxButtons.OK)
    Finally
      oPS = Nothing
    End Try
  End Function

With this piece in place, the module operates as I need it to. Thanks for the lead on the SetDefaultPrinter function!

LDC


Monday, July 26, 2010 5:32 PM

Just as a follow-up, the entry for lblPrinter.Text contains the fully pathed reference to the network printer.LDC


Thursday, July 29, 2010 6:40 AM

Hi,

You need to pinvoke the SetDefaultPrinter fuction which have been defined in winspool.drv.

[DllImport("winspool.drv", CharSet=CharSet.Auto, SetLastError=true)]
    public static extern bool SetDefaultPrinter(string Name);

Check these links out:

http://msdn.microsoft.com/en-us/library/dd162971.aspx

http://www.pinvoke.net/default.aspx/winspool/setdefaultprinter.html?diff=y

http://www.codeguru.com/cpp/w-p/printing/article.php/c2951

Thanks

Binze

Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.


Friday, July 30, 2010 11:33 AM

The issue remains; it has not yet been answered.  I checked the three references you provided, and while the SetDefaultPrinter function appears to make sense, I still need the module to first identify the user's default printer, the switch over to the designated network printer, and when the routine is complete, restore the user's default.  Here are the declarations I placed at the top of the module:

  Declare Function SetDefaultPrinter Lib "winspool.drv" Alias "SetDefaultPrinterA" (ByVal pszPrinter As String) As Boolean
  Declare Function GetDefaultPrinter Lib "winspool.drv" Alias "GetDefaultPrinterA" (ByVal pszBuffer() As String, ByVal pcchBuffer As Integer) As Boolean

When I test the GetDefaultPrinter function, it always reports back False.

  Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim myBuffer() As String
    Dim mySize As Integer
    Dim retVal As Boolean = GetDefaultPrinter(myBuffer, mySize)
    Stop
  End Sub

What am I missing here?

 

LDC


Thursday, July 26, 2018 4:03 AM

Hi, where should I write name of printer which I want to set as default? My code 

Private Function DefaultPrinterName1(sender As Object, e As EventArgs)
 
       Dim oPS As New System.Drawing.Printing.PrinterSettings
       Try
           DefaultPrinterName1 = oPS.PrinterName = "Vario III 107/12 (kopie 1)"
       Catch ex As Exception
           MessageBox.Show(ex.Message, "inf", MessageBoxButtons.OK)
       Finally
           oPS = Nothing
       End Try
   End Function

Thursday, July 26, 2018 1:31 PM

I am assuming you have declared the SetDefaultPrinter at the top of the program?

    Declare Function SetDefaultPrinter Lib "winspool.drv" Alias "SetDefaultPrinterA" (ByVal pszPrinter As String) As Boolean

Once that is set, you simply have to call the function and provide the valid printer name...

Dim xVal As Boolean = SetDefaultPrinter({printer name here})

HTH

LDC