Reputation: 1516
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?
Upvotes: 2
Views: 4036
Reputation: 1516
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.
Upvotes: 1
Reputation: 10411
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
Upvotes: 1