有没有办法使用VSTO查找WordDocument的所有ContentControls(包括页眉,页脚,文本框中的ContentControl)?
Microsoft.Office.Tools.Word.Document.ContentContols仅返回Main-Document的ContentControls,而不返回页眉/页脚内的ContentControl。
答案 0 :(得分: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;
}
答案 1 :(得分:0)
试试这个:
foreach (Word.ContentControl contentcontrol in this.Application.ActiveDocument.ContentControls)
{
//Some action on all contentcontrol objects
}
如果这不起作用,请尝试迭代文档StoryRanges
中的所有范围(对于contentcontrols)答案 2 :(得分:0)
我正在处理同样的问题,但是从MATLAB驱动Word。 Word MVP的这个页面为我解决了这个问题:
http://www.word.mvps.org/FAQs/MacrosVBA/FindReplaceAllWithVBA.htm
基本上,你必须: