我正在使用互操作,我想获取包含在 word 文档中的所有内容控件的列表(在正文、形状、页眉、页脚中......)。这是正确和最好的方法吗:
public static List<ContentControl> GetAllContentControls(Document wordDocument)
{
if (null == wordDocument)
throw new ArgumentNullException("wordDocument");
List<ContentControl> ccList = new List<ContentControl>(); ;
// Body cc
var inBodyCc = (from r in wordDocument.ContentControls.Cast<ContentControl>()
select r);
ccList.AddRange(inBodyCc);
// cc within shapes
foreach (Shape shape in wordDocument.Shapes)
{
if (shape.Type == Microsoft.Office.Core.MsoShapeType.msoTextBox)
{
ccList.AddRange(WordDocumentHelper.GetContentControlsInRange(shape.TextFrame.TextRange));
}
}
// Get the list of cc in the story ranges : wdFirstPageHeaderStory, wdFirstPageFooterStory, wdTextFrameStory (textbox)...
foreach (Range range in wordDocument.StoryRanges)
{
ccList.AddRange(WordDocumentHelper.GetContentControlsInRange(range));
}
return ccList;
}
public static List<ContentControl> GetContentControlsInRange(Range range)
{
if (null == range)
throw new ArgumentNullException("range");
List<ContentControl> returnValue = new List<ContentControl>();
foreach (ContentControl cc in range.ContentControls)
{
returnValue.Add(cc);
}
return returnValue;
}
问候。