Word 2007 Automation - Grouping and COM Exceptions

Feb 22 2008 6:12 AM
This is a bit of a long shot posting something that may require an expert from Microsoft to clarify.

I am trying to generate a Word 2007 from a C# Add-in using Visual Studio 2008 Beta 2.  The code creates the document from a pre-defined template.

I need to do some manipulation to the document to inject data from our application in placeholders specified by the user.

The document essentially contains a series of tables that I need to clear down and rebuild.  I use Group Content Controls to lock the tables so that the user can't delete them unintentionally.

Anyway, I am experiencing a couple of different COM Exceptions from Word and I can't determine that the cause of the problems are to implement a reliable fix.

1.  If I try to unlock a content control group with <content control group>.ungroup() I get the exception "This command is only valid for group content controls.".  The control is definitely a Content Control group as I checked its TYPE property, which is "Microsoft.Office.Interop.Word.WdContentControlType.wdContentControlGroup".
2. Since the ungroup() method failed with the above bizarre exception, I tried to remove it with the delete() method instead.  The <content control group>.delete() method gives the following COM exception - "This method or property is not available because the current selection is locked for editing.".

My initial thought was to check if something is selected and collapse the selection.  However, querying the following in Visual Studio reveals:

objWord.Selection.Range.Start = 0   and
objWord.Selection.Range.End = 1

The range of the group I am trying to unlock is from the range 1003 to 1220, so I don’t understand how there could be an overlap as the current selection is nowhere near my content control.

Can anyone explain why the above errors are happening and advise on how to resolve the situation.  The first error looks like a bug in Word 2007, but the second is bizarre - what is the selection that word thinks is locked for editing, and how do I unlock it to make my changes?

Thanks in advance.

Peter