1
votes

I got an interesting problem when using ole-automation to insert content to existing word documents using code like this:

WordApp.Selection.HomeKey(unit := wdStory);
if FileExists(s) then
  WordApp.Selection.InsertFile(Filename := s, ConfirmConversions := false, 
  Link := false, Attachment := false);

This works quite well with most word-documents, except those where we got an "automatic table of Content" as first element in the document. In this case the automation crashes with:

This action is not valid outside of a block-level XML

I tried some other functions like:

WordApp.Selection.InsertText('Test') / (#13#10)
WordApp.Selection.InsertBreak(Page/Line)

with the same negative result. It seems that the Cursor Position is invalid when the first element is that automatic table.

Any ideas how I can fix this on my side?

1

1 Answers

1
votes

You need to make sure that the content is inserted at the very beginning of the document and that the first paragraph does not contain a content control (the thing surrounding the ToC).

The home key doesn't always get you to the very beginning. In the case of a table of contents at the beginning of the document, pressing the home key takes you to the beginning of the content control that contains the table of contents. To get in front you can set the End (and Start) property of the Selection to 0. In addition, you have to insert a paragraph before the table of contents:

WordApp.Selection.Start = 0
WordApp.Selection.End = 0
WordApp.Selection.InsertParagraphBefore