Mirror

Get the HTML code of the active document of a TWebBrowser component (Views: 704)


Problem/Question/Abstract:

How to get the HTML code of the active document of a TWebBrowser component

Answer:

Solve 1:

procedure GetHtmlCode(WebBrowser: TWebBrowser; FileName: string);
var
  htmlDoc: IHtmlDocument2;
  PersistFile: IPersistFile;
begin
  htmlDoc := WebBrowser.document as IHtmlDocument2;
  PersistFile := HTMLDoc as IPersistFile;
  PersistFile.save(StringToOleStr(FileName), true);
end;


Solve 2:

This function returns the body as a string, but, maybe, all you need is the InnerText from IHTMLDocument2.

function TFrameBook.GetFullHTMLBody(): string;
var
  S: TStringStream;
begin
  S := TStringStream.Create('');
  try
    (WebBrowser1.Document as IPersistStreamInit).Save(TStreamAdapter.Create(S), True);
    Result := S.DataString;
  finally
    S.Free;
  end;
end;

<< Back to main page