Store the HTML source code of a TWebBrowser document into a string (Views: 100)


How can I access the HTML content of a TWebBrowser object? I tried to use OLECMDID_SAVEAS to save it as a file first and then access it afterwards. But it always asks for the directory and file name for this file.


Use the following function to store the HTML source code to a string (e.g. a TStringStream):

procedure SaveDocumentSourceToStream(Document: IDispatch; Stream: TStream);
  PersistStreamInit: IPersistStreamInit;
  StreamAdapter: IStream;
  {Delete stream content}
  Stream.Size := 0;
  Stream.Position := 0;
  {IPersistStreamInit - get document interface}
  if Document.QueryInterface(IPersistStreamInit, PersistStreamInit) = S_OK then
    {Use stream adapter to get the IStream Interface to our stream}
    StreamAdapter := TStreamAdapter.Create(Stream, soReference);
    {Save data from document into stream}
    PersistStreamInit.Save(StreamAdapter, False);
    {Destroy stream adapter. Optional, as it would happen anyway}
    StreamAdapter := nil;

<< Back to main page