[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

How to load and save Ms Word document from Blob field

Posted on 2006-11-19
9
Medium Priority
?
2,086 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses

650 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