2005. augusztus 7., vasárnap

Store the HTML source code of a TWebBrowser document into a string


Problem/Question/Abstract:

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.

Answer:

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

procedure SaveDocumentSourceToStream(Document: IDispatch; Stream: TStream);
var
  PersistStreamInit: IPersistStreamInit;
  StreamAdapter: IStream;
begin
  {Delete stream content}
  Stream.Size := 0;
  Stream.Position := 0;
  {IPersistStreamInit - get document interface}
  if Document.QueryInterface(IPersistStreamInit, PersistStreamInit) = S_OK then
  begin
    {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;
  end;
end;

Nincsenek megjegyzések:

Megjegyzés küldése