none
Trouble adding content controls to footer RRS feed

  • Question

  • Hi,

    I've got a Word 2007 Template Project that I created in Visual Studio 2010.  The project is targetting the .NET 4.0 framework.  I'm trying to add content controls to the Footers in my document.  But I'm getting an error that says "System.Runtime.InteropServices.COMException (0x800A11FD): This method or property is not available because the current selection partially covers a plain text content control."

    See my sample code.  The first two content controls are being added correctly.  I get the error only when adding the third content control. I've tried everything I can think of to get the range/selection so that it's at the very beginning of the footer before I add the ContentControl, but I'm still getting the error.

    Here's my C# code:

    Word.Range range = doc.Sections[1].Footers[Word.WdHeaderFooterIndex.wdHeaderFooterFirstPage].Range;
    range.set_Style("myfooterstyle");
    
    range.Collapse(Word.WdCollapseDirection.wdCollapseStart);
    Word.ContentControl cc = range.ContentControls.Add(DocumentSubtypeContentControl.Type, range);
    cc.XMLMapping.SetMapping(DocumentSubtypeContentControl.XMLMappingXpath, ResearchTemplateMetadata.NAMESPACE_ATTRIBUTE);
    cc.Tag = DocumentSubtypeContentControl.TagFooter;
    cc.Title = DocumentSubtypeContentControl.Title;
    cc.MultiLine = DocumentSubtypeContentControl.MultiLine;
    cc.LockContentControl = DocumentSubtypeContentControl.LockContentControl;
    cc.LockContents = DocumentSubtypeContentControl.LockContents;
    
    range.StartOf(Word.WdUnits.wdStory);
    range.Collapse(Word.WdCollapseDirection.wdCollapseStart);
    range.InsertParagraphBefore();
    
    range = range.Paragraphs[1].Range;
    range.StartOf(Word.WdUnits.wdStory);
    range.Collapse(Word.WdCollapseDirection.wdCollapseStart);
    cc = range.ContentControls.Add(DocumentNumberContentControl.Type, range);
    cc.XMLMapping.SetMapping(DocumentNumberContentControl.XMLMappingXpath, ResearchTemplateMetadata.NAMESPACE_ATTRIBUTE);
    cc.Tag = DocumentNumberContentControl.Tag;
    cc.Title = DocumentNumberContentControl.Title;
    cc.LockContentControl = DocumentNumberContentControl.LockContentControl;
    cc.LockContents = DocumentNumberContentControl.LockContents;
    cc.SetPlaceholderText(Text: DocumentNumberContentControl.PlaceholderText);
    
    range.StartOf(Word.WdUnits.wdStory);
    range.Collapse(Word.WdCollapseDirection.wdCollapseStart);
    range.InsertBefore(", #");
    
    range = range.Paragraphs[1].Range;
    range.StartOf(Word.WdUnits.wdStory);
    range.Collapse(Word.WdCollapseDirection.wdCollapseStart);
    cc = range.ContentControls.Add(PublicationDateContentControl.Type, range); // This is where the error is being thrown!
    cc.XMLMapping.SetMapping(PublicationDateContentControl.XMLMappingXpath, ResearchTemplateMetadata.NAMESPACE_ATTRIBUTE);
    cc.Tag = PublicationDateContentControl.Tag;
    cc.DateDisplayFormat = PublicationDateContentControl.DateDisplayFormat;
    cc.LockContentControl = PublicationDateContentControl.LockContentControl;
    cc.LockContents = PublicationDateContentControl.LockContents;

    The "DocumentSubtypeContentControl", "DocumentNumberContentControl", and "PublicationDateContentControl" are classes we wrote that just contain a bunch of static accessor methods that return the appropriate values for the ContentControls. The first two types are Word.WdContentControlType.wdContentControlText and the PublicationDateContentControl type is a Word.WdContentControlType.wdContentControlDate.

    Thanks in advance for the help.




    • Edited by theLuggage Tuesday, February 28, 2012 8:45 PM
    Tuesday, February 28, 2012 8:37 PM

Answers

  • Hi Calvin,

    Thanks for the help.

    It turns out the problem is that before adding the text and content controls to the footer, I was adding a graphic that appears in the margin of the page.  (I didn't put that code in my code snippet because I didn't think it was relevant.)  When I moved that graphic insertion code down to after I've added all the text and content controls, everything worked.

    So even though the error pointed to the selection having overlapping content controls, the error really was related to the image.

    Thanks again for the help.  Knowing that the code worked for you helped me narrow down the problem to the images.


    • Marked as answer by theLuggage Wednesday, February 29, 2012 4:54 PM
    • Edited by theLuggage Wednesday, February 29, 2012 5:12 PM
    Wednesday, February 29, 2012 4:54 PM

All replies

  • Hi,

    I don't know what kind of controls DocumentSubtypeContentControl DocumentNumberContentControl and PublicationDateContentControl are stand for respectively. You need to see ControlControls.Add method to check how the arguments look like: WdContentControlType Enumeration

    You can also have a look at my test code:

        private void AddContentControls(Word.Document doc)
            {
                Word.Range range = doc.Sections[1].Footers[Word.WdHeaderFooterIndex.wdHeaderFooterFirstPage].Range;  
    
                range.Collapse(Word.WdCollapseDirection.wdCollapseStart);
                Word.ContentControl cc = range.ContentControls.Add(Word.WdContentControlType.wdContentControlRichText, range);        
    
                range.StartOf(Word.WdUnits.wdStory);
                range.Collapse(Word.WdCollapseDirection.wdCollapseStart);
                range.InsertParagraphBefore();
    
                range = range.Paragraphs[1].Range;
                range.StartOf(Word.WdUnits.wdStory);
                range.Collapse(Word.WdCollapseDirection.wdCollapseStart);
                cc = range.ContentControls.Add(Word.WdContentControlType.wdContentControlRichText, range);
          
    
                range.StartOf(Word.WdUnits.wdStory);
                range.Collapse(Word.WdCollapseDirection.wdCollapseStart);
                range.InsertBefore(", #");
    
                range = range.Paragraphs[1].Range;
                range.StartOf(Word.WdUnits.wdStory);
                range.Collapse(Word.WdCollapseDirection.wdCollapseStart);
                cc = range.ContentControls.Add(Word.WdContentControlType.wdContentControlRichText, range); // This is where the error is being thrown!
               
            }

    As I think the error has nothing to do with data binding, I removed that part and made a little bit of modification to fit the arguments. The code works for me.

    I look forward to hearing of you.


    Calvin Gao[MSFT]
    MSDN Community Support | Feedback to us

    Wednesday, February 29, 2012 6:50 AM
    Moderator
  • Hi Calvin,

    Thanks for the help.

    It turns out the problem is that before adding the text and content controls to the footer, I was adding a graphic that appears in the margin of the page.  (I didn't put that code in my code snippet because I didn't think it was relevant.)  When I moved that graphic insertion code down to after I've added all the text and content controls, everything worked.

    So even though the error pointed to the selection having overlapping content controls, the error really was related to the image.

    Thanks again for the help.  Knowing that the code worked for you helped me narrow down the problem to the images.


    • Marked as answer by theLuggage Wednesday, February 29, 2012 4:54 PM
    • Edited by theLuggage Wednesday, February 29, 2012 5:12 PM
    Wednesday, February 29, 2012 4:54 PM
  • Great~Glad  to hear you that you got it worked.


    Calvin Gao[MSFT]
    MSDN Community Support | Feedback to us

    Thursday, March 1, 2012 5:55 AM
    Moderator