wordpress hit counter
Highlight a specific content control by ID programmatically - .Net - Development Tools - OpenXML Developer

Highlight a specific content control by ID programmatically

Development Tools

Discussions about working with Open XML using a wide range of development tools

Highlight a specific content control by ID programmatically

  • rated by 0 users
  • This post has 4 Replies |
  • 1 Follower
  • I am trying to highlight a content control in word 2007 header programmatically. I manage to do this in the main document, but the code can't find the content controls placed in the header.

    My code looks like this

    internal void markContentcontrolById(string contCtrlID)

    {

    Word.ContentControls contentControls = Globals.ThisAddIn.Application.ActiveDocument.H.ContentControls;

    foreach (Word.ContentControl contentControl in contentControls)

       {

          if (contentControl.ID == contCtrlID)

           {

              Globals.ThisAddIn.Application.Selection.SetRange(contentControl.Range.Start, contentControl.Range.End);

             Globals.ThisAddIn.Application.Selection.Select();

             break;

          }

          else

          {

             Globals.ThisAddIn.Application.Selection.SetRange(0, 0);

          }

        }

    }

    Does anyone have a suggestion on how to select content controls belonging to the header?

     

  • Hi,

    If your content control is placed in Header part, how can you search in main document?

    Seach in header part part of your document.

    Sheela

     

  •  

    I realize that I loop through content controls in the main document and that I need to do the same for the header, but I just can't figure out how to get hold of the content controls in the header.

    Can you help?

     

  • Hi,

    Are you using VSTO for this?

    If so, please check this line

    Globals.ThisAddIn.Application.ActiveDocument

    See what other menthods are available to get hold of header part.

    If this does not help, post this issue in VSTO/Word 2007 specific forum.

     

    Sheela

  • in C#      

    private IEnumerable<ContentControl> GetContentControls()

           {

               var document = Globals.ThisDocument.InnerObject;

               var result = new List<ContentControl>();

               if (document != null)

               {

                   Range rangeStory;

                   foreach (Range range in document.StoryRanges)

                   {

                       rangeStory = range;

                       do

                       {

                           try

                           {

                               foreach (ContentControl contentControl in rangeStory.ContentControls)

                               {

                                   result.Add(contentControl);

                               }

                           }

                           catch (COMException) { }

                           rangeStory = rangeStory.NextStoryRange;

                       }

                       while (rangeStory != null);

                   }

                   return result;

               }

               return null;

           }

Page 1 of 1 (5 items)