Question

The following code gives me several errors two of which are:

  • [Error] Unit1.pas(28): Undeclared identifier: 'WebBrowser1NavigateComplete2'
  • [Error] Unit1.pas(34): Undeclared identifier: 'WebBrowser1DocumentComplete'

I'm wondering if they aren't declared in the 'uses' section. Does anyone have any idea why it won't compile. I got the code from Delphi Help.

Also the 'message box' or the 'beep' for completion doesn't appear.

unit Unit1;

interface

uses
    Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
    Dialogs, OleCtrls, SHDocVw, StdCtrls, MSHTML, activex;

type
    TForm1 = class(TForm)
    Button1: TButton;
    Edit1: TEdit;
    WebBrowser1: TWebBrowser;
    procedure Button1Click(Sender: TObject);
private
    { Private declarations }
public
    { Public declarations }
end;

var
    Form1: TForm1;
    CurDispatch: IDispatch; {save the interface globally }

implementation

{$R *.dfm}
procedure TForm1.WebBrowser1NavigateComplete2(Sender: TObject; const pDisp: IDispatch; var URL: OleVariant);
begin
    if CurDispatch = nil then
    CurDispatch := pDisp; { save for comparison }
end;

procedure TForm1.WebBrowser1DocumentComplete(Sender: TObject; const pDisp: IDispatch; var URL: OleVariant);
begin
if (pDisp = CurDispatch) then
    begin
        Beep; {the document is loaded, not just a frame }
        showmessage('download complete');
        CurDispatch := nil; {clear the global variable }
    end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
    WebBrowser1.Navigate('www.google.com');
end;

end.
Was it helpful?

Solution

The problem is, that you did not declare the methods.

type
    TForm1 = class(TForm)
    Button1: TButton;
    Edit1: TEdit;
    WebBrowser1: TWebBrowser;
    procedure Button1Click(Sender: TObject);
    // missing declarations
    procedure WebBrowser1NavigateComplete2(Sender: TObject; const pDisp: IDispatch; var URL: OleVariant);
    procedure WebBrowser1DocumentComplete(Sender: TObject; const pDisp: IDispatch; var URL: OleVariant);
private
    { Private declarations }
public
    { Public declarations }
end;

Seems that you just copy'paste the methods from somewhere else :o)


Manual assign Event Handler

For testing purpose I have a simple form with a single TWebBrowser component and I have written 2 methods WebBrowser1DocumentComplete and HandmadeDocumentComplete.

The form code look like this

unit Unit1;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.OleCtrls, SHDocVw;

type
  TForm1 = class( TForm )
    WebBrowser1 : TWebBrowser;
    procedure WebBrowser1DocumentComplete( ASender : TObject; const pDisp : IDispatch; const URL : OleVariant );
    procedure HandmadeDocumentComplete( ASender : TObject; const pDisp : IDispatch; const URL : OleVariant );
  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  end;

var
  Form1 : TForm1;

implementation

{$R *.dfm}

procedure TForm1.HandmadeDocumentComplete( ASender : TObject; const pDisp : IDispatch; const URL : OleVariant );
begin
  // Do something
end;

procedure TForm1.WebBrowser1DocumentComplete( ASender : TObject; const pDisp : IDispatch; const URL : OleVariant );
begin
  // Do something
end;

end.

Now have a look at the ObjectInspector for WebBrowser1

ObjectInspector

Every published method that matches the event handler property will be shown inside the combo box and can be selected.

Or you can assign the method by code

procedure TForm1.FormCreate( Sender : TObject );
begin
  // WebBrowser1.OnDocumentComplete := WebBrowser1DocumentComplete;
  WebBrowser1.OnDocumentComplete := HandmadeDocumentComplete;
end;

OTHER TIPS

This code is working. Tested under Xe³. http://i.stack.imgur.com/y3WdX.png

enter image description here

Here is the diff between your code & my compiled code.

http://i.stack.imgur.com/sRITO.png

enter image description here

You have to place TWebBrowser visual component to your form.

http://i.stack.imgur.com/t1CBY.png

enter image description here

enter image description here

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top