Skip to content

Commit e9cdc04

Browse files
committed
Create a menu to easily import or export vba code
1 parent 39eb62b commit e9cdc04

File tree

2 files changed

+109
-2
lines changed

2 files changed

+109
-2
lines changed

src/vbaDeveloper.xlam/Menu.bas

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
Attribute VB_Name = "Menu"
2+
Option Explicit
3+
4+
Private Const MENU_TITLE = "vbaDeveloper"
5+
6+
Public Sub createMenu()
7+
Dim rootMenu As CommandBarPopup
8+
9+
'Add the top-level menu to the ribbon Add-ins section
10+
Set rootMenu = Application.CommandBars(1).Controls.Add(Type:=msoControlPopup, _
11+
Before:=10, _
12+
Temporary:=True)
13+
rootMenu.caption = MENU_TITLE
14+
15+
16+
Dim exSubMenu As CommandBarPopup
17+
Dim imSubMenu As CommandBarPopup
18+
Set exSubMenu = addSubmenu(rootMenu, 1, "Export code for ...")
19+
Set imSubMenu = addSubmenu(rootMenu, 2, "Import code for ...")
20+
addMenuSeparator rootMenu
21+
Dim refreshItem As CommandBarButton
22+
Set refreshItem = addMenuItem(rootMenu, "Menu.refreshMenu", "Refresh this menu")
23+
refreshItem.FaceId = 37
24+
25+
' menuItem.FaceId = FaceId ' set a picture
26+
Dim vProject As Variant
27+
For Each vProject In Application.VBE.VBProjects
28+
' We skip over unsaved projects where project.fileName throws error
29+
On Error GoTo nextProject
30+
Dim project As VBProject
31+
Set project = vProject
32+
Dim projectName As String
33+
projectName = project.name
34+
Dim caption As String
35+
caption = projectName & " (" & Dir(project.fileName) & ")" '<- this can throw error
36+
Dim exCommand As String
37+
Dim imCommand As String
38+
exCommand = "'Menu.exportVbProject """ & projectName & """'"
39+
imCommand = "'Menu.importVbProject """ & projectName & """'"
40+
addMenuItem exSubMenu, exCommand, caption
41+
addMenuItem imSubMenu, imCommand, caption
42+
nextProject:
43+
Next vProject
44+
On Error GoTo 0 'reset the error handling
45+
End Sub
46+
47+
Private Function addMenuItem(menu As CommandBarPopup, ByVal onAction As String, ByVal caption As String) As CommandBarButton
48+
Dim menuItem As CommandBarButton
49+
Set menuItem = menu.Controls.Add(Type:=msoControlButton)
50+
menuItem.onAction = onAction
51+
menuItem.caption = caption
52+
Set addMenuItem = menuItem
53+
End Function
54+
55+
56+
Private Function addSubmenu(menu As CommandBarPopup, ByVal position As Integer, ByVal caption As String) As CommandBarPopup
57+
Dim subMenu As CommandBarPopup
58+
Set subMenu = menu.Controls.Add(Type:=msoControlPopup)
59+
subMenu.onAction = position
60+
subMenu.caption = caption
61+
Set addSubmenu = subMenu
62+
End Function
63+
64+
Private Sub addMenuSeparator(menuItem As CommandBarPopup)
65+
menuItem.BeginGroup = True
66+
End Sub
67+
68+
69+
'This sub should be executed when the workbook is closed
70+
Public Sub deleteMenu()
71+
On Error Resume Next
72+
Application.CommandBars(1).Controls(MENU_TITLE).Delete
73+
On Error GoTo 0
74+
End Sub
75+
76+
Public Sub refreshMenu()
77+
menu.deleteMenu
78+
menu.createMenu
79+
End Sub
80+
81+
Public Sub exportVbProject(ByVal projectName As String)
82+
On Error GoTo exportVbProject_Error
83+
84+
Dim project As VBProject
85+
Set project = Application.VBE.VBProjects(projectName)
86+
Build.exportVbaCode project
87+
MsgBox "Finished exporting code for: " & project.name
88+
89+
On Error GoTo 0
90+
Exit Sub
91+
exportVbProject_Error:
92+
ErrorHandling.handleError "Menu.exportVbProject"
93+
End Sub
94+
95+
Public Sub importVbProject(ByVal projectName As String)
96+
On Error GoTo importVbProject_Error
97+
98+
Dim project As VBProject
99+
Set project = Application.VBE.VBProjects(projectName)
100+
Build.importVbaCode project
101+
MsgBox "Finished importing code for: " & project.name
102+
103+
On Error GoTo 0
104+
Exit Sub
105+
importVbProject_Error:
106+
ErrorHandling.handleError "Menu.importVbProject"
107+
End Sub

src/vbaDeveloper.xlam/ThisWorkbook.sheet.cls

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,16 @@ Private Sub Workbook_Open()
88
Debug.Print "vbaDeveloper thisWorkbook_open()"
99
Set listener = New EventListener
1010
Set customAction = New MyCustomActions
11+
menu.createMenu
1112
customAction.afterOpen
1213
End Sub
1314

1415
' Clean up our private members
1516
Private Sub Workbook_BeforeClose(Cancel As Boolean)
1617
Debug.Print "vbaDeveloper thisWorkbook_BeforeClose()"
18+
menu.deleteMenu
1719
customAction.beforeClose
1820
Set customAction = Nothing
1921
Set listener = Nothing
2022
End Sub
21-
22-
2323

0 commit comments

Comments
 (0)