有没有一种方法可以使用VSTO查找WordDocument的所有ContentControl(包括页眉,页脚,文本框等中的ContentControl)?

Microsoft.Office.Tools.Word.Document.ContentContols仅返回主文档的ContentControl,而不返回Headers / Footer内部的ContentControl。

最佳答案

http://social.msdn.microsoft.com/Forums/is/vsto/thread/0eb0af6f-17db-4f98-bc66-155db691fd70复制

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);
            }
            foreach (Shape shapeRange in rangeStory.ShapeRange)
            {
              foreach (ContentControl cc in shapeRange.TextFrame.TextRange.ContentControls)
              {
                ccList.Add(cc);
              }
            }
          }
          catch (COMException) { }
          rangeStory = rangeStory.NextStoryRange;

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

关于c# - VSTO查找Word文档的ContentControls,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/1571292/

10-12 22:20