Board index » delphi » Memo Fields and Word documents (Revisited)

Memo Fields and Word documents (Revisited)

I posted a message asking how to take a memo field (ascii text) from a
legacy file and put it into an Advantage Data Server table field of type
TBlobField (Image) as a Word2000 document.  Deborah Pate replied with:

OK, here's how to do it in brief:
pop a TOleContainer on a form.
Create a new Word document in it.
Load up a TStringStream from your
blob field, insert it into the
Word document, something like this:
 Olecontainer.OleObject.content.text
   := Strm.Text;
Then save it to the blob field using
the olecontainer's SaveToStream
method.

I am a newbie to Delphi, but have a good deal of experience in database
applications development.

Can Deborah, or anyone else, tell me how to tell the OleContainer that is on
my form that it is a word2000 document.  The Delphi OleContainer doesn't
appear to have a property that I can set (other than OleContainer.Create)
that uses a document object as the parameter.  What don't I know that I
don't know?

Thanks!
Tom.

 

Re:Memo Fields and Word documents (Revisited)


<<Tom Alexander:
Can Deborah, or anyone else, tell me how to tell the
OleContainer that is on my form that it is a word2000
document.  

Quote

Not sure what problem you're having. When you create the
new object, the olecontainer will create the object using
the application associated with the object type on your
machine. Have a look at your registry settings for the
HKEY_CLASSES_ROOT\Word.Document\CurVer key.

--
Deborah Pate

Re:Memo Fields and Word documents (Revisited)


Thanks!

The Registry was OK.  The document exists and/or can be created, but the
construct you gave me to change the text gives an error 'Invalid Interface'.

MyOleContainer.OleObject.Content.Text := MyStream.Text;

LoadFromStream destroys the original document and when I subsequently
SaveToStream I get an invalid stream error.
It's so close to working.  Thanks again.

Re:Memo Fields and Word documents (Revisited)


<<Tom Alexander:
the construct you gave me to change the text gives an error
'Invalid Interface'.

MyOleContainer.OleObject.Content.Text := MyStream.Text;

Quote

You haven't activated your olecontainer; you must do this
before you can use the oleobject at all. To do it code you
just call
  OleContainer1.DoVerb(ovShow);
and the code I gave you should then work.

--
Deborah Pate

Re:Memo Fields and Word documents (Revisited)


Deborah,

Here is the code I'm fighting.  I tried to build a TStringStream from the
SourceContentsField and assigning it to
OleContainerforWord.OleObject.Content.Text and that assignment failed as
does the one shown here where I'm assigning the memo field
(SourceADstable.Fields[2]) contents directly.  What am I doing wrong?

      // Convert the memo data to a Word2000 document.
      if ( Length( SourceADstable.Fields[2].Value ) > 0 ) and
         ( not SourceADstable.AdsIsRecordDeleted( SourceADstable.RecNo ) )
then
      begin
        // Get memo field data.
        SourceContentsField := SourceADstable.Fields[2] as TBlobField;
        SourceContentsStream := TADSBlobStream.Create(SourceContentsField,
bmRead);

        // Prepare a blob to receive the Word document.
        TargetContentsField := TargetADstable.Fields[4] as TBlobField;
        TargetContentsStream := TAdsBlobStream.Create(TargetContentsField,
bmReadWrite);

        // Bump the old object out of the OleContainer and make a new one.
        OleContainerforWord.DoVerb(ovShow);
        OleContainerforWord.CreateObject('Word.Document', False);

        // Set the text of the new document.
        OleContainerforWord.OleObject.Content.Text :=
SourceADstable.Fields[2].AsString;

        // Save the new document to the ADS table blob field.
        OleContainerforWord.SaveToStream(TargetContentsStream);

      end; // if Length( SourceADstable.Fields[2].Value....

By the way, I'd like to keep the ole container invisible so the user doesn't
see what's going on.  I do provide a progress bar so they know something is
happening.

Thanks!
Tom.

Re:Memo Fields and Word documents (Revisited)


<<Tom Alexander:
// Bump the old object out of the OleContainer and make a
new one.
  OleContainerforWord.DoVerb(ovShow);
  OleContainerforWord.CreateObject('Word.Document', False);

Quote

Call CreateObject *once*, when you  start up, and then
DoVerb after it (you can't show an object that's not
created). Assigning text to the document's content.text
property will overwrite any text that's there already, so
you don't need to create a new document every time.What
errors do you get exactly (and do you still get them when
the olecontainer is visible)?

<<Tom:
By the way, I'd like to keep the ole container invisible so
the user doesn't see what's going on.  

Quote

I don't think that's possible, TOleContainers want to be
visible. The alternative would be to create a Word Document
via automation (CreateOleObject or whatever), insert the
text as you do above, and then use the document's
IPersistStorage interface to save to a stream - but I
haven't tried saving a storage to a stream. (The
ToleContainer SaveToStream method does it, so you could
look at the source fr that if you decided that's what you
want to do.)

--
Deborah Pate

Re:Memo Fields and Word documents (Revisited)


Deborah,

We have success!  Now I can assign the sourcefield.AsString to the
content.text and all works as I would expect.

Thanks for leading me by the hand through this.  And they say you can't
teach an old dog new tricks...

Tom.

Other Threads