2009-10-15 61 views
2

有没有一种方法可以使用VSTO查找WordDocument的所有ContentControl(包括Header,Footers,TextBoxes中的ContentControls)?VSTO找到Word文档的ContentControls

Microsoft.Office.Tools.Word.Document.ContentContols只返回Main-Document的ContentControls,而不是头/页脚内的ContentControls。

回答

0

试试这个:

foreach (Word.ContentControl contentcontrol in this.Application.ActiveDocument.ContentControls) 
{ 
    //Some action on all contentcontrol objects 
} 

如果没有在文档的StoryRanges

0

我处理同样的问题,但驱动字工作尝试遍历所有范围(contentcontrols)来自MATLAB。此页由字MVP解决了这个问题对我来说:

http://www.word.mvps.org/FAQs/MacrosVBA/FindReplaceAllWithVBA.htm

从本质上讲,你必须:

  1. 遍历所有Document.StoryRanges让每个故事类型的第一个范围。
  2. 在每个范围内,在range.ContentControls上执行您的工作。
  3. range = range.NextStoryRange。
  4. 重复2-4,直到范围为空。
3

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