webbrowser help

Hi!

I need some help on the webbrowser component!

I'm using Delphi 5.

Need to know how to :

- Copy all the component content to the clipboard (like select all; copy)
- Copy the content to a file (imagine is a text file).
- The same as above but the source code.


Thanks
LVL 1
jpcsAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

intheCommented:
Hi

uses activeX (and maybe MSHTML_TLB;)

procedure TForm1.Button1Click(Sender: TObject);
begin
opendialog1.execute;
webbrowser1.oleobject.navigate(opendialog1.filename);
end;

procedure TForm1.Button2Click(Sender: TObject);
var
 vInput,vOutput,cInput,Coutput : OleVariant;
begin
 WebBrowser1.ExecWB(OLECMDID_SELECTALL,0,vInput,vOutput);
 WebBrowser1.ExecWB(OLECMDID_COPY,0,cInput,cOutput);
 Webbrowser1.refresh;
 memo1.SetFocus;
 memo1.pastefromclipboard;
 end;

procedure TForm1.Save1Click(Sender: TObject);
var
persist :ipersistfile;
begin
persist := (webbrowser1.document as ipersistfile);
persist.save('c:\a.html',false);
showmessage('page saved as c:\a.html');
end;


to get source code:

uses MSHTML_TLB;

procedure TForm1.Button1Click(Sender: TObject);
var
Doc :  IHtmlDocument2 ;
begin
Doc := Webbrowser1.Document as IHtmlDocument2;
Memo1.Clear;
Memo1.lines.add(trim(doc.body.innerHtml));
{also body.outertext,body.innertext and body.outerhtml}
end;

Regards Barry
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
intheCommented:
note:
 you should also add a message handler something like below,this stops several errors and adds some functionality that otherwise doesnt work properly like some keys strokes dont work etc..

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, OleCtrls, SHDocVw_TLB,ActiveX;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Edit1: TEdit;
    WebBrowser1: TWebBrowser;
    procedure Button1Click(Sender: TObject);
    procedure FormActivate(Sender: TObject);
    procedure FormDeactivate(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
  private
    { Private declarations }
  public
  procedure MyMessageHandler(var Msg: TMsg; var Handled: Boolean);

    { Public declarations }
  end;

var
  Form1: TForm1;
  FOleInPlaceActiveObject: IOleInPlaceActiveObject;
  SaveMessageHandler: TMessageEvent;


implementation

{$R *.DFM}

procedure TForm1.MyMessageHandler(var Msg: TMsg; var Handled: Boolean);
var
  iOIPAO: IOleInPlaceActiveObject;
  Dispatch: IDispatch;
begin
  { exit if we don't get back a webbrowser object }
  if WebBrowser1 = nil then
  begin
    Handled := False;
    Exit;
  end;

  Handled:=(IsDialogMessage(WebBrowser1.Handle, Msg) = True);

  if (Handled) and (not WebBrowser1.Busy) then
  begin
    if FOleInPlaceActiveObject = nil then
    begin
      Dispatch := WebBrowser1.Application_;
      if Dispatch <> nil then
      begin
        Dispatch.QueryInterface(IOleInPlaceActiveObject, iOIPAO);
        if iOIPAO <> nil then
          FOleInPlaceActiveObject := iOIPAO;
      end;
    end;

    if FOleInPlaceActiveObject <> nil then
      if ((Msg.message = WM_KEYDOWN) or (Msg.message = WM_KEYUP)) and
         ((Msg.wParam = VK_BACK) or (Msg.wParam = VK_LEFT) or (Msg.wParam = VK_RIGHT)) then
        //nothing - do not pass on Backspace, Left or Right arrows
      else
        FOleInPlaceActiveObject.TranslateAccelerator(Msg);
  end;
end;

procedure TForm1.FormActivate(Sender: TObject);
begin
  SaveMessageHandler := Application.OnMessage;
  Application.OnMessage := MyMessageHandler;

end;

procedure TForm1.FormDeactivate(Sender: TObject);
begin
Application.OnMessage := SaveMessageHandler;
end;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
Application.OnMessage := SaveMessageHandler;
  FOleInPlaceActiveObject := nil;
end;

procedure TForm1.Button1Click(Sender: TObject);
var a,b,c,d:OleVariant;
begin
     WebBrowser1.Navigate(Edit1.Text,a,b,c,d);
end;

initialization
  OleInitialize(nil);

finalization
  OleUninitialize;

end.

0
feupCommented:
Sorry to reject your answer, but the copy/paste function work fine, but the source code didn't :

MSHTML_TLB unit could not be found. Where is it?

All the rest was OK!

Please note that it doesn't work without the code from the comment.


Regards
Joao
0
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

jpcsAuthor Commented:
Hi FEUP!

Sorry to inform you that only I can Accept or reject the answers... hehehe

Maybe you shouldn't have eatten so many candys!


Se ya!
0
jpcsAuthor Commented:
Thanks Inthe!

FEUP is right, but it's a minor detail, since if I save the file to the disk, it saves the HTML document, so there's no big deal on that unit.

Thanks a lot.
0
jpcsAuthor Commented:
Hi Inthe. Sorry for disturbing you again, but I'm having a probem of converting a string (the path of the file) to the PWIDECHAR (used to save the file).

Can you help me?


thanks
0
jpcsAuthor Commented:
Ops.... never mind. I already found out :


var path : string;
begin
....
string := extractfilepath (application.exename)+'yada.html';
pwidechar (widestring (path));


Thanks anyway.

0
jpcsAuthor Commented:
err...the last line is
persist.save(pwidechar (widestring (path)),false);

sorry
0
jpcsAuthor Commented:
err...the code is :


path := extractfilepath (application.exename)+'yada.html';
persist.save(pwidechar (widestring (path)),false);

sorry :))
0
intheCommented:
hi jpcs
i see you have it working but for the info i use the code as i pasted it here(it came straight from one of my projects)
maybe a different version of delphi uses different declarations or something..


feup ,
MSHTML_TLB comes from MSHTML.dll in windows system.when you go to the import activeX page click the "add" button and find this dll then it will add it to the list and you can import it (cant remember exaclty but maybe you need ie5 iunstalled first but i think it works in ie4 as well)..
0
jpcsAuthor Commented:
Hi inthe. Just one question :

In the line

Dispatch := WebBrowser1.Application_;
     
Application has a low-slash ... Is it OK? in my project, I removed it and it worked fine.


regards
0
intheCommented:
alot of the activeX use low slash strange isnt it(some naming convention) ,as you say it works without it also..
btw
updated message handler(lets ie take care of more things)

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, OleCtrls, SHDocVw_TLB,ActiveX;

type
  TForm1 = class(TForm)
    WebBrowser1: TWebBrowser;
    Button1: TButton;
    OpenDialog1: TOpenDialog;
    procedure Button1Click(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
   OIPAO  : IOleInPlaceActiveObject;
   OIPAO_Wnd:HWND;

  public
    { Public declarations }
    procedure MyMessageHandler(var Msg: TMsg; var Handled: Boolean);
  end;

var
  Form1: TForm1;
  SaveMessageHandler: TMessageEvent;
implementation

{$R *.DFM}

procedure Tform1.MyMessageHandler(var Msg: TMsg; var Handled: Boolean);
const
  DuplicatedKeys: set of Byte = [VK_TAB, VK_BACK, VK_LEFT, VK_RIGHT, VK_UP, VK_DOWN, VK_PRIOR, VK_NEXT];
var
   w:HWND;
begin
     Handled:=False;
     if Msg.message = WM_KEYDOWN then
     begin
          if Msg.wParam in DuplicatedKeys then
         //do nothing let ie take care of it
           end;
     w:=GetFocus;
     if (w=0) {or (w=[].Handle)} then Exit;
     if OIPAO<>nil then
     begin
          if w = OIPAO_Wnd then Handled := IsDialogMessage(w,Msg);
          if not Handled then Handled := (OIPAO.TranslateAccelerator(Msg) = S_OK);
     end;
     if (not Handled) and Assigned(SaveMessageHandler) then SaveMessageHandler(Msg,Handled);
 end;

procedure TForm1.FormCreate(Sender: TObject);
var
   Dispatch:IDispatch;
begin
    if assigned(WebBrowser1) then
     begin
          Dispatch := WebBrowser1.Application_;
          if Dispatch<>nil then
          begin
               Dispatch.QueryInterface(IOleInPlaceActiveObject, OIPAO);
          end;
          if OIPAO <> nil then OIPAO.GetWindow(OIPAO_Wnd);
     end;
     SaveMessagehandler := Application.OnMessage;
     Application.OnMessage:=MyMessageHandler;
 end;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
OIPAO := nil;
Application.OnMessage := SaveMessageHandler;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
opendialog1.execute;
webbrowser1.OleObject.navigate(opendialog1.filename);
end;

initialization
  OleInitialize(nil);

finalization
  OleUninitialize;

end.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.