Word文档中所有标题的字体

发布于 2024-07-30 09:52:40 字数 46 浏览 0 评论 0原文

我正在尝试从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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

吹梦到西洲 2024-08-06 09:52:40

基本结构如下所示:

Public Sub ShowFontAndSize()
    Dim singleLine As Paragraph
    Dim lineText As String

    For Each singleLine In ActiveDocument.Paragraphs
     Debug.Print singleLine.Range.Font.Name
     Debug.Print singleLine.Range.Font.Size
    Next singleLine
End Sub

问题是,如果同一行上有不同的字体和大小,则不会感觉到。 如果可能的话,您将需要在段落循环内添加另一个带有 For Each singleCharacter In singleLine.Range.Characters 的循环。

编辑:一个更棘手的问题是收集数据后如何处理这些数据。 构建数组似乎是很自然的事情,但 VBA 数组几乎毫无用处,因为像 .append() 这样的基本方法需要您重新调整整个数组。 有关详细信息,请参阅 http://www.cpearson.com/excel/VBAArrays.htm如果您想走那条路,请参阅信息。

The basic structure will be something like below:

Public Sub ShowFontAndSize()
    Dim singleLine As Paragraph
    Dim lineText As String

    For Each singleLine In ActiveDocument.Paragraphs
     Debug.Print singleLine.Range.Font.Name
     Debug.Print singleLine.Range.Font.Size
    Next singleLine
End Sub

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.

忘你却要生生世世 2024-08-06 09:52:40

最直接的解决方案是在 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.

仅此而已 2024-08-06 09:52:40

这是我从 MSDN 的简要浏览中得到的结果“HeadingStyles”页面

MsgBox ActiveDocument.HeadingStyles(1).Style

This is what I got from a brief skim of the MSDN page on "HeadingStyles":

MsgBox ActiveDocument.HeadingStyles(1).Style
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文