Word文档中所有标题的字体
我正在尝试从Word文档中检索所有标题的字体名称和大小。 知道如何获得它吗?
I am trying to retrieve the font name and size of all the headings from a word document. Any idea how to get it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
基本结构如下所示:
问题是,如果同一行上有不同的字体和大小,则不会感觉到。 如果可能的话,您将需要在段落循环内添加另一个带有
For Each singleCharacter In singleLine.Range.Characters
的循环。编辑:一个更棘手的问题是收集数据后如何处理这些数据。 构建数组似乎是很自然的事情,但 VBA 数组几乎毫无用处,因为像 .append() 这样的基本方法需要您重新调整整个数组。 有关详细信息,请参阅 http://www.cpearson.com/excel/VBAArrays.htm如果您想走那条路,请参阅信息。
The basic structure will be something like below:
The catch will be that this won't sense if there are different fonts and sizes on the same line. If that's a possibility, you will need to add another loop with
For Each singleCharacter In singleLine.Range.Characters
inside of the paragraphs loop.Edit: A trickier problem is what to do with this data once you've collected it. Building up an array seems like the natural fit, but VBA arrays are borderline useless, since basic methods like .append() require you to redim the whole array. See http://www.cpearson.com/excel/VBAArrays.htm for more info if you would like to go down that road.
最直接的解决方案是在 Word 中打开文档并访问对象模型。 传统上,这是使用 VBA 完成的,但您也可以通过使用 VSTO(Visual Studio Tools for Office)来使用 .NET(例如 C# 和 VB.NET)。 我个人认为 C#/VB.NET 比 VBA 更好。
一旦您有权访问对象模型,您将必须枚举文档中的段落。 当您找到标题(可能由样式定义)时,您必须弄清楚标题的格式。
The most straight forward solution is to open the document in Word and access the object model. This is traditionally done using VBA, but you can also use .NET (e.g. C# og VB.NET) by using VSTO (Visual Studio Tools for Office). Personally I find C#/VB.NET much better languages than VBA.
Once you have access to the object model you will have to enumerate paragraphs in the document. When you find a heading (perhaps defined by the style) you will have to figure out the formatting of the heading.
这是我从 MSDN 的简要浏览中得到的结果“HeadingStyles”页面:
This is what I got from a brief skim of the MSDN page on "HeadingStyles":