none
Gallery Dropdownlist disappears after adding new content control to document RRS feed

  • Question

  • I have been struggling with programatically inserting building block galleries that may contain text with bookmarks.  As you know, the challenge is when the new gallery must be inserted between existing galleries or existing bookmarks.   I have it working, kind of.  However when I insert a building block gallery after an existing building block gallery, the previously existing gallery prior to the insertion point ceases to exist and leaves only the content behind like when setting temporary=true;  changing the temporary property doesn't seem to have an affect. (I actually prefer the temporary setting)  The strange thing is that if I insert at the end of the document, the prior gallery object doesn't disappear.   Functionally I can use temporary bb galleries but it has to be consistent.  Setting the temporary property to true when I create them doesn't make the bb galleries temporary.

    Here is the code I'm using to find the insertion range and insert the BB Galleries.

    Thanks in advance

     

      public void btnInsertGallery_Click(object sender, RibbonControlEventArgs e)
      {
       RibbonControl s = (RibbonControl) sender;
       Word.Range r = null;
       Word.ContentControl cc = null;
       Word.Bookmark bmk = null;
    
       Object paramValue = 0;  //generic object for VSTO Word API calls
    
       //find the range object where new content control should be inserted
       //after the closest previous content control or bookmark
       try
       {
        //get the bookmarkid of the current selection if exists
        if (this.Application.ActiveWindow.Selection.Range.BookmarkID > 0)
        {
         paramValue = this.Application.ActiveWindow.Selection.Range.BookmarkID;
         bmk = this.Application.ActiveDocument.Bookmarks.get_Item(ref paramValue);
        }
    
        //get the outermost content control
        cc = this.Application.Selection.ParentContentControl;
        if (cc != null)
        {
         while (cc.ParentContentControl != null)
         {
          cc = cc.ParentContentControl;
         }
        }
    
        if (cc == null && bmk == null)    // add control at current selection
         r = this.Application.ActiveWindow.Selection.Range;
        else if (cc == null && bmk != null)   // selection is in bookmark - add after bookmark
        {
         r = this.Application.ActiveDocument.Bookmarks.get_Item(ref paramValue).Range.Duplicate;
         r.InsertParagraphAfter();
         r.Start = r.End;
         //paramValue = -1;
         //object oUnit = Word.WdUnits.wdCharacter;
         //bmk.Range.MoveEnd(ref oUnit, ref paramValue);
        }
        else if (cc != null && bmk == null)   // selection is in content control but not bookmark - add after control
        {
         r = cc.Range.Duplicate;
         r.InsertParagraphAfter();
         r.Start = r.End;
         //paramValue = -1;
         //object oUnit = Word.WdUnits.wdCharacter;
         //cc.Range.MoveEnd(ref oUnit, ref paramValue);
        }
        else if (cc != null && bmk != null)   // selection is contained inside both - find the superior and add after control
        {
         if (bmk.Range.Start <= cc.Range.Start)
         {
          r = this.Application.ActiveDocument.Bookmarks.get_Item(ref paramValue).Range.Duplicate;
          r.InsertParagraphAfter();
          r.Start = r.End;
          //paramValue = -1;
          //object oUnit = Word.WdUnits.wdCharacter;
          //bmk.Range.MoveEnd(ref oUnit, ref paramValue);
         }
         else
         {
          r = cc.Range.Duplicate;
          r.InsertParagraphAfter();
          r.Start = r.End;
          //paramValue = -1;
          //object oUnit = Word.WdUnits.wdCharacter;
          //cc.Range.MoveEnd(ref oUnit, ref paramValue);
         }
        }
        else          // default get the current selection range and put it in R
        {     
         r = this.Application.ActiveWindow.Selection.Range.Duplicate;
        }
    
        // safety catch, make sure we haven't somehowe bounced beyond the end of the document
        if (r.Start > this.Application.ActiveDocument.Content.End)
        {
         r.Start = this.Application.ActiveDocument.Content.End;
        }
       }
       catch (Exception ex)
       {
        s_log.DebugFormat("Error Positioning to bookmark {0}", ex.Message);
        return;
       }
    
       try
       {
        paramValue = r;
        Word.ContentControl bbc = this.Application.ActiveDocument.ContentControls.Add(Microsoft.Office.Interop.Word.WdContentControlType.wdContentControlBuildingBlockGallery, ref paramValue);
        
        switch (s.Tag.ToString())
        {
         case "HEADINGS" : //Properties.Resources.HEADINGS;
          bbc.BuildingBlockType = Word.WdBuildingBlockTypes.wdTypeCustom2;
          break;
         case "NOTES": //Properties.Resources.NOTES;
          bbc.BuildingBlockType = Word.WdBuildingBlockTypes.wdTypeCustom3;
          break;
         default :
          bbc.BuildingBlockType = Word.WdBuildingBlockTypes.wdTypeCustom2;
          break;
        }
    
        bbc.BuildingBlockCategory = ObjectModel.Locale.ToString();
        bbc.Temporary = false;
        bbc.LockContentControl = false;
        bbc.LockContents = false;
        bbc.Title = "Choose one";
        bbc.Tag = s.Tag.ToString();
    
       }
       catch (Exception ex)
       {
        s_log.DebugFormat("Failed inserting content control{0}", ex.Message);
        return;
       }
       
      }
    
    


    Monday, August 15, 2011 5:27 PM

Answers

  • Hi Eddie,

     

    Would you please take a look this snippet and to see whether it can help you solve your issue.

     

    object r = null;

                Globals.ThisAddIn.Application.ActiveDocument.Sections[1].Range.Collapse();

                r = Globals.ThisAddIn.Application.ActiveDocument.Sections[1].Range;

                Word.ContentControl cc = Globals.ThisAddIn.Application.ActiveDocument.ContentControls.Add(Word.WdContentControlType.wdContentControlBuildingBlockGallery,ref r);

     

    Have a good day,

     

    Tom


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, August 18, 2011 9:36 AM
    Moderator

All replies

  • Hi Eddie,

     

    Thanks for posting in the MSDN Forum.

     

    Would you please tell me some questions:

     

    1.         Is your Office version 2007 or 2010?

    2.         Is your snippet in a VSTO add-in project or Share add-in project?

     

    Have a good day,

     

    Tom


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, August 17, 2011 2:17 AM
    Moderator
  • I'm using office 2010 but it needs to work in 2007 too.  This is an addin.

     

    Eddie

    Wednesday, August 17, 2011 2:51 AM
  • Hello,

     

    Before inserting, make a check if r.ContentControls.Count==0. Make sure that you are inserting in a range without controls. And another thing, why not use r.ContentControls.Add(..., ref missing); instead of doc.ContentControls.Add(..., ref r);

     

    Hope this helps,

    Silviu.


    http://www.rosoftlab.net/
    Wednesday, August 17, 2011 5:19 AM
  • Didn't use the range.contentcontrols.add because I didn't think about it.  Good catch.  Not sure it will fix my issue but certainly a plausible change to make.  Thanks for the suggestion.

     

    Eddie

    Wednesday, August 17, 2011 3:49 PM
  • Hi Eddie,

     

    Would you please take a look this snippet and to see whether it can help you solve your issue.

     

    object r = null;

                Globals.ThisAddIn.Application.ActiveDocument.Sections[1].Range.Collapse();

                r = Globals.ThisAddIn.Application.ActiveDocument.Sections[1].Range;

                Word.ContentControl cc = Globals.ThisAddIn.Application.ActiveDocument.ContentControls.Add(Word.WdContentControlType.wdContentControlBuildingBlockGallery,ref r);

     

    Have a good day,

     

    Tom


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, August 18, 2011 9:36 AM
    Moderator