Click here to Skip to main content
12,951,289 members (49,364 online)
Rate this:
Please Sign up or sign in to vote.
See more:
I'm trying to bulk print HTML reports to my default printer which is PDF Creator setup for auto saving. I have the HTML files loading up through Internet Explorer and from there I shall print without user prompt.

The problem I'm having is that when my program loops through to print the list of HTML files it has found that some of the documents do not spool and do not get printed. I did read on the net that this is can be solved with using a while loop and a Application.Dowork(). When I implemented this occasionally all of my documents would print which was an improvement however this was only occasionally and not a sure fix.

Could my problem be that the each thread is closing before it has finished it processing?

If so how could I get the threads to run independently so they do not close while they are still processing?

Below is the code I'm using to print the documents to the default printer:

foreach (var x in fileList)
                        // Printing files through IE on default printer.
                        Console.WriteLine("{0}", x);
                        SHDocVw.InternetExplorer IE = new SHDocVw.InternetExplorer();
                        IE.DocumentComplete += new SHDocVw.DWebBrowserEvents2_DocumentCompleteEventHandler(IE_DocumentComplete);
                        IE.PrintTemplateTeardown += new SHDocVw.DWebBrowserEvents2_PrintTemplateTeardownEventHandler(IE_PrintTemplateTeardown);
                        IE.Visible = true;
                        while (IE.ReadyState != SHDocVw.tagREADYSTATE.READYSTATE_COMPLETE)
        static void IE_PrintTemplateTeardown(object pDisp)
            if (pDisp is SHDocVw.InternetExplorer)
                SHDocVw.InternetExplorer IE = (SHDocVw.InternetExplorer)pDisp;
        static void IE_DocumentComplete(object pDisp, ref object URL)
            if (pDisp is SHDocVw.InternetExplorer)
                SHDocVw.InternetExplorer IE = (SHDocVw.InternetExplorer)pDisp;
Posted 18-Dec-12 4:54am
Updated 19-Dec-12 1:58am
Adam R Harris 19-Dec-12 18:25pm
Have you tried pausing your thread to allow IE to send the page to the printer?
maybe try pausing your thread for a few milliseconds after you send it to the printer to allow it the time to spool it and all that fun stuff.
bikerben 20-Dec-12 5:45am
Hi Adam, Yes I've tried popping a thread.sleep in there using various lengths from millliseconds to a few seconds. I've currently changed the code so the IE_PrintTemplateTeardown method is not called, instead once the application has finsihed processing all the files it will then close all the instances of IE which are open in the background. Its dirty I know but for the time been it will do since the application has the desired effect. I'll come back to this issue at a later date with a fresh pair of eyes.
Adam R Harris 20-Dec-12 10:42am
Glad to hear you at least got it working. Like you said it might not be pretty but it functions and i'm sure that's all the client cares about. If you do end up finding a better solution i would be interested in knowing what it was so please dig up the old thread and give us an update if and when you get around to cleaning it up.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

    Print Answers RSS
Top Experts
Last 24hrsThis month
OriginalGriff 6,084
CHill60 3,480
Maciej Los 3,083
Jochen Arndt 1,975
ppolymorphe 1,830

Advertise | Privacy | Mobile
Web02 | 2.8.170525.1 | Last Updated 19 Dec 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100