IE Printing HTML file through ExecWB - print dialog comming up behindmy app - help!

A

Andrew Meador

I have implemented a printing scenario where an html file is
printed using the the following code:

public void PrintHtmlFile(string url)
{
RegistryKey IERegKey;
string header = null;
string footer = null;
object o = null;
InternetExplorerClass ie = null;
IWebBrowser wb = null;

// Get registry key for IE PageSetup and current values
for header and footer. If IE hasn't been
// through a print cycle, the PageSetup key will not
exist. Best thing to do is to run IE through
// at least one print job so the key will be set. With the
code below, the print will work, but the
// first printout will have headers and footers based on
default setting created on first print cycle.
// Once the registry key is in place - the following will
effectively hide the header and footer on
// printouts.
IERegKey = Registry.CurrentUser.OpenSubKey("software\
\Microsoft\\Internet Explorer\\PageSetup", true);
if (IERegKey != null)
{
footer = IERegKey.GetValue("footer", 0).ToString();
header = IERegKey.GetValue("header", 0).ToString();

// Set header and foot to blank
IERegKey.SetValue("footer", "");
IERegKey.SetValue("header", "");
}

// Create Internet Explorer instance - this (or the wb
object below) will fail if IE has not been through
// it's initial first run sequence.
ie = new InternetExplorerClass();

// Create web browser page - this (or the ie object above)
will fail if IE has not been through
// it's initial first run sequence.
wb = (IWebBrowserApp)ie;

//ie.Visible = true; // Shows the IE instance.

wb.Navigate(url, ref o, ref o, ref o, ref o);

// Wait for IE object to be ready - otherwise intermittant
errors occur when print is initiated.
while (ie.ReadyState !=
SHDocVw.tagREADYSTATE.READYSTATE_COMPLETE)
{
Application.DoEvents();
}
// Print the page - prompt user
ie.ExecWB(SHDocVw.OLECMDID.OLECMDID_PRINT,
OLECMDEXECOPT.OLECMDEXECOPT_PROMPTUSER, ref o, ref o);

if (IERegKey != null)
{
// Replace IE Page settings
IERegKey.SetValue("footer", footer);
IERegKey.SetValue("header", header);
}

This works pretty well. Anyway, when the user clicks the 'Print'
button on the application form, this sub is called with the full path
to the file passed via the URL parameter. The user then has a Print
Dialog box pop up letting them select the printer to print to. All
they have to do it click ok/print and it prints out. The problem is
that the print dialog box is coming up behind the application. How can
I make the dialog box come up on top of the application?

Thanks in advance!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top