Hi everyone, I have this vba code that creates a menu bar in an Excel userform But I want to trigger actions when I click the sub menus.

John Peter

New Member
Joined
Apr 30, 2022
Messages
10
Office Version
  1. 365
Platform
  1. Windows
VBA Code:
Private Declare PtrSafe Function DestroyMenu Lib "user32" (ByVal hMenu As Long) As Long
Private Declare PtrSafe Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare PtrSafe Function CreatePopupMenu Lib "user32" () As Long
Private Declare PtrSafe Function DrawMenuBar Lib "user32" (ByVal hWnd As Long) As Long
Private Declare PtrSafe Function SetMenu Lib "user32" (ByVal hWnd As LongPtr, ByVal hMenu As LongPtr) As Long
Private Declare PtrSafe Function CreateMenu Lib "user32" () As Long
Private Declare PtrSafe Function AppendMenu Lib "user32" Alias "AppendMenuA" (ByVal hMenu As Long, ByVal wFlags As Long, ByVal wIDNewItem As Long, ByVal lpNewItem As Any) As Long
Const MF_STRING = &H0&: Const MF_POPUP = &H10&
Const MF_SEPARATOR = &H800&:  Const MF_GRAYED = &H1&
Dim hMenu, hMenu2, hMenu3, hMenu4 As Long
Dim hWnd As Long

Private Sub UserForm_Initialize()
    hWnd = FindWindow(vbNullString, Me.Caption)
    hMenu2 = CreatePopupMenu()
    hMenu3 = CreatePopupMenu()
    hMenu4 = CreatePopupMenu()
    hMenu = CreateMenu()
    SetMenu hWnd, hMenu
    AppendMenu hMenu, MF_POPUP, hMenu2, "More"
    AppendMenu hMenu2, MF_POPUP, 1, "Cut"
    AppendMenu hMenu2, MF_STRING, 2, "set"
    AppendMenu hMenu2, MF_STRING, 3, " Pdf"
    AppendMenu hMenu, MF_POPUP, hMenu3, "More"
    AppendMenu hMenu3, MF_STRING, 1, "Reset"
    AppendMenu hMenu3, MF_STRING, 11, "Op"
    AppendMenu hMenu3, MF_STRING, 4, "Paste"
    AppendMenu hMenu, MF_POPUP, hMenu4, "More2"
    AppendMenu hMenu4, MF_STRING, 3, "Reset"
    AppendMenu hMenu4, MF_STRING, 11, "Open"
    AppendMenu hMenu4, MF_SEPARATOR, 333, ByVal 0&
    AppendMenu hMenu4, MF_STRING, 3, "Reopen"
    AppendMenu hMenu4, MF_STRING, 11, "Open Pdf"
    DrawMenuBar hWnd
End Sub
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
Hi,
welcome to forum

have a look here: Adding a Menu system to UserForms
for a very comprehensive solution created by @Jaafar Tribak

Dave
Thank you Dave, actually this is the code that inspired me to write my code but module in that code is very complicated for my to understand or even to track the functions in it and ad you see I write as simple as possible codes. So if you or anyone
in the forum can help me to take out the code that creates the event for the menu bar ,I would be very thankful.
My regards
John
 
Upvote 0
@Jaafar Tribak Tribak is the person best placed to assist in this area & hopefully, will pick thread up.

Dave
 
Upvote 0
You're missing pretty much all of the code. I'm not surprised it's not working.
 
Upvote 0

Forum statistics

Threads
1,223,885
Messages
6,175,183
Members
452,615
Latest member
bogeys2birdies

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top