有没有一种方法可以使用VSTO查找WordDocument的所有ContentControl(包括Header,Footers,TextBoxes中的ContentControls)?VSTO找到Word文档的ContentControls
Microsoft.Office.Tools.Word.Document.ContentContols只返回Main-Document的ContentControls,而不是头/页脚内的ContentControls。
有没有一种方法可以使用VSTO查找WordDocument的所有ContentControl(包括Header,Footers,TextBoxes中的ContentControls)?VSTO找到Word文档的ContentControls
Microsoft.Office.Tools.Word.Document.ContentContols只返回Main-Document的ContentControls,而不是头/页脚内的ContentControls。
试试这个:
foreach (Word.ContentControl contentcontrol in this.Application.ActiveDocument.ContentControls)
{
//Some action on all contentcontrol objects
}
如果没有在文档的StoryRanges
我处理同样的问题,但驱动字工作尝试遍历所有范围(contentcontrols)来自MATLAB。此页由字MVP解决了这个问题对我来说:
http://www.word.mvps.org/FAQs/MacrosVBA/FindReplaceAllWithVBA.htm
从本质上讲,你必须:
从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;
}
复制