Getting the HTML code for the current page displayed in Internet Explorer?

I want to display the HTML code of the current page of IE in a memo.  I am using Delphi 6.  Please supply the code, for which I will award 500 points.

Many thanks for your help.
rincewind666Asked:
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.

DragonSlayerCommented:
is your IE an embedded browser in your application? Or is it some external IE process? If internal:

procedure TForm1.WebBrowser1DocumentComplete(Sender: TObject;
  const pDisp: IDispatch; var URL: OleVariant);
var
  pf: IPersistFile;
begin
  if pDisp = WebBrowser1.ControlInterface then
      if Succeeded(WebBrowser1.Document.QueryInterface(IPersistFile, pf)) then
      begin
        OleCheck(pf.Save('somefile.htm', True));
        Memo1.Lines.LoadFromFile('somefile.html');
      end;
end;
0
rincewind666Author Commented:
No.  This is the external Internet Explorer, as supplied with Windows XP.
0
HypoviaxCommented:
If you can work out the URL of the page then you could temporarily download it (copy function) and load it into a memo. The problem is getting the URL.

Hypoviax
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

rincewind666Author Commented:
Hypoviax, I may be able to get the URL. If I know this, how can I download it to my computer?  Can you please supply the code - I am a beginner with Delphi.  Many thanks.
0
HypoviaxCommented:
Ok,

I'll post soon. Bit busy today
0
HypoviaxCommented:
Acutally here is an example:

copyfile(pchar('URL HERE.htm'),pchar( 'C:\DESTINATION.htm'),true);
0
HypoviaxCommented:
Ill post how to open it soon
0
paulb1989Commented:
0
HypoviaxCommented:
Ok,

Download the HTML file:

Example here:

http://www.experts-exchange.com/Programming/Programming_Languages/Delphi/Q_20977025.html

Then load it into your memocontrol:

memo1.lines.loadfromfile(filepath)

Regards,

Hypoviax

0
Eddie ShipmanAll-around developerCommented:
Try this code:

uses .., ShDocVW;

var
  ShellWin:   IShellWindows;
  i:          Integer;
  WB:         IWebbrowser2;
  Doc:        IHTMLDocument2;
  sTitle:      String;
  sURL:        String;
  sSource:     String;
begin
  ShellWin := CoShellWindows.Create;
  for i := 0 to ShellWin.Count - 1 do
  begin
    WB := ShellWin.Item(i) as IWebbrowser2;
    try
      // Can only get HTML source if it is an HTML pages!
      if (Pos('http', WB.Get_LocationURL) = 1) then  // could also be https
      begin
        try
          Doc :=(WB.document as IHTMLDocument2);
          if (Doc.protocol = 'HyperText Transfer Protocol') then
           begin
              sTitle  := Doc.title;
              sURL    := Doc.url;
              sSource := Doc.body.innerHTML;
           end;
        except
        end;
      end;
    except
    end;
  end;
  ShellWin.Free;
end;
0
hush021299Commented:
the code is not pretty but it works for me.
It shows the url of the current webpage as focused in the iexplorer and save it into a memo.

unit pagememoU;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    Memo1: TMemo;
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}
uses
  URLMon, ShellApi, MSHTML_TLB, ActiveX, COMObj,ddeman;


function GetURL(Service: string): string;
var
  ClDDE: TDDEClientConv;
  temp: PChar;
begin
  Result := '';
  //create a new DDE Client object
  ClDDE := TDDEClientConv.Create(nil);
  with ClDDE do
  begin
    SetLink(Service, 'WWW_GetWindowInfo');
    temp := RequestData('0xFFFFFFFF');
    Result := StrPas(temp);
    StrDispose(temp);
    CloseLink;
  end;
  ClDDE.Free;
end;

function DownloadFile(SourceFile, DestFile: string): Boolean;
begin
  try
    Result := UrlDownloadToFile(nil, PChar(SourceFile), PChar(DestFile), 0, nil) = 0;
  except
    Result := False;
  end;
end;

function ExtractUrlFileName(const AUrl: string): string;
var
  i,po: Integer; s:string;
begin
  i := LastDelimiter('/', AUrl);
  result:= uppercase(Copy(AUrl, i + 1, Length(AUrl) - (i)));
end;

function ExtractUrlFilePath(const AUrl: string): string;
var
  i,po: Integer; s:string;
begin
  i := LastDelimiter('/', AUrl);
  result:= uppercase(Copy(AUrl, 1,i + 1));//, Length(AUrl) - (i)));
end;


procedure TForm1.Button1Click(Sender: TObject);

   var n,source,sourcepath,dest:string;l:integer;
begin
    source:= GetURL('IExplore');
    delete(source,1,1);
    l:=pos('"',source);
    delete(source,l-1,length(source));
    n:=ExtractUrlFileName(source);
    sourcepath:=copy(source,1,length(source)-length(N));

    dest:=includetrailingbackslash(extractfilepath(application.exename))+
    ExtractUrlFileName(source);
    if DownloadFile(Source, Dest) then
    memo1.Lines.LoadFromFile(dest);
end;

end.
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
Eddie ShipmanAll-around developerCommented:
rincewind666,
  Please finalize this question
0
rincewind666Author Commented:
Apologies for the delay, due to personal reasons. Anyway, thanks for the help, everyone.
0
Eddie ShipmanAll-around developerCommented:
So, you would rather download the file twice to ge the source? That is what you are doing with the solution you accepted.
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.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.