Link to home
Start Free TrialLog in
Avatar of iana1uk
iana1uk

asked on

Get html source from a frame within an Internet Explorer page

Hi, I need a procedure that will get the source html from within a frame on a web page in an already open instance of Internet Explorer, the problem is that the page contains frames. I don't need anything complicated, the frame will always be the same one. The source html then needs to be dumped into a TMemo. Thanks.
ASKER CERTIFIED SOLUTION
Avatar of Eddie Shipman
Eddie Shipman
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of iana1uk
iana1uk

ASKER

Hi Eddie, thanks for the quick reply, I have implemented the code above, added an idHTTP component (as one is referenced in the code) and the frame name is mainFrame, so no change needed there. Code executes without error, but when the button is pressed, nothing happens, Memo1 remains unchanged.

The frames are called using a relative URL.

Another point is that the page being retrieved has to be the one that is currently displayed as it is returning information that is different every time it is called.
Got a URL for me to test on? I tested on http://reportman.sourceforge.net/doc/index.html, only framed site I knew of OTTOMH.
Avatar of iana1uk

ASKER

Hi Eddie, problem solved, it might be an untidy hatchet job, but it seems to do what I need. I've used some of your code above, the points are yours, thanks for your help! Ian.

procedure TForm1.Button5Click(Sender: TObject);
var
  ShellWin : TShellWindows;
  i,x : integer;
  IE : IWebBrowser2;
  Document: IHTMLDocument2;
  Frame: IDispatch;
  FrameNum: OleVariant;
  FrameDoc: IHTMLDocument2;
begin
  ShellWin := TShellWindows.Create(nil);
  for i := 0 to ShellWin.Count-1 do
  begin
    ShellWin.Item(i).QueryInterface(IID_IWebBrowser2, IE);
    Document := IE.Document as IHTMLDocument2;
  end;
  try
    x := Document.Frames.Length;
    if x <> 0 then
      for x := 0 to x - 1 do
      try
        FrameNum := x;
        Frame := Document.Frames.Item(FrameNum);
        FrameDoc := (Frame as IHTMLWindow2).Document as IHTMLDocument2;
        Memo1.Text := FrameDoc.Get_body.Get_outerHTML;
      except
      end;
   except
   end;
  Document := nil;
  ShellWin.Free;
end;
Oh, DAMN, I forgot about the IHTMLWindow2 object. I'm surprised that IE Security let you do it, though.
Avatar of iana1uk

ASKER

I knew of it but didn't quite know how it worked. I borrowed a couple of lines of code from the IESnifferAutoFill components. I'm just happy it's now doing what I want!