Solved

How to load and save Ms Word document from Blob field

Posted on 2006-11-19
9
1,914 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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
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
 
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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

813 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