简体   繁体   中英

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

I have created simple Template (Open word file--> Alt + F11 --> Save file as .dtom) to add comment to the selected text. I have save file as .dotm and place it on Start up folder C:\\Users\\abc\\AppData\\Roaming\\Microsoft\\Word\\STARTUP But I am getting error of Macro Setting of Ms Word 2013. I have followed as they suggested but still can't work.

I have attached my code. Can anybody suggest if I am missing anything from code side?

Code:

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

在此处输入图片说明

If you want to use it the way you're setup right now, you can continue to use multiple global variables

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

Have you tried:

  1. click the File tab, click Options, click Trust Center, and then click Trust Center settings.

  2. Click Macro settings.

  3. Under Macro settings, click Enable all macros.

Can you add C:\\Users\\abc\\AppData\\Roaming\\Microsoft\\Word\\STARTUP to Trusted Locations?

It is solved. I just change the way of passing parameter of function on button click. I don't know why it was not generating error but it was done by just one change.

Here is working code: (I still don't know how to pass multiple parameters)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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