C# VSF用于查找Word文档的内容控件
有没有办法使用VSTO查找WordDocument的所有ContentControl(包括页眉、页脚、文本框中的ContentControl…) Microsoft.Office.Tools.Word.Document.ContentControl仅返回主文档的ContentControl,而不返回页眉/页脚中的ContentControl。尝试以下操作:C# VSF用于查找Word文档的内容控件,c#,ms-word,vsto,C#,Ms Word,Vsto,有没有办法使用VSTO查找WordDocument的所有ContentControl(包括页眉、页脚、文本框中的ContentControl…) Microsoft.Office.Tools.Word.Document.ContentControl仅返回主文档的ContentControl,而不返回页眉/页脚中的ContentControl。尝试以下操作: foreach (Word.ContentControl contentcontrol in this.Application.Active
foreach (Word.ContentControl contentcontrol in this.Application.ActiveDocument.ContentControls)
{
//Some action on all contentcontrol objects
}
如果这不起作用,试着在文档的中的所有范围(对于contentcontrols)上迭代,我正在处理相同的问题,但要从MATLAB中驱动Word。这个页面由一个单词MVP为我解决了这个问题: 基本上,你必须:
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;
}