繁体   English   中英

如何获取文档中所有内容控件的列表?

[英]How to get the list of all content controls in the document?

我正在使用互操作,我想得到word文档中包含的所有内容控件的列表(在正文,形状,页眉,页脚..)。 这是正确的,也是最好的方法:

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;
}

问候。

这是一个更简单的方法(VBA,但可以移植到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

是Lars Holm,你是对的,页眉和页脚中的文本框内的内容控件丢失了,这是完整的解决方案:

/// <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;
}

问候

这是执行此操作的正确方法:

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;
}

问候

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM