.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?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

É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.
Éric MoreauSenior .Net ConsultantCommented:
Have you tried the 3 methods? Do you see your default printer changing in the Control Panel?
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

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
LudvigXIVAuthor 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;


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Development

From novice to tech pro — start learning today.