Click here to Skip to main content
15,890,123 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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

Posted
Updated 14-Dec-14 22:58pm
v2
Comments
Maciej Los 15-Dec-14 2:24am    
Not clear. Please, explain with details. What's an issue?
What version of MS Word (2000, 2003, 2010, 2013)?
Vidhya Raju 15-Dec-14 3:26am    
MSword 2007. How to add a custom menu option in the right click mouse event?
Maciej Los 15-Dec-14 3:24am    
Use "Reply" widget.

1 solution

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:

  1. Go to the VBACode editor (ALT+F11)
  2. Insert new class module and change its name to: cRightClickEventHandler. Copy and paste code:
    VB
    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
  3. Insert new module, copy paste code:
    VB
    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
  4. Find ThisDocument module, copy and paste code
    VB
    Option Explicit
    
    Private Sub Document_Close()
    Set cApp = Nothing
    End Sub
    
    Private Sub Document_Open()
    Set cApp = New cRightClickEventHandler
    End Sub
  5. Save a document and reopen it (do not forget to use "Enable Content" button)
  6. Have a fun!
 
Share this answer
 
Comments
Vidhya Raju 15-Dec-14 5:35am    
Actually how to create (cRightClickEventHandler)class module and where this
(oApp_WindowBeforeRightClick) sub is called in the code? Am not clear to use this code.Can you tell it detaily?
Maciej Los 15-Dec-14 5:39am    
I'll promise to update my answer ASAP. In a meanwhile, you can download an example file.
Vidhya Raju 15-Dec-14 6:17am    
From the link you provided,I could understand the code structure and it works fine. Thanks you so much
Maciej Los 15-Dec-14 8:35am    
You're very welcome ;)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900