Solved

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

Posted on 2006-10-24
4
245 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
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…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

867 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

21 Experts available now in Live!

Get 1:1 Help Now