如何获取文档中所有内容控件的列表?
我正在使用互操作,我想获取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;
}
问候。
I am using interop and I want to get the list of all content controls contained in word document (in the body, shapes, header, footer..). Is this the correct and the best way to do this :
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;
}
Regards.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是一种更简短的方法(VBA,但可以移植到 C#):
Here's a much shorter way of going about it (VBA, but can be ported to C#):
是的,拉尔斯·霍尔姆,你是对的,页眉和页脚文本框中的内容控件丢失了,这是完整的解决方案:
问候
Yes Lars Holm,you are right, the content controls inside text boxs in header and footer are missing , here is the complete solution for this:
Regards
这是执行此操作的正确方法:
问候
Here is the correct way to do this :
Regards