How to get the list of all content controls in the

2019-04-12 04:02发布

I am using interop and I want to get the list of all content controls contained in word document (in the body, shapes, header, footer..). Is this the correct and the best way to do this :

public static List<ContentControl> GetAllContentControls(Document wordDocument)
{
  if (null == wordDocument)
    throw new ArgumentNullException("wordDocument");

  List<ContentControl> ccList = new List<ContentControl>(); ;
  // Body cc
  var inBodyCc = (from r in wordDocument.ContentControls.Cast<ContentControl>()
          select r);
  ccList.AddRange(inBodyCc);

  // cc within shapes
  foreach (Shape shape in wordDocument.Shapes)
  {
    if (shape.Type == Microsoft.Office.Core.MsoShapeType.msoTextBox)
    {
      ccList.AddRange(WordDocumentHelper.GetContentControlsInRange(shape.TextFrame.TextRange));
    }
  }

  // Get the list of cc in the story ranges : wdFirstPageHeaderStory, wdFirstPageFooterStory, wdTextFrameStory (textbox)... 
  foreach (Range range in wordDocument.StoryRanges)
  {
    ccList.AddRange(WordDocumentHelper.GetContentControlsInRange(range));
  }
  return ccList;
}

public static List<ContentControl> GetContentControlsInRange(Range range)
{
  if (null == range)
    throw new ArgumentNullException("range");

  List<ContentControl> returnValue = new List<ContentControl>();

  foreach (ContentControl cc in range.ContentControls)
  {
    returnValue.Add(cc);
  }

  return returnValue;
}

Regards.

3条回答
不美不萌又怎样
2楼-- · 2019-04-12 04:12

Here is the correct way to do this :

public static List<ContentControl> GetAllContentControls(Document wordDocument)
{
  if (null == wordDocument)
    throw new ArgumentNullException("wordDocument");

  List<ContentControl> ccList = new List<ContentControl>();

  // The code below search content controls in all
  // word document stories see http://word.mvps.org/faqs/customization/ReplaceAnywhere.htm
  Range rangeStory;
  foreach (Range range in wordDocument.StoryRanges)
  {
    rangeStory = range;
    do
    {
      try
      {
        foreach (ContentControl cc in rangeStory .ContentControls)
  {
    ccList .Add(cc);
  }
      }
      catch (COMException) { }
      rangeStory = rangeStory.NextStoryRange;

    }
    while (rangeStory != null);
  }
  return ccList;
}

Regards

查看更多
ら.Afraid
3楼-- · 2019-04-12 04:17

Here's a much shorter way of going about it (VBA, but can be ported to C#):

Sub GetCCs()
    Dim d As Document
    Set d = ActiveDocument
    Dim cc As ContentControl
    Dim sr As Range
    Dim srs As StoryRanges
    For Each sr In d.StoryRanges
        For Each cc In sr.ContentControls
            ''# do your thing
        Next
    Next
End Sub
查看更多
做个烂人
4楼-- · 2019-04-12 04:32

Yes Lars Holm,you are right, the content controls inside text boxs in header and footer are missing , here is the complete solution for this:

/// <summary>
/// Get all content controls contained in the document.
/// </summary>
/// <param name="wordDocument"></param>
/// <returns></returns>
public static List<ContentControl> GetAllContentControls(Document wordDocument)
{
    if (null == wordDocument)
        throw new ArgumentNullException("wordDocument");

    List<ContentControl> ccList = new List<ContentControl>();

    // The code below search content controls in all
    // word document stories see http://word.mvps.org/faqs/customization/ReplaceAnywhere.htm
    Range rangeStory;
    foreach (Range range in wordDocument.StoryRanges)
    {
        rangeStory = range;
        do
        {
            try
            {
                foreach (ContentControl cc in range.ContentControls)
                {
                    ccList.Add(cc);
                }

                // Get the content controls in the shapes ranges
                foreach (Shape shape in range.ShapeRange)
                {
                    foreach (ContentControl cc in shape.TextFrame.TextRange.ContentControls)
                    {
                        ccList.Add(cc);
                    }

                }
            }
            catch (COMException) { }
            rangeStory = rangeStory.NextStoryRange;

        }
        while (rangeStory != null);
    }
    return ccList;
}

Regards

查看更多
登录 后发表回答