none
vba Word: How to add selected text into right click menu, or add right clicked word in menu RRS feed

  • Question

  • I also have related question with above post

    every thing is working fine but i want to add Selected text into right click menu as a .caption

    here is my code

    Public Sub CreateMacro()
    Application.CommandBars("Text").Reset
    
        Dim MenuButton As CommandBarButton
        With CommandBars("Text")
            Set MenuButton = .Controls.Add(msoControlButton)
            
            
            With MenuButton
                .Caption = Selection.Text
                
                
                
                
                .Style = msoButtonCaption
                .OnAction = "Test_Macro"
            End With
        End With
    
        
    End Sub
    
    Public Sub Test_Macro()
        MsgBox "I work"
        ResetRightClick
        CreateMacro
    End Sub
    
    Sub ResetRightClick()
        
        Application.CommandBars("Text").Reset
             
    End Sub

    on first attempt right click menu is displayed but empty caption, and on next times it show older caption (not current selection) until the menu item is clicked

    or how to add Right clicked word into right clicked menu

    Wednesday, October 10, 2018 6:51 AM

All replies

  • Hi Fahad,

    >> on first attempt right click menu is displayed but empty caption, and on next times it show older caption (not current selection) until the menu item is clicked.

    Base on my test, it show me current selection even the menu item is not cliked.

    Please take a look at the gif that i recorded:

    Best Regards,

    Yuki


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread.

    Thursday, October 11, 2018 2:41 AM
    Moderator
  • but here i m facing same problem, what did u do with it ? it is not showing current selection
    Thursday, October 11, 2018 4:37 AM
  • Hi Fahad,

    When i select "111" and run the code, the right clicked menu shows me "111".

    And then i select "222" and run the code, the right clicked menu shows me "222".

    So i think it is showing current selection, right?

    Or what result do you want to achieve?

    Looking forward to hearing from you.

    Best Regards,

    Yuki


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread.

    Thursday, October 11, 2018 4:47 AM
    Moderator
  • it seems you run macro each time again for new right click, i want to run macro once and appear word in right click menu each time current thanks
    Thursday, October 11, 2018 5:12 AM
  • Hi Fahad,

    I'm so sorry for the late reply.

    We've researching on your issue, but it seems that there's no better way to do that.

    Thanks for your understanding.

    Best Regards,

    Yuki


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread.

    Wednesday, October 24, 2018 6:35 AM
    Moderator