views:

949

answers:

2

I've got a WebBrowser control on a WinForms app that is loading XML which is converted to HTML/CSS. Looks beautiful if I just want to view it there or in a regular browser.

When the form loads, it's supposed to navigate to the file, then when the OnDocumentCompleted event fires, it should set the header, footer, and default printer name in the registry, then call Print() for however many copies the user has specified.

I've run it through the debugger, and it's calling webBrowser.Print() the correct number of times in the OnDocumentCompleted event. Nothing is null that shouldn't be, Visible is true on the WebBrowser control, as is AllowNavigation. And yes, the printer is turned on and reachable over the network. The exact same code works elsewhere in the project, but not here.

What else could be causing this hellish control to ignore my Print commands?

Ideally, I'd like the whole thing to be hidden off-screen, as this is meant to run using Windows Scheduler, and the users should never see it. I've read that the control needs to be visible, though, so until I work out this first kink, that can wait.

EDIT: The last two lines of my OnDocumentCompleted event handler set the DialogResult to OK and closed the form. Removing the call to Close() let it print, so I'm guessing it didn't get to the print spooler before the form was closed and the WebBrowser control was disposed.

Short of just setting an arbitrary time limit to wait before closing, is there any way to tell when it is done printing?

+2  A: 

Try this:

mshtml.IHTMLDocument2 doc;
doc = oWeb.Document;        
doc.execCommand("Print", True, Nothing);

FROM EDIT:

It is possible to use SHELL commands to check the status of the jobs in the print queue

Stephen Wrighton
Can't convert webBrowser.Document (HTMLDocument) to any of the IHTMLDocument* interfaces. HTMLDocument does have it's own ExecCommand, which I called using your suggestion. Whether I set showUI to true or false, nothing happens.
Chris Doggett
you may have to explicitly tell it to convert the Document object to an IHTMLDocument2 object using "(IHTMLDocument2)oWeb.Document;" Also, ensure that you have the MSHTML dll as a reference in your project.Regardless, this is how I'm printing the contents of the browser control.
Stephen Wrighton
+2  A: 

Turned out to be a timing issue, with the form being closed before it could send the document to the print spooler. I wound up adding a Timer to the form, setting it for 30 seconds, and having it's Tick() event close the form. Where it was closing the form previously, it now just calls closeTimer.Start().

Chris Doggett