Solved

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

Posted on 2006-10-24
4
244 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:
ciuly 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:ciuly
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

743 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

13 Experts available now in Live!

Get 1:1 Help Now