如何使用vba word在鼠标右键单击事件中添加自定义菜单选项? [英] How to add custom menu option in mouse right click event using vba word?
问题描述
我是视觉基础知识的新手。我有一个要求,比如当在文档中选择一些单词并点击右键时,应该调用宏函数。或类似的东西,必须在右键单击鼠标事件中创建自定义右键菜单选项,当我单击该菜单选项时,应调用宏。
例如:
在文档中,我有四位数字,如2001年和2010年。当我选择四位数字并点击右键并按我的自定义菜单选项时,宏应调用并选择四位数字。应该被解析为被调用的宏函数。
怎么做?我得到了一些代码但是它的excel,我不知道如何为微软词定制它
excel的代码:
< br $>
Const strMacro =YourCode
Sub CreateMacro()
Dim cBut
Call KillMacro
Set cBut = Application.CommandBars(Cell)。Controls.Add(Temporary:= True)
使用cBut
.Caption = strMacro
.Style = msoButtonCaption
.OnAction =Test_Macro
结束
结束子
子Test_Macro()
MsgBox我工作
结束子
Sub KillMacro( )
On Error Resume Next
Application.CommandBars(Cell)。Controls(strMacro).Delete
End Sub
MS Word与Excel不同。添加上下文菜单有点复杂,因为我们需要创建自定义类才能管理右键单击事件。
步骤:
- 转到VBACode编辑器( ALT + F11 )
- 插入新的类模块将其名称更改为:
cRightClickEventHandler
。复制并粘贴代码:
公共 WithEvents oApp 作为应用程序
私有 Sub Class_Initialize()
设置 oApp =应用
结束 Sub
私有 Sub Class_Terminate()
设置 oApp = 没什么
结束 Sub
私有 < span class =code-keyword> Sub oApp_WindowBeforeRightClick( ByVal Sel As 选择,取消 As Boolean )
CreateCommand
Cancel = True
End Sub
- 插入新模块,复制粘贴代码:
< pre lang =vb> 选项 明确
公共 Const sCommName 作为 字符串 = 自定义
公开 cApp 作为 cRightClickEventHandler
Sub CreateCommand()
Dim cb As CommandBar
Dim ctr As CommandBarControl
开启 错误 GoTo Err_CreateCommand
KillCommand
设置 cb = Application.CommandBars.Add(sCommName,msoBarPopup)
设置 ctr = cb.Controls.Add(msoControlButton)
使用 ctr
.Caption = 让我们这样做!
.TooltipText = .Caption
.FaceId = 611
.OnAction = SayHello
结束 使用
cb.ShowPopup
Exit_CreateCommand :
开启 错误 恢复 下一步
设置 ctr = 无
设置 cb = Nothing
退出 Sub
Err_CreateCommand:
MsgBox Err.Description,vbExclamation,Err.Number
Resume Exit_CreateCommand
结束 Sub
Sub KillCommand()
On 错误 简历 下一步
Application.CommandBars(sCommName)。删除
结束 Sub
Sub SayHello()
MsgBox Selection.Text,vbInformation, Hello ...
结束 Sub
- 查找
ThisDocument
模块,复制并粘贴代码
选项 明确
私有 Sub Document_Close()
设置 cApp = Nothing
结束 Sub
私有 Sub Document_Open()
Set cApp = 新 cRightClickEventHandler
结束 Sub
- 保存文档并重新打开(不要忘记使用启用内容按钮)
- 玩得开心!
醇>
I'm a newbie in visual basic stuffs. I have a requirement like when some words are selected in the document and hit right click ,macro function should be called. Or something like,have to create custom right click menu option in the right click mouse event and when I click that menu option ,macro should be called.
For example:
In the document I have four digit numbers like 2001 and 2010. When I select four digit number and hit right click and press my custom menu option,macro should be called and selected four digit no. should be parsed to the macro function which is called.
How to do it?? I got some code but its for excel, I dont't know how to customize it for microsoft word
Code for excel:
Const strMacro = "YourCode" Sub CreateMacro() Dim cBut Call KillMacro Set cBut = Application.CommandBars("Cell").Controls.Add(Temporary:=True) With cBut .Caption = strMacro .Style = msoButtonCaption .OnAction = "Test_Macro" End With End Sub Sub Test_Macro() MsgBox "I work" End Sub Sub KillMacro() On Error Resume Next Application.CommandBars("Cell").Controls(strMacro).Delete End Sub
MS Word differs from Excel. Adding a context menu is a bit more complicated, because we need to create custom class to be able to "manage" right-click event.
Steps to do:
- Go to the VBACode editor (ALT+F11)
- Insert new class module and change its name to:
cRightClickEventHandler
. Copy and paste code:
Public WithEvents oApp As Application Private Sub Class_Initialize() Set oApp = Application End Sub Private Sub Class_Terminate() Set oApp = Nothing End Sub Private Sub oApp_WindowBeforeRightClick(ByVal Sel As Selection, Cancel As Boolean) CreateCommand Cancel = True End Sub- Insert new module, copy paste code:
Option Explicit Public Const sCommName As String = "Custom" Public cApp As cRightClickEventHandler Sub CreateCommand() Dim cb As CommandBar Dim ctr As CommandBarControl On Error GoTo Err_CreateCommand KillCommand Set cb = Application.CommandBars.Add(sCommName, msoBarPopup) Set ctr = cb.Controls.Add(msoControlButton) With ctr .Caption = "Let's do it!" .TooltipText = .Caption .FaceId = 611 .OnAction = "SayHello" End With cb.ShowPopup Exit_CreateCommand: On Error Resume Next Set ctr = Nothing Set cb = Nothing Exit Sub Err_CreateCommand: MsgBox Err.Description, vbExclamation, Err.Number Resume Exit_CreateCommand End Sub Sub KillCommand() On Error Resume Next Application.CommandBars(sCommName).Delete End Sub Sub SayHello() MsgBox Selection.Text, vbInformation, "Hello..." End Sub- Find
ThisDocument
module, copy and paste code
Option Explicit Private Sub Document_Close() Set cApp = Nothing End Sub Private Sub Document_Open() Set cApp = New cRightClickEventHandler End Sub- Save a document and reopen it (do not forget to use "Enable Content" button)
- Have a fun!
这篇关于如何使用vba word在鼠标右键单击事件中添加自定义菜单选项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!