Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help with TWebBrowser 1

Status
Not open for further replies.

gcaramia

Programmer
Oct 30, 2003
185
IT
I want to add a 'text search' on the page displaied in TWebBrowser. Is it possible?
Thanks to all
 
You need to work with IHTMLDocument2.

If memory serves it is something like this:

Code:
function TForm1.GetDocument(pdisp : IDispatch) : IHTMLDocument2;
var
  Browser : IWebBrowser2;
begin
  Browser := pDisp as IWebBrowser2;
  while Browser.Busy do // nothing;
  Result := Browser.Document as IHTMLDocument2;
end;

function TForm1.GetHTML(Doc : IHTMLDocument2) : AnsiString;
var
  Col   : IHTMLelementCollection;
  Ele   : IHTMLelement;
begin
  Result := '';
  Col := Doc.All.Tags('HTML') as IHTMLelementCollection;
  Ele := Col.Item(0, '') as IHTMLelement;
  if Ele <> Nil
    then  Result := Ele.OuterHTML;
end;

procedure TForm1.Browser1DocumentComplete(Sender: TObject;
                                          const pDisp: IDispatch;
                                          var URL: OleVariant);
var
  Doc : IHTMLDocument2;
  Txt : AnsiString;
begin
  // Rememeber DocumentComplete is fired in every completed
  // frame and may be this is not what you need.
  // (pDisp as IUnknow) = (Browser1.ControlInterface as IUknown)
  // will tell you what is going on.
  Doc := GetDocument(pDisp);
  Txt := GetHTML(Doc);
  // Process the text here.        
end;

Disclaimer: code written on the fly, only to give an idea about what is involved.

HTH.
buho (A).

 
Thanks buho. I appreciate your suggestion. I find another solution that i post here for anybody.

unit Unit1;

interface

uses
.., ACTIVEX,..;

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

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
OpenFindDialog(WebBrowser1);
end;


procedure TForm1.OpenFindDialog(webbrowser :Twebbrowser);
const
CGID_WebBrowser: TGUID = '{ED016940-BD5B-11cf-BA4E-00C04FD70816}';
HTMLID_FIND = 1;
var
CmdTarget : IOleCommandTarget;
vaIn, vaOut: OleVariant;
PtrGUID: PGUID;
begin
New(PtrGUID);
PtrGUID^ := CGID_WebBrowser;
if WebBrowser1.Document <> nil then
try
WebBrowser1.Document.QueryInterface(IOleCommandTarget, CmdTarget);
if CmdTarget <> nil then
try
CmdTarget.Exec(PtrGUID, HTMLID_FIND, 0, vaIn, vaOut);
finally
CmdTarget._Release;
end;
except
end;
Dispose(PtrGUID);
end;

end.
 
Whoops! So I overlooked this IOleCommandTarget HTMLID_FIND thing for years?

It is worth a star.

buho (A).



 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top