.NET Webbrowser control and default printer


In a vb.net application I have webrowser control that I use to print out reports to different printers.
Based on the URL I set the default printer for the specific report, and print the report.
The problem is that this is not working. The report always goes to the original default printer, even If I change the default.

This problem is also relevant for IE, Opera, Firefox etc
Changing the default printer via code, does not work.
The user has to manually select the printer they want to use for the current page/report, via the print dialog.

The PrintDocument object do print to the default printer, but this object has no simple support for printing HTML.

See link for demo project(vb.net), reproducing this problem.

Private Function SetDefaultSystemPrinter2(ByVal strPrinterName As String) As Boolean
        Dim moReturn As Management.ManagementObjectCollection
        Dim moSearch As Management.ManagementObjectSearcher
        Dim mo As Management.ManagementObject

        moSearch = New Management.ManagementObjectSearcher("Select * from Win32_Printer")
        moReturn = moSearch.Get

        For Each mo In moReturn
            Dim objReturn As Object
            If mo("Name").ToString.Trim.ToUpper = strPrinterName.Trim.ToUpper Then
                mo.InvokeMethod("SetDefaultPrinter", objReturn)
                Return True
            End If
        Return False
    End Function

Open in new window

Who is Participating?
LudvigXIVConnect With a Mentor Author Commented:
At last I found a solution to this, actually two solutions.

Via process.
This one require the print dialog, and the default printer is correct

      Dim Prostart As ProcessStartInfo = New ProcessStartInfo()
        Prostart.FileName = TempFile
        Prostart.Verb = "print"
        Prostart.CreateNoWindow = True
        Prostart.UseShellExecute = True
        Pro.StartInfo = Prostart
        Pro.EnableRaisingEvents = True

Via a new IE instance using this class

using System.Reflection;
using System.Threading;
using SHDocVw;

namespace HTMLPrinting
  public class HTMLPrinter
    private bool documentLoaded;
    private bool documentPrinted;

    private void ie_DocumentComplete(object pDisp, ref object URL)
      documentLoaded = true;

    private void ie_PrintTemplateTeardown(object pDisp)
      documentPrinted = true;

    public void Print(string htmlFilename)
      documentLoaded = false;
      documentPrinted = false;

      InternetExplorer ie = new InternetExplorerClass();
      ie.DocumentComplete += new DWebBrowserEvents2_DocumentCompleteEventHandler(ie_DocumentComplete);
      ie.PrintTemplateTeardown += new DWebBrowserEvents2_PrintTemplateTeardownEventHandler(ie_PrintTemplateTeardown);

      object missing = Missing.Value;

      ie.Navigate(htmlFilename, ref missing, ref missing, ref missing, ref missing);
      while (!documentLoaded && ie.QueryStatusWB(OLECMDID.OLECMDID_PRINT) != OLECMDF.OLECMDF_ENABLED)

      while (!documentPrinted)

      ie.DocumentComplete -= ie_DocumentComplete;
      ie.PrintTemplateTeardown -= ie_PrintTemplateTeardown;

Éric MoreauSenior .Net ConsultantCommented:
LudvigXIVAuthor Commented:

The code is from his sample. I have just added a webbrowser control to his project to illustrate the problem. There are 3 different functions to set the default. They all do set the default browser, but the webbrowser control ignores this.  So does  IE, Opera, Firefox etc.
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

Éric MoreauSenior .Net ConsultantCommented:
Have you tried the 3 methods? Do you see your default printer changing in the Control Panel?
LudvigXIVAuthor Commented:
Yes, I have tested all 3.
The default printer changes in the control panel.
But the print goes to the original default.
In the added project you can try this. Change the default, print the Google page, and try another printer, print again. Same result. The print goes to the original default.
Éric MoreauSenior .Net ConsultantCommented:
surely because the browser is not a good citizen of Windows and does not listen to the changed printer event.

could you set the default printer before loading the form with webbrowser?
LudvigXIVAuthor Commented:
Setting the default, before loading the form works. But in my project where the goal is to alternate between printers without use of the print dialog, this is not a solution.
I have searching the web for days for a solution for this task. Now I’m stucked.
Éric MoreauSenior .Net ConsultantCommented:
so you just confirmed that it is a limitation of the browser!
LudvigXIVAuthor Commented:
All I say is that I’m out of options.
Setting the default printer is not working in a running application.
As you said, maybe this has no solution.
Éric MoreauSenior .Net ConsultantCommented:
There is definitely a problem with IE (and the WebBrowser will have the same problem because it is in fact IE).

I just made this test:
-open IE
-press CTRL-P
-note the default printer
-changed the default printer in the control panel
-returned to the same IE instance
-press CTRL-P again
-it is still the same selected printer

I really doubt you will be able to change this behavior
All Courses

From novice to tech pro — start learning today.