Chris Chris - 29 days ago 14
C# Question

Error code 800A1066 when recreating control in Word 2007

I am working on a Word 2007 Document-Level Add-In, using a .dotm file. It's a document that is mostly controlled by buttons on the document pane, which are used to create and destroy certain parts of the document. Some of the things created are content controls.

One such button is a toggle, that needs to remove and re-add about 10 characters and a content control. My problem is that whenever I try to recreate the content control, Word throws a COMException with the message "Command Failed" and an error code of -2146824090 (or 0x800a1066).

Googling this error code suggests that this is a security-related exception related to using the

Open
or
SaveAs
methods to interact with Word documents, or with documents on network drives (See posts here, here, and here). This does not fit what I am doing at all.

Here is the code I am using to delete the control:

LevelControl.LockContentControl = false;
LevelControl.Delete(true);
LevelControl = null; // probably not needed


Here is the recreation code:

DropDownListContentControl LevelControl = this.Controls.AddDropDownListContentControl(r, "SectionLevel");


This is the same line of code I'm using to create the control initially.

Answer

The cause of my issue was a misplaced range object. I had unwittingly placed the range inside another content control, which still contained placeholder text. Unfortunately, the error message said nothing to this effect.

To determine whether you're making the same mistake, try calling r.Select() at the end instead of whatever action may be causing this exception, and see where the cursor is placed. Be sure that the document is not limited to editing in fields when you do this (Review>Protect Document>Restrict Editing and Formatting), otherwise it may automatically move to the beginning of the document, or into a content control, before you can see it.