How to load and save Ms Word document from Blob field

Is it possible to load a document into Word directly from a Blob field using Ole. I am currently saving the Blob field to a temporary file using
dataset.fieldbyname('xx').savetofile('c:\tempfile.doc')
but would prefer if possible to skip the creation of c:\tempfile.doc and send the blob directly to Word.

Thanks in anticipation

Alan
LVL 1
alanjbrownAsked:
Who is Participating?
 
atul_parmarConnect With a Mentor Commented:
I see. You need to drop an OleContainer and adjust your code as following

var
  ms : TMemoryStream;
begin
  ms := TMemoryStream.Create;
  TBlobField(dataset.FieldByName('xx')).SaveToStream(ms); // load into memory;
  ms.position := 0;
  OleContainer1.LoadFromStream(ms); //
  ms.free;
  OleContainer1.Run;
  // assign the same to your existing TWordApplication; now your wd1.document points to the same document
  wd1.ConnectTo(IDISPATCH (OleContainer1.OleObject.Application) as _Application);
  /// continue with wd1 (TWordApplication) which will be in sync with your existing code
end;

0
 
atul_parmarCommented:
0
 
alanjbrownAuthor Commented:
Thanks Atul

Can you be a bit more specific please.

Currently I have

dataset.fieldbyname('xx').savetofile('c:\tempfile.doc')
..
..
..
wd1.documents.open('c:\tempfile.doc, emptyparam, emptyparam.......... )

are you able to give me some example code to replace the above and have you an example of the corresponding save to a blob field.



Regards
Alan
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
atul_parmarCommented:
>>are you able to give me some example code to replace the above and have you an example of the corresponding save to a blob field.

By looking at your code, it seems that you are using the TWordApplication; for OLE I would suggest to replace it with TOleContainer. OR do you have any specific reason for using TWordApplication?

Here is a code with TOleContainer; let me know if you need more info.

var
  ms : TMemoryStream;
begin
  ms := TMemoryStream.Create;
  TBlobField(dataset.FieldByName('xx')).SaveToStream(ms); // load into memory;
  ms.position := 0;
  OleContainer1.LoadFromStream(ms); //
  // to save
  OleContainer1.SaveToStream(ms);
  ms.position := 0;
  TBlobField(dataset.FieldByName('xx')).LoadFromStream(ms);
  Table1.Post;
end;
0
 
alanjbrownAuthor Commented:
I have used the TwordApplication as I am familiar with it and have a lot of existing code within the project that uses it. At the moment I am unfamiliar with ToleContainer but will investigate it further.
0
 
atul_parmarCommented:
ok, so why you want to replace your following code?

dataset.fieldbyname('xx').savetofile('c:\tempfile.doc')
..
wd1.documents.open('c:\tempfile.doc, emptyparam, emptyparam.......... )

Anyways, the following will help you to save the document using the way you familar with.

var
  ms : TMemoryStream;
begin
  ms := TMemoryStream.Create;
  ms.LoadFromFile('c:\tempfile.doc');
  ms.position := 0;
  TBlobField(dataset.FieldByName('xx')).LoadFromStream(ms);
  dataset.Post;
end;
0
 
alanjbrownAuthor Commented:
I am just trying to remove the need for saving the dataset contents to a temporary file.
0
 
bernaniCommented:
Hi,

When you are in Word  (lets say Word2000 for the example), you can directly insert the content of a database field into your document when you click on the button (or menu option) Insert Database which invoke a dialog to create the required query.

If you create a macro inWord, the vba code looks like: (insert all the fields of Table1 from database called Name.mdb)

Selection.Range.InsertDatabase Format:=0, Style:=0, LinkToSource:=False, _
        Connection:="TABLE Table1", SQLStatement:="SELECT * FROM [Table1]" & "", _
        PasswordDocument:="", PasswordTemplate:="", WritePasswordDocument:="", _
        WritePasswordTemplate:="", DataSource:= _
        "F:\ Name.mdb", From:=-1, To:=-1, IncludeFields:=True
End Sub

If you look in the Word200.pas (for example), you'll see in the range interface:

    procedure InsertDatabase {Flags(2), (14/14) CC:0, INV:1, DBG:4}({VT_12:1}var Format: OleVariant;
                                                                    {VT_12:1}var Style: OleVariant;
                                                                    {VT_12:1}var LinkToSource: OleVariant;
                                                                    {VT_12:1}var Connection: OleVariant;
                                                                    {VT_12:1}var SQLStatement: OleVariant;
                                                                    {VT_12:1}var SQLStatement1: OleVariant;
                                                                    {VT_12:1}var PasswordDocument: OleVariant;
                                                                    {VT_12:1}var PasswordTemplate: OleVariant;
                                                                    {VT_12:1}var WritePasswordDocument: OleVariant;
                                                                    {VT_12:1}var WritePasswordTemplate: OleVariant;
                                                                    {VT_12:1}var DataSource: OleVariant;
                                                                    {VT_12:1}var From: OleVariant;
                                                                    {VT_12:1}var To_: OleVariant;
                                                                    {VT_12:1}var IncludeFields: OleVariant); dispid 194;
    procedure AutoFormat {Flags(2), (0/0) CC:0, INV:1, DBG:4}; dispid 195;

As you can import in Word data coming from database (mdb, qry, dbf ...) or text, html, ..., you can surely easily load the content of your blob field directly in Word without the need to store the content in a tmp file .

0
 
alanjbrownAuthor Commented:
Thank you Atul.

Very good

Alan
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.