Mirror

Print url/html file using IE browser (Views: 101)


Problem/Question/Abstract:

How can I print url/html file using IE browser?

Answer:

Solve 1:

I want to show how you can activate printing of any url and/or html file using installed IE.

I solved this task yesterday and solution is very useful and have a small size:-)

uses ComObj;

procedure PrintHTMLByIE(const url: string);
const
  OLECMDID_PRINT = $00000006;
  OLECMDEXECOPT_DONTPROMPTUSER = $00000002;
var
  ie, vaIn, vaOut: Variant;
begin
  ie := CreateOleObject('InternetExplorer.Application');
  ie.Navigate(url);
  ie.Visible := True;
  ie.ExecWB(OLECMDID_PRINT, OLECMDEXECOPT_DONTPROMPTUSER, vaIn, vaOut);
end;

Sample:

PrintHTMLByIE('file:\\c:\misha\webpage\index.htm');

or

PrintHTMLByIE('http:\\www.scalabium.com\sme\index.htm');

I get "Trying to revoke drop target that has not been registered" error all the time. The only way the function works is if you step over the code with F8. Any ideas why?

there is a bugfix for that on the community site !

just do this...

const
  OLECMDF_SUPPORTED = 1;
  OLECMDF_ENABLED = 2;
var
  ie, vaIn, vaOut: Variant;
begin
  ie := CreateOleObject('InternetExplorer.Application');
  ie.Navigate(url);
  ie.Visible := True;
  while ie.QueryStatusWB(OLECMDID_PRINT) <> OLECMDF_SUPPORTED + OLECMDF_ENABLED do
    Forms.Application.ProcessMessages;
  sleep(2000);
  ie.ExecWB(OLECMDID_PRINT, OLECMDEXECOPT_DONTPROMPTUSER, vaIn, vaOut);
end;


Solve 2:

I had to use OleVariant for the type (instead of Variant) but I'm using the TWebBrowser control like this:

procedure TForm1.btnPrintClick(Sender: TObject);
const
  OLECMDID_PRINT = 6;
  OLECMDEXECOPT_DONTPROMPTUSER = 2;
var
  vaIn, vaOut: OleVariant;
begin
  WebBrowser1.ExecWB(OLECMDID_PRINT, OLECMDEXECOPT_DONTPROMPTUSER, vaIn, vaOut);
end;

<< Back to main page