Link to home
Start Free TrialLog in
Avatar of zxw
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(Self);
     FInternetExplorer.OnDocumentComplete := 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.WebBrowserDocumentComplete(Sender: 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.Document 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(AnchorString);
    end;
  end; //for I := 0 to ElementCollection.length - 1 do



end;

procedure TForm1.ToolButton1Click(Sender: TObject);
begin
     if (OpenDialog1.Execute) then
     begin
          ParseHTMLFile(OpenDialog1.FileName);
         

     end;
end;



end.
ASKER CERTIFIED SOLUTION
Avatar of Lee_Nover
Lee_Nover

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Netminder
Netminder

Force-accepted by
Netminder
CS Moderator