确定 Word 注释的当前标题文本和编号 [英] Determine current heading text and number for a Word comment

查看:54
本文介绍了确定 Word 注释的当前标题文本和编号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

上下文:通过 VBA 或 VSTO 进行 Microsoft Word 编程.

Context: Microsoft Word programming via VBA or VSTO.

Word 文档对象的 Comments 属性允许枚举 Word 文档中的所有注释.

The Comments property of a Word Document object allows enumerating over all comments in a Word document.

如何找到 Word 评论的当前标题?

How can you find the current heading for a Word comment?

示例文档:

标题 1

标题 1.1

(评论 A)

输出:评论 A - 标题 1.1

Output: comment A - Heading 1.1

推荐答案

我找不到更简单的方法,但它确实有效.以下代码在活动文档中的第一个评论之前搜索最后一个标题.您可以使用 For Each 循环 轻松地将它用于所有注释.

I couldn't find easier way of doing it but it's working. The following code is searching for last heading before first comment in active document. You can easily adopt it for all comments using For Each loop.

Sub Heading_Above_Comment()

    Dim COMM As Comment
    Set COMM = ActiveDocument.Comments(1)

    'set new selection for range to search
    Dim rngWHERE As Range
    Set rngWHERE = ActiveDocument.Range(0, COMM.Reference.Start)
    rngWHERE.Select

    Selection.Find.ClearFormatting

    'set heading style name you applied>>
    Selection.Find.Style = ActiveDocument.Styles("Nagłówek 1")

    With Selection.Find
        .Text = ""
        .Replacement.Text = ""
        .Forward = False
        .Wrap = wdFindContinue
        .Format = True
    End With

    Do While Selection.Find.Execute
        If Selection.End < COMM.Reference.Start And _
            Selection.Start > rngWHERE.Start Then
            Set rngWHERE = Selection.Range
        Else
            Exit Do
        End If
    Loop

    'select the range
    rngWHERE.Select
    'range selected is last heading
    MsgBox "last heading befor comment is selected and it is: " & Selection.Text
End Sub

工作原理:

这篇关于确定 Word 注释的当前标题文本和编号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆