Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 379
  • Last Modified:

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.
0
rincewind666
Asked:
rincewind666
  • 5
  • 3
  • 3
  • +3
1 Solution
 
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 
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

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 5
  • 3
  • 3
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now