zxw
asked on
exception when use MSHTML_TLB
Hi,
I want to use IHTMLDocument2 and IHTMLElement in MSHTML_TLB to analyse HTML files with D5.
But I got exception:
Project Project2.exe raised exception class EVarianError with message 'Invalid variant operation'.Process stopped.
The following are the source code, can anyone help me?
BTW: I got the sample source code from Borland community, the article ID is 26574. This arictle tells me to ignore the exception. But I can't.
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
OleCtrls,SHDocVw,OleServer , ComCtrls, StdCtrls, ExtCtrls, ToolWin;
type
TForm1 = class(TForm)
CoolBar1: TCoolBar;
ToolBar1: TToolBar;
ToolBar2: TToolBar;
StatusBar1: TStatusBar;
Panel1: TPanel;
Splitter1: TSplitter;
Panel2: TPanel;
lstbxLinks: TListBox;
OpenDialog1: TOpenDialog;
ToolButton1: TToolButton;
procedure ToolButton1Click(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
FInternetExplorer: TInternetExplorer;
procedure WebBrowserDocumentComplete (Sender: TObject; var pDisp: OleVariant;
var URL: OleVariant);
procedure ParseHTMLFile(const sFile : string);
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
uses MSHTML_TLB, ComObj;
{$R *.DFM}
procedure TForm1.FormCreate(Sender: TObject);
begin
FInternetExplorer := TInternetExplorer.Create(S elf);
FInternetExplorer.OnDocume ntComplete := WebBrowserDocumentComplete ;
end;
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
if FInternetExplorer <> nil then FInternetExplorer.Free;
end;
procedure TForm1.ParseHTMLFile(const sFile : string);
begin
// Simply browse to the given page
FInternetExplorer.Navigate (sFile, EmptyParam, EmptyParam,
EmptyParam, EmptyParam);
end;
procedure TForm1.WebBrowserDocumentC omplete(Se nder: TObject;
var pDisp: OleVariant; var URL: OleVariant);
var
HtmlDoc: IHTMLDocument2;
ElementCollection: IHTMLElementCollection;
HtmlElement: IHTMLElement;
I: Integer;
AnchorString: string;
begin
HtmlDoc := FInternetExplorer.Document as IHTMLDocument2;
if HtmlDoc = nil then
raise Exception.Create('Couldn'' t convert the ' +
'FInternetExplorer.Documen t to an IHTMLDocument2');
// First, grab all the elements on the web page
ElementCollection := HtmlDoc.all;
for I := 0 to ElementCollection.length - 1 do
begin
// Get the current element
HtmlElement := ElementCollection.item(I, '') as IHTMLElement;
// Next, check the tagName
if HTMLElement.tagName = 'A' then
begin
AnchorString := HtmlElement.innerText;
if AnchorString = '' then
AnchorString := '(Empty Name)';
// We know that the element is an IHTMLAnchorElement since the tagName
// is 'A'.
AnchorString := AnchorString + ' - ' +
(HtmlElement as IHTMLAnchorElement).href;
lstbxLinks.Items.Add(Ancho rString);
end;
end; //for I := 0 to ElementCollection.length - 1 do
end;
procedure TForm1.ToolButton1Click(Se nder: TObject);
begin
if (OpenDialog1.Execute) then
begin
ParseHTMLFile(OpenDialog1. FileName);
end;
end;
end.
I want to use IHTMLDocument2 and IHTMLElement in MSHTML_TLB to analyse HTML files with D5.
But I got exception:
Project Project2.exe raised exception class EVarianError with message 'Invalid variant operation'.Process stopped.
The following are the source code, can anyone help me?
BTW: I got the sample source code from Borland community, the article ID is 26574. This arictle tells me to ignore the exception. But I can't.
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
OleCtrls,SHDocVw,OleServer
type
TForm1 = class(TForm)
CoolBar1: TCoolBar;
ToolBar1: TToolBar;
ToolBar2: TToolBar;
StatusBar1: TStatusBar;
Panel1: TPanel;
Splitter1: TSplitter;
Panel2: TPanel;
lstbxLinks: TListBox;
OpenDialog1: TOpenDialog;
ToolButton1: TToolButton;
procedure ToolButton1Click(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
FInternetExplorer: TInternetExplorer;
procedure WebBrowserDocumentComplete
var URL: OleVariant);
procedure ParseHTMLFile(const sFile : string);
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
uses MSHTML_TLB, ComObj;
{$R *.DFM}
procedure TForm1.FormCreate(Sender: TObject);
begin
FInternetExplorer := TInternetExplorer.Create(S
FInternetExplorer.OnDocume
end;
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
if FInternetExplorer <> nil then FInternetExplorer.Free;
end;
procedure TForm1.ParseHTMLFile(const
begin
// Simply browse to the given page
FInternetExplorer.Navigate
EmptyParam, EmptyParam);
end;
procedure TForm1.WebBrowserDocumentC
var pDisp: OleVariant; var URL: OleVariant);
var
HtmlDoc: IHTMLDocument2;
ElementCollection: IHTMLElementCollection;
HtmlElement: IHTMLElement;
I: Integer;
AnchorString: string;
begin
HtmlDoc := FInternetExplorer.Document
if HtmlDoc = nil then
raise Exception.Create('Couldn''
'FInternetExplorer.Documen
// First, grab all the elements on the web page
ElementCollection := HtmlDoc.all;
for I := 0 to ElementCollection.length - 1 do
begin
// Get the current element
HtmlElement := ElementCollection.item(I, '') as IHTMLElement;
// Next, check the tagName
if HTMLElement.tagName = 'A' then
begin
AnchorString := HtmlElement.innerText;
if AnchorString = '' then
AnchorString := '(Empty Name)';
// We know that the element is an IHTMLAnchorElement since the tagName
// is 'A'.
AnchorString := AnchorString + ' - ' +
(HtmlElement as IHTMLAnchorElement).href;
lstbxLinks.Items.Add(Ancho
end;
end; //for I := 0 to ElementCollection.length - 1 do
end;
procedure TForm1.ToolButton1Click(Se
begin
if (OpenDialog1.Execute) then
begin
ParseHTMLFile(OpenDialog1.
end;
end;
end.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Netminder
CS Moderator