[英]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. 我已经创建了简单的模板(Open word file--> Alt + F11 --> Save file as .dtom)
以向选定的文本添加注释。 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. 我将文件另存为.dotm并将其放置在启动文件夹C:\\Users\\abc\\AppData\\Roaming\\Microsoft\\Word\\STARTUP
但是出现Word 2013女士宏设置错误。我按照他们的建议进行操作,但是仍然无法工作。
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: 你有没有尝试过:
click the File tab, click Options, click Trust Center, and then click Trust Center settings. 单击文件选项卡,单击选项,单击信任中心,然后单击信任中心设置。
Click Macro settings. 单击宏设置。
Under Macro settings, click Enable all macros. 在宏设置下,单击启用所有宏。
Can you add C:\\Users\\abc\\AppData\\Roaming\\Microsoft\\Word\\STARTUP
to Trusted Locations? 您可以将C:\\Users\\abc\\AppData\\Roaming\\Microsoft\\Word\\STARTUP
到受信任位置吗?
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.