I am using the following unit to display - and print - HTML code via a TWebBrowser which is displayed in a non-modal dialog. In my production program, the following code works under Windows-XP but fails with Windows-7 (the error message is always External exception C015D00F). In order to isolate the problem, I wrote a simple test program which also has a non-modal dialog containing a TWebBrowser; on its own, this test program works correctly with Windows-7, but when I plug the non-modal dialog from the test program into the production program, I get the external exception.
This presumably indicates that there is a problem with the calling program and not the called unit, but I can't see what that problem is. The HTML code is hand crafted but displays correctly.
What could be the problem? The printing code comes from the Embarcadero site
unit Test4;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, OleCtrls, SHDocVw, MSHTML;
type
THTMLPreview = class(TForm)
web: TWebBrowser;
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure webDocumentComplete(Sender: TObject; const pDisp: IDispatch;
var URL: OleVariant);
private
options: word;
fn: string;
procedure DoPrint;
public
Constructor Create (const afn, acapt: string; opts: word);
end;
implementation
{$R *.dfm}
constructor THTMLPreview.Create (const afn, acapt: string; opts: word);
begin
inherited create (nil);
caption:= acapt;
fn:= afn;
options:= opts;
web.Navigate (fn);
end;
procedure THTMLPreview.webDocumentComplete(Sender: TObject;
const pDisp: IDispatch; var URL: OleVariant);
begin
DoPrint
end;
procedure THTMLPreview.DoPrint;
var
HTMLDoc: IHTMLDocument2;
HTMLWnd: IHTMLWindow2;
HTMLWindow3: IHTMLWindow3;
begin
if options and 4 = 4 then
begin
HTMLDoc:= web.Document as IHTMLDocument2;
if HTMLDoc <> nil then
begin
HTMLWnd:= HTMLDoc.parentWindow;
HTMLWindow3:= HTMLWnd as IHTMLWindow3;
HTMLWindow3.print;
end
end
end;
procedure THTMLPreview.FormClose(Sender: TObject; var Action: TCloseAction);
begin
if options and 1 = 1 then deletefile (fn);
action:= caFree
end;
end.
Using the statement Web.ControlInterface.ExecWB (OLECMDID_PRINT, OLECMDEXECOPT_PROMPTUSER,
vaIn, vaOut)
gives the same error.
Edit from a few days later:
I tried a completely different approach to the problem. In the HTML code I added a javascript snippet which displays a 'print' button and adds an 'onprint' event. Once again, this works fine on my development machine (XP) but not on my client's machines (Win7), where the program freezes with the announcement External exception C015D00F (same address as previously).
After no small amount of googling, I discovered that Exception code C015000F is caused by "the activation context being deactivated is not the most recently activated one." What does this mean to a poor Delphi programmer?
Application
as an owner to the form. and doweb.Navigate
on form show, instead of onCreate. - kobik