Solved

Save webbrowser component containing ONLY text as a text file (500 points)

Posted on 2006-10-24
4
246 Views
Last Modified: 2010-04-05
I  have the webbrowser component displaying the result of a cgi script - just text without any images, etc.

I want this to be saved automatically as a text file in a specific directory (ie, saved as aaa.txt in a directory called "files" which I already have in the same directory as the software).

I am using Delphi 6.  This is URGENT so I am awarding 500 points for this.  Thanks.
0
Comment
Question by:rincewind666
  • 2
4 Comments
 
LVL 28

Accepted Solution

by:
2266180 earned 250 total points
ID: 17794314
this works for anything from the twebrowser (text or html)

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, OleCtrls, SHDocVw;

type
  TForm1 = class(TForm)
    WebBrowser1: TWebBrowser;
    procedure FormCreate(Sender: TObject);
    procedure WebBrowser1DocumentComplete(Sender: TObject;
      const pDisp: IDispatch; var URL: OleVariant);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

uses activex;

procedure TForm1.FormCreate(Sender: TObject);
begin
  WebBrowser1.Navigate('http://www.ciuly.com');
end;

procedure TForm1.WebBrowser1DocumentComplete(Sender: TObject;
  const pDisp: IDispatch; var URL: OleVariant);
var
  PersistStreamInit: IPersistStreamInit;
  StreamAdapter: IStream;
  FileStream: TFileStream;
begin
  if WebBrowser1.Document.QueryInterface(IPersistStreamInit,
    PersistStreamInit) = S_OK then
  begin
      FileStream:= TFileStream.Create('c:\aaa.txt',fmCreate);
      try
        StreamAdapter:= TStreamAdapter.Create(FileStream, soOwned);
        PersistStreamInit.Save(StreamAdapter,true);
      except
        FileStream.Free;
        raise;
      end;
  end;
end;

end.
0
 
LVL 28

Expert Comment

by:2266180
ID: 17794318
ups. forgot to remove the test page. sorry for that.
0
 
LVL 28

Assisted Solution

by:TName
TName earned 250 total points
ID: 17794384
Hi, ciuly's solution is perfect, but here's another way to do it (now that I've typed it... ;)

procedure TForm1.WebBrowser1DocumentComplete(Sender: TObject;
  const pDisp: IDispatch; var URL: OleVariant);
var
 aText:String;
 fs:TFileStream;
 p:Pointer;
begin
  aText:=IHTMLDocument2(WebBrowser1.Document).Body.innerText;
  fs:=TFileStream.Create(ExtractFilePath(Paramstr(0))+'files\aaa.txt',fmCreate);
  p:=pointer(aText);
  fs.Write(p^, Length(aText));
  fs.Free;
end;
0
 

Author Comment

by:rincewind666
ID: 17798662
Thanks to both of you. Both work - hard to decide.  Therefore I will split the points.
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

815 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now