繁体   English   中英

如何在Word 2013中使用VBA为选定的文本添加注释?

[英]How to add comment for selected text using VBA in Ms Word 2013?

我已经创建了简单的模板(Open word file--> Alt + F11 --> Save file as .dtom)以向选定的文本添加注释。 我将文件另存为.dotm并将其放置在启动文件夹C:\\Users\\abc\\AppData\\Roaming\\Microsoft\\Word\\STARTUP但是出现Word 2013女士宏设置错误。我按照他们的建议进行操作,但是仍然无法工作。

我已附上我的代码。 有人可以建议我在代码方面是否缺少任何东西?

码:

Sub autoexe()
    Dim MainMenu As CommandBarControl
    Dim MenuItem As CommandBarPopup
    'add pop button
    MenuItem = MainMenu.Controls.Add(msoControlPopup, , , , True)
    With MenuItem
        .Caption = "Item1"
        .Visible = True
        'add simple button
        Dim simpleButton As CommandBarButton
        Dim commentText As String
        commentText = "Comment inserted successfully"
        simpleButton = MenuItem.Controls.Add(msoControlButton, , , , True)
        With simpleButton
            .Caption = "Show Message"
            .Visible = True
            .OnAction = "addComments(commentText)"
        End With
    End With
End Sub 


Sub addComments(ByVal cmtText As String)
    ActiveWindow.View.Type = wdPageView
    Selection.Comments.Add Range:=Selection.Range
    If (Len(Selection) > 0) Then
        MsgBox ("inside comment")
        With Selection
            .TypeText (cmtText)
        End With
    End If
End Sub

在此处输入图片说明

如果要以现在设置的方式使用它,则可以继续使用多个全局变量

Dim commentText As String
Dim param2 As String
Dim param3 As String

commentText = "Comment inserted successfully"
param2 = "This is parameter 2"
param3 = "This is parameter 3"
simpleButton = MenuItem.Controls.Add(msoControlButton, , , , True)
With simpleButton
    .Caption = "Show Message"
    .Visible = True
    .OnAction = "addComments()"
End With

Sub addComments()
    commentText = Application.CommandBars.ActionControl.Parameter
    ActiveWindow.View.Type = wdPageView
    Selection.Comments.Add Range:=Selection.Range
    If (Len(Selection) > 0) Then
        MsgBox ("inside comment")
        With Selection
            .TypeText (commentText)
        End With
    End If
    Msgbox "Param 2: " & param2
    Msgbox "Param 3: " & param3
End Sub

你有没有尝试过:

  1. 单击文件选项卡,单击选项,单击信任中心,然后单击信任中心设置。

  2. 单击宏设置。

  3. 在宏设置下,单击启用所有宏。

您可以将C:\\Users\\abc\\AppData\\Roaming\\Microsoft\\Word\\STARTUP到受信任位置吗?

解决了。 我只是更改按钮单击时传递函数参数的方式。 我不知道为什么它没有产生错误,但是仅需更改一次即可完成。

这是工作代码:(我仍然不知道如何传递多个参数)

Dim commentText As String
        commentText = "Comment inserted successfully"
        simpleButton = MenuItem.Controls.Add(msoControlButton, , , , True)
        With simpleButton
            .Caption = "Show Message"
            .Visible = True
            .OnAction = "addComments()"
            .Parameter = commentText 
        End With

Sub addComments()
    Dim commentText As String
    commentText = Application.CommandBars.ActionControl.Parameter
    ActiveWindow.View.Type = wdPageView
    Selection.Comments.Add Range:=Selection.Range
    If (Len(Selection) > 0) Then
        MsgBox ("inside comment")
        With Selection
            .TypeText (commentText)
        End With
    End If
End Sub

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM