Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
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;
                        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 18-Dec-12 4:54am
Edited 19-Dec-12 1:58am
v2
Comments
Adam R Harris at 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 at 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 at 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
0 CPallini 330
1 Sergey Alexandrovich Kryukov 313
2 George Jonsson 291
3 Prasad Avunoori 155
4 OriginalGriff 149
0 OriginalGriff 4,623
1 CPallini 3,410
2 Sergey Alexandrovich Kryukov 2,929
3 George Jonsson 2,319
4 Gihan Liyanage 2,077


Advertise | Privacy | Mobile
Web02 | 2.8.140905.1 | Last Updated 19 Dec 2012
Copyright © CodeProject, 1999-2014
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