Solved

How to load and save Ms Word document from Blob field

Posted on 2006-11-19
9
1,874 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
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

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
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…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

746 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