View Single Post
 
Old 05-07-2021, 01:59 PM
coffent coffent is offline Windows 10 Office 2019
Advanced Beginner
 
Join Date: Dec 2019
Posts: 40
coffent is on a distinguished road
Default Sub Auto_Open() runs but doesn't always work

I have the following subroutine

VBAProject (PERSONAL.xlsb)
Modules
Module1
Sub Auto_Open()
Dim Shortcut As CommandBar
Dim NewItem As CommandBarButton
Set Shortcut = Application.CommandBars("Cell")
Set NewItem = Shortcut.Controls.Add()
With NewItem
Caption = "Insert C Comment"
OnAction = "AddComment"
End With
MsgBox ("Test")
End Sub
which should add a new item to the right click/context menu titled "Insert C Comment", which when clicked, calls the subroutine AddComment() (also stored in Module1).

If I run the routine with Excel already open, everything works as expected. However, if I initially open Excel, my understanding is that a subroutine named Auto_Open stored as above should run automatically. And indeed it does, as evidenced by the test MsgBox being displayed. However recently even though the subroutine runs, "Insert C Comment" is *not* added to the context menu. Some time ago it was inserted ok, and later it was inserted twice(!), but now it's not inserted at all. Any ideas as to what's going on?
Reply With Quote