Marin Atanasov Guest
|
Posted: Fri Jun 27, 2003 11:12 am Post subject: Re: Load MSWord from a memory file |
|
|
Hi, George!
Hope, I'm not too late with the answer.
Yes, MSWord cannot load it's document from any place, but the file.
But!
I solved problem, getting different point of view on it.
I needed to Hold documents in a database, but their sources(originals) are
word files.
So I read them and put in the database, so I can read them back later.
Here you are some code illustrating my words, because my English is too bad
:
Assume that:
"OLE1" is an OLEContainer.
===== 1 ====== Opening DOC file and saving it into database :
procedure TfrmMain.DocFileImport(aFileName: String; Qry: TQuery);
var
BlobMemStream:TMemoryStream;
......
begin
OLE1.CreateObjectFromFile(aFileName,false);
OLE1.Close;
BlobMemStream:=TMemoryStream.Create;
OLE1.SaveToStream(BlobMemStream);
BlobMemStream.Seek( 0, soFromBeginning);
With uQry do
begin
Close;
SQL.Text := 'INSERT INTO MyTable VALUES ( :docBody)';
ParamByName(aParamName).LoadFromStream(aStream, ftBlob);
ExecSQL;
FreeAndNil(BlobMemStream);
end;
end;
===== 2 ====== Loading DOC from database in OLEContainer :
Procedure LoadDatabaseDoc( Qry : TQuery);
Var
MemBlobStream : TStream;
begin
With Qry do
begin
Close;
SQL.Text := 'SELECT docBody FROM MyTable';
Open;
try
try
MemBlobStream:=Qry.CreateBlobStream(Qry1.FieldByName('advTemplateBody'),bmRe
ad);
MemBlobStream.Seek( 0, soFromBeginning);
OLE1.LoadFromStream( MemBlobStream);
finally
FreeAndNil( MemBlobStream);
end;
finally
Close;
end;
end;
In other words, you save OLEContainer's content to stream which is OLE
Header + OLE Document, which gives you ability to load it later. Word as an
application cannot load document from stream, but OLEContainer can read any
ole object data and construct ole object depending on data. (of course OLE
Server must present in order to edit document e.g. you must have Word
installed on the computer which need to run your program if program requires
document editing).
Hope, it will help you,
Marin
"GW" <george.wnag (AT) justice (DOT) com> wrote
| Quote: | In Delphi 7pro, I need to open and view a Word document in MSWord from
memory (binary stream-retrieved from database). In the past, for lacking a
better way of doing it, I just saved the stream to a temp file and then
ask
MSWord to open the temp file. I think there must be a way of sending the
stream directly from memory to Word, sort of a loadFromStream method. Can
anyone help? Thanks.
--George
|
|
|