Hello, guys!

I am writing a wrapper class library for some of the main objects of the Word Automation API and need to provide some functionality to the user to display the current page number in a document at a specific location on the page. Using the Macros Tools in Word, I found out how it is done in VBA and managed to translate it into C# code:

//Getting the AutoTextEntry object from the collection of entries in the NormalTemplate.

AutoTextEntry entry = Application.NormalTemplate.AutoTextEntries["- PAGE -"];

//Calling the entry's Insert method to insert the entry into a range in the document.

entry.Insert(this.mRawCell.Range, ref richText) //richText = true;

And here is where a COMException is thrawn with the familiar message "Error HRESULT E_FAIL has been returned from a call to a COM component".

Actually, the this.mRawCell.Range thing is the Range object of a Cell in a Table in the document. I tried to use the Application.Selection.Range object after calling this.mRawRange.Select() , but it didn't work either.

For your information:

I am using Microsoft Word 11.0 Object Library v8.3 with Visual Studio 2005.

The version of the Word Application is 11.8134.8132 SP2.

Thanks for your help!


Re: Visual Studio Tools for Office Word Automation: Inserting AutoText in a document fails

Cindy Meister

You've tested, and the problem only occurs with a cell's range, not in "plain text"

Try first collapsing the Range to its start point (WdCollapseDirection.wdCollapseStart) so that it doesn't include the end-of-cell characters.

Re: Visual Studio Tools for Office Word Automation: Inserting AutoText in a document fails


Hello, Cindy!

Thanks for answering:

Code Snippet

object collapseDirection = WdCollapseDirection.wdCollapseStart;

object startRange = this.mRawCell.Range.Start;

object endRange = this.mRawCell.Range.End;

Range newRange = this.mParentRow.ParentDocument.RawDocument.Range(ref startRange, ref endRange);

newRange.Collapse(ref collapseDirection);

entry.Insert(newRange, ref richText);

Written in this way the code works for me. Actually I modify the Selection object to the bounds of the range of the cell, collapse the range to the start of it, and then use the Selection.Range object to point out where I want the page number positioned.

What bothers me is that some articles I have read about Word Object Model in the MSDN White Pages do not suggest modifying the Selection unless extremely necessary (in order not to mess around with the user experience).

I think that's not an issue, because in my case the user is not shown or allowed to interact with the Word interface while modifying documents via Automation, so changing the Application.Selection's bounds wouldn't be a problem

In fact, collapsing the Cell's range to the end or to the start of it didn't work, as well.

Anyway, thanks for helping me out!

Re: Visual Studio Tools for Office Word Automation: Inserting AutoText in a document fails

Cindy Meister

I'm glad it's working :-)

Yes, it's generally better to not use Select and Selection, unless absolutely necessary. The rule is, if that's the only way it works, then it's necessary. (Of course, there may be a way to get it to work that you just can't find...)

Besides interfering with the user experience, the other issue can be speed/efficiency. Plus, in a general sort of way, you can never be absolutely sure if the Selection is what you think it is (if you're moving left, right, up, down, etc.) But if you're first identifying the range, then selecting it, you should be OK.