Click here to Skip to main content
15,891,431 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
void SendDocToPrinterSilently(string htmlfilename)
       {
           try
           {
               documentLoaded = false;
               documentPrinted = false;

               SHDocVw.InternetExplorer ie = new InternetExplorer();
               ie.Visible = false;
               ie.DocumentComplete += new DWebBrowserEvents2_DocumentCompleteEventHandler(ie_DocumentComplete);
               ie.PrintTemplateTeardown += new DWebBrowserEvents2_PrintTemplateTeardownEventHandler(ie_PrintTemplateTeardown);
               object missing = Missing.Value;

               ie.Navigate(htmlfilename, ref missing, ref missing, ref missing, ref missing);
               while (!documentLoaded && ie.QueryStatusWB(OLECMDID.OLECMDID_PRINT) != OLECMDF.OLECMDF_ENABLED)
                   Thread.Sleep(100);


               ie.ExecWB(OLECMDID.OLECMDID_PRINT, OLECMDEXECOPT.OLECMDEXECOPT_DONTPROMPTUSER, ref missing, ref missing);
               while (!documentPrinted)
                   Thread.Sleep(100);

               ie.DocumentComplete -= ie_DocumentComplete;
               ie.PrintTemplateTeardown -= ie_PrintTemplateTeardown;
               ie.Quit();


               //SHDocVw.InternetExplorer IE = new SHDocVw.InternetExplorer();
               //IE.Visible = false;
               //IE.Silent = true;
               //IE.DocumentComplete += new SHDocVw.DWebBrowserEvents2_DocumentCompleteEventHandler(IE_DocumentComplete);
               //IE.PrintTemplateTeardown += new SHDocVw.DWebBrowserEvents2_PrintTemplateTeardownEventHandler(IE_PrintTemplateTeardown);

               //IE.Navigate(htmlfilename);

           }
           catch (Exception ex)
           {
               throw ex;
           }

       }

private void ie_DocumentComplete(object pDisp, ref object URL)
       {
           documentLoaded = true;
       }

       private void ie_PrintTemplateTeardown(object pDisp)
       {
           documentPrinted = true;
       }


I am using above code to print HTML files silently. It works like a charm when the user is an administrator(belongs to administrator group). For other user it is showing up the document in IE browser besides printing. Could anyone can help me with this. Struggling form a long time.
Thanks a lot well in advance.
Posted
Comments
NightWizzard 27-Jan-16 13:10pm    
No idea why IE is shown only for non-admins but how about moving the IE window out of the display area of the screen instead of setting visible property to false?
VipinKumar Maurya 28-Jan-16 3:07am    
I did not get you properly but how will I print the document then?
NightWizzard 28-Jan-16 3:13am    
Same way as before - print command is sent by code. The only difference is that the IE window is at a location outside the screen's display area (and therefore invisible to the user).
VipinKumar Maurya 3-Feb-16 0:21am    
I dont want to just hide it but also print the document. The problem I am facing is that IE shows up and the user is forced to manually print the document and then close the IE.

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