Cry about...
Delphi Programming with TWebBrowser


How to navigate a frameset


The following example shows how to navigate through a frameset, to extract the URL and HTML of each document. In this example a message box is used to display the URL and HTML of each document. The code fragment works just as well if given a TWebBrowser document that does not contain a frameset:

procedure TMyForm.NavigateFrameset(document: IHTMLDocument2);
var
    index: Integer;
    ole_index: OleVariant;
    frame_dispatch: IDispatch;
    framed: IHTMLWindow2;
begin
    if document = nil then
        exit;
    try
        Application.MessageBox(
            PChar('Content:' + String(document.body.innerHTML)),
            PChar('URL: ' + String(document.URL)),MB_OK or
            MB_ICONINFORMATION);
        for index := 1 to document.Frames.Length do
        try
            ole_index := index-1;
            frame_dispatch := document.Frames.Item(ole_index);
            if frame_dispatch <> nil then
            begin
                framed := frame_dispatch as IHTMLWindow2;
                NavigateFrameset(framed.document);
            end;
        except
            on E: Exception do
            begin
            end
        end;
    except
        on E: Exception do
            begin
                Application.MessageBox(PChar(E.Message),
                PChar('Exception'));
            end;
    end;
end;

and to call it:

procedure TMyForm.Button1Click(Sender: TObject);
begin
    NavigateFrameset(WebBrowser.Document as IHTMLDocument2);
end;

You will also need to use the MSHTML_TLB and SHDocVw_TLB units to provide the necessary definitions.

See also: How to get the HTML displayed in a TWebBrowser.


These notes are believed to be correct for Delphi 6 and may apply to other versions as well.



About the author: is a dedicated software developer and webmaster. For his day job he develops websites and desktop applications as well as providing IT services. He moonlights as a technical author and consultant.