Solved

How to load and save Ms Word document from Blob field

Posted on 2006-11-19
9
1,892 Views
Last Modified: 2012-05-05
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
0
Comment
Question by:alanjbrown
  • 4
  • 4
9 Comments
 
LVL 10

Expert Comment

by:atul_parmar
ID: 17977519
0
 
LVL 1

Author Comment

by:alanjbrown
ID: 17978239
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
 
LVL 10

Expert Comment

by:atul_parmar
ID: 17978390
>>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
 
LVL 1

Author Comment

by:alanjbrown
ID: 17978468
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
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.

 
LVL 10

Expert Comment

by:atul_parmar
ID: 17978490
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
 
LVL 1

Author Comment

by:alanjbrown
ID: 17978500
I am just trying to remove the need for saving the dataset contents to a temporary file.
0
 
LVL 10

Accepted Solution

by:
atul_parmar earned 250 total points
ID: 17978666
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
 
LVL 9

Expert Comment

by:bernani
ID: 17985690
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
 
LVL 1

Author Comment

by:alanjbrown
ID: 17995697
Thank you Atul.

Very good

Alan
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

Title # Comments Views Activity
Delphi 10 Seattle Dataset Actions 5 78
Best Firemonkey component pack 1 85
Delphi Firemonkey Need Sample for Online Shopping Example. 2 119
Reconfigure Delphi Install? 2 45
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…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Need to grow your business through quality cloud solutions? With everything required to build a cloud platform and solution, you may feel like the distance between you and the cloud is quite long. Help is here. Spend some time learning about the Con…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

930 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

10 Experts available now in Live!

Get 1:1 Help Now