Click here to Skip to main content
15,867,939 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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:


C#
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;
                        IE.Navigate2(x);
                        while (IE.ReadyState != SHDocVw.tagREADYSTATE.READYSTATE_COMPLETE)
                        {
                            System.Windows.Forms.Application.DoEvents();
                        }

                    }
                }
            }
        }

        static void IE_PrintTemplateTeardown(object pDisp)
        {
            if (pDisp is SHDocVw.InternetExplorer)
            {
                SHDocVw.InternetExplorer IE = (SHDocVw.InternetExplorer)pDisp;
                IE.Quit();
                System.Environment.Exit(0);
            }
        }

        static void IE_DocumentComplete(object pDisp, ref object URL)
        {
            if (pDisp is SHDocVw.InternetExplorer)
            {
                SHDocVw.InternetExplorer IE = (SHDocVw.InternetExplorer)pDisp;
                 IE.ExecWB(SHDocVw.OLECMDID.OLECMDID_PRINT, SHDocVw.OLECMDEXECOPT.OLECMDEXECOPT_DONTPROMPTUSER, 2);
            }
        }
Posted
Updated 19-Dec-12 1:58am
v2
Comments
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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900