2

I'm using the MS Web Browser ActiveX control in a native C++/Win32/MFC dialog application to render some information via HTML.

I'd like to print the contents without any user interaction to the specified printer (which is not the default printer). The printer I'd like to use is a PDF printer.

I'm using a nice wrapper class from Code Project which makes the using the Web Browser control a little easier (http://www.codeproject.com/Articles/3919/Using-the-WebBrowser-control-simplified) and one of the things that wrapper provides is printing. It provides a print method which uses ExecWB(OLECMDID_PRINT,OLECMDEXECOPT_DONTPROMPTUSER,...) to initiate printing of the contents.

This works great and results in the contents of the control being printed to the default printer without any user interaction.

The challenge, then is to have it use a different printer than the default. My initial attempt was to call ::SetDefaultPrinter (http://msdn.microsoft.com/en-us/library/windows/desktop/dd162971(v=vs.85).aspx). This doesn't seem to work. It seems that, despite calling ::SetDefaultPrinter, the web browser ActiveX control still prints to the system default printer. Its as though it ignores whatever this call does.

I tried broadcasting the system settings change message as suggested in the MSDN link as well as directly sending it to the browser window without any luck.

Any ideas how I can get the web browser activex control to print the printer I specify instead of using the default printer?

Nerdtron
  • 1,486
  • 19
  • 32

2 Answers2

1

What I do in addition to all your steps is set the printer in the IE registry, here:

HKCU\Software\Microsoft\Internet Explorer\PageSetup\printer="the required printer name"

And restore everything back after the printing

cha
  • 10,301
  • 1
  • 18
  • 26
  • Update: Looks like that didn't do it. I had, the previous day before leaving, manually set the PDF printer as the default. I forgot to set it back and tried your suggestion and it worked. But only because the PDF printer was manually set as the system default. When I switched the default back to my physical printer, the above did not help. Even with setting the registry key prior to the print, the browser control still prints the document to the system default printer. – Nerdtron Mar 06 '13 at 20:10
  • I got it working; the registry key thing is not necessary nor is it sufficient from my testing. I'll add the solution which worked for me. – Nerdtron Mar 06 '13 at 21:06
  • Its possible the registry thing is required in some contexts; not sure, but for me it didn't seem to matter either way. Even with the registry key thing I still had to set the default printer first, and even without the registry thing it still worked, once I figured out the problem. – Nerdtron Mar 06 '13 at 21:09
1

I got it working. The reason for the problem was NOT that the browser control was printing to the wrong printer, it was that the printing operation (via ExecWB) is performed asynchronously. So, I was setting the default printer to the PDF printer, then initiating the print, then restoring the original default printer. This happened quickly enough that by the time the asynchronous print operation was ready the original default printer was set as the default again and so it went to that printer.

The solution was set the default printer to the PDF printer, initiate the print, then wait for the print completion callback. Once that callback is received, it then restores the original default printer.

If you're using Gary Wheeler's excellent web browser control on Code Project (see here), its very easy to get the print completed callback, you just override the virtual OnPrintTemplateTeardown() method.

Nerdtron
  • 1,486
  • 19
  • 32