user1009073
user1009073

Reputation: 3238

Delphi - Using OLE causes error in Word

Delphi XE6. I have an application for text snippets, which are stored in a local Database (ABS DB). Some snippets may be straight text, others may include formatting. If the snippets include formatting, the snippets are MS Word format.

The user can view the snippets in two ways, inside my app, or by calling MS-Word, and having the snippet loaded there automatically, - IT IS THE SAME SNIPPET ..

Issue: If the snippet is loaded into a TOLEContainer in my app, it displays fine without a problem... If the snippet gets spawned off into MSWord, I get an error...

"We're sorry. We can't open because we found a problem with its contents." I click on OK, and then get "Word found unreadable content in . Do you want to recover the contents of this document?..." I clik OK, and everything displays fine.

My general processing flow for the "spawn off MS WORD" is...

           // FN is a temp file name          
          FileStream := TFileStream.Create(FN, fmCreate);
          BlobStream := dm_text.tEntries.CreateBlobStream(dm_text.tEntries.FieldByName('ANSWER_FMT'), bmRead);
          FileStream.CopyFrom(BlobStream, BlobStream.Size);
          BlobStream.Free;
          FileStream.Free;
          // Now open default association, which will be Word
          ShellExecute(Handle, 'open', PWideChar(FN), nil, nil, SW_SHOWNORMAL);

This flow is nearly identical for the In place viewing... other than a few commands for the TOleContainer.

    OleWord.Enabled := True;
    FileStream := TFileStream.Create(FN, fmCreate);
    BlobStream := tEntries.CreateBlobStream(tEntries.FieldByName('ANSWER_FMT'), bmRead);
    FileStream.CopyFrom(BlobStream, BlobStream.Size);
    BlobStream.Free;
    FileStream.Free;

    OleWord.LoadFromFile(FN);  
    OleWord.DoVerb(ovInPlaceActivate);

Any ideas why this is happening? This happens on MULTIPLE versions of MSWord.

ADDITIONAL INFO: Both routines look at the same data, pulled from the exact same row/column in my DB. What I do is create a TEMP file, and then load either via TOleContainer, which loads it fine, or via ShellExecute, which gives an error. However, if I manually load the TEMP file for the OLE Container into MSWord, I get the same error.

So - possibilities... 1). My data is corrupted, i.e. how I save it is wrong...but Word can correct it. 2). I have a setting somehow so that OLEContainer doesn't show the error but Word does.

Upvotes: 2

Views: 2220

Answers (1)

The_Fox
The_Fox

Reputation: 7062

It is because when using OleContainer.SaveToFile or SaveAsDocument, you are not creating a docx file, but an OleObject containing a docx file. When using OleContainer.SaveToFile with UseOldStreamFormat = True, there is even a Delphi specific header added. Word fortunatly detects this and gives you the option to restore the file.

If you want a valid word-document, then activate the OleContainer (OleContainer.DoVerb(ovPrimary) and then save the document via Word itself (OleContainer.OleObject.SaveAs(MyFileName, wdFormatDocument, EmptyParam, EmptyParam, False). After that you can store the resulting file in your database.

Upvotes: 4

Related Questions