vba word 添加评论和作者

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4935031/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 12:38:01  来源:igfitidea点击:

vba word add comment and author

vbams-wordcommentsauthor

提问by user366121

I have a user form in Word 2007 which searches for specific terms in a document and adds comments. I have three different categories for these comments. I want the comments to be color coded for each category. For the moment I have a solution which works but it is very slow. Is there another way to assign a comment author directly when creating the comments?

我在 Word 2007 中有一个用户表单,用于搜索文档中的特定术语并添加注释。我将这些评论分为三个不同的类别。我希望对每个类别的评论进行颜色编码。目前我有一个可行的解决方案,但速度很慢。创建评论时是否有另一种方法可以直接指定评论作者?

Code for comment creation:

创建评论的代码:

For i = 0 To UBound(CritArray)
    PosCount = 1
    With Selection
    .HomeKey wdStory
        With .Find
        .ClearFormatting
            Do While .Execute(FindText:=CritArray(i), _
            Forward:=True, _
            MatchWholeWord:=True)
Select Case i
...
End Select
            PosCount = PosCount + 1

            Selection.Comments.Add _
            Range:=Selection.Range, _
            Text:=MessArray(i) & CritArray(i) & "' - found for the" & Str(FoundCount) & ". time"

            Loop

        End With
    End With
Next

Code for assigning a different author to each comment - this results in different color coded comments if under Review>Track Changes>Track Changes Options>Comments by author is selected:

为每条评论分配不同作者的代码 - 如果在 Review>Track Changes>Track Changes Options>Comments by author 下选择了不同的颜色编码的评论:

Dim CurrentExpField As String

For Each objCom In ActiveDocument.Comments

    CurrentExpField = Left$(objCom.Range.Text, 3)
    objCom.Author = UCase(CurrentExpField)
    objCom.Initial = UCase(CurrentExpField)

Next

采纳答案by i_saw_drones

Yes, it is possible to set additional properties for a Commentafter it is created since the Addmethod for Comments returns a reference to a new Commentobject. This means that you can do your colour-coding in one pass. I modified your code slightly to do this as follows:

是的,Comment因为AddComments的方法返回对新Comment对象的引用,所以可以在创建之后为其设置其他属性。这意味着您可以一次性完成颜色编码。我稍微修改了您的代码以执行以下操作:

Dim cmtMyComment as Comment

For i = 0 To UBound(CritArray)
    PosCount = 1
    With Selection
    .HomeKey wdStory
        With .Find
        .ClearFormatting
            Do While .Execute(FindText:=CritArray(i), _
            Forward:=True, _
            MatchWholeWord:=True)
Select Case i
...
End Select
            PosCount = PosCount + 1

            Set cmtMyComment = Selection.Comments.Add(Range:=Selection.Range, _
            Text:=MessArray(i) & CritArray(i) & "' - found for the" & Str(FoundCount) & ". time")

            cmtMyComment.Author = UCase(Left$(cmtMyComment.Range.Text, 3))
            cmtMyComment.Initial = UCase(Left$(cmtMyComment.Range.Text, 3))

            Loop

        End With
    End With
Next