Skip to content

Commit fe56355

Browse files
committed
Installing process
Allow to assemble a xlsm file instead of an add-in to use as a submodule in other VBA projects.
1 parent 7886059 commit fe56355

File tree

2 files changed

+179
-0
lines changed

2 files changed

+179
-0
lines changed

Installer.xls

3.5 KB
Binary file not shown.

src/Installer.xls/Installer.bas

Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
Attribute VB_Name = "Installer"
2+
3+
Option Explicit
4+
5+
'1) Create an Excel file called Installer.xlsm in same folder than Installer.bas:
6+
' *\GIT\vbaDeveloper-master\
7+
8+
'2) Open the VB Editor (Alt+F11) right click on the active project and choose Import a file and chose:
9+
' *\GIT\vbaDeveloper-master\Installer.bas
10+
11+
12+
'3a) Go in Tools--> References and activate:
13+
' - Microsoft Scripting Runtime
14+
' - Microsoft Visual Basic for Application Extensibility X.X
15+
16+
'3b) Enable programatic access to VBA:
17+
' File -> Options -> Trust Center, Trust Center Settings, -> Macros,
18+
' tick the box: 'Enable programatic access to VBA' (In excel 2010: 'Trust access to the vba project object model')
19+
20+
'4) Run the Sub AutoInstaller in the module Installer
21+
22+
23+
'5) Create a new excel file and also open the file vbaDeveloper.xlam located in the folder: *\GIT\vbaDeveloper-master\
24+
25+
'6) Make step 3a and 3b again for this file and run the sub testImport located in the module "Build".
26+
27+
Public Const TOOL_NAME = "vbaDeveloper"
28+
29+
Sub AutoInstaller()
30+
31+
AutoInstaller_step0
32+
33+
End Sub
34+
Sub AutoInstaller_step0()
35+
36+
'Close the vbaDevelopper Workbook if already open and uninstall from addins
37+
On Error Resume Next
38+
Workbooks(TOOL_NAME & ".xlam").Close
39+
Application.AddIns2(AddinName2index(TOOL_NAME & ".xlam")).Installed = False
40+
On Error GoTo 0
41+
42+
Application.OnTime Now + TimeValue("00:00:06"), "AutoInstaller_step1"
43+
44+
End Sub
45+
46+
Sub AutoInstaller_step1()
47+
48+
'Prepare variable
49+
Dim CurrentWB As Workbook, NewWB As Workbook
50+
51+
Dim textline As String, strPathOfBuild As String, strLocationXLAM As String
52+
53+
'Set the variables
54+
Set CurrentWB = ThisWorkbook
55+
Set NewWB = Workbooks.Add
56+
57+
'Import code form Build.bas to the new workbook
58+
strPathOfBuild = CurrentWB.Path & "\src\vbaDeveloper.xlam\Build.bas"
59+
NewWB.VBProject.VBComponents.Import strPathOfBuild
60+
61+
'Rename the project (in the VBA) to vbaDeveloper
62+
NewWB.VBProject.Name = TOOL_NAME
63+
64+
'Add references to the library
65+
'Microsoft Scripting Runtime
66+
NewWB.VBProject.References.AddFromGuid "{420B2830-E718-11CF-893D-00A0C9054228}", 1, 0
67+
68+
'Microsoft Visual Basic for Applications Extensibility 5.3
69+
NewWB.VBProject.References.AddFromGuid "{0002E157-0000-0000-C000-000000000046}", 5, 3
70+
71+
'In VB Editor, press F4, then under Microsoft Excel Objects, select ThisWorkbook.Set the property 'IsAddin' to TRUE
72+
NewWB.IsAddin = True
73+
'In VB Editor, menu File-->Save Book1; Save as vbaDeveloper.xlam in the same directory as 'src'
74+
75+
strLocationXLAM = CurrentWB.Path
76+
NewWB.SaveAs strLocationXLAM & "\" & TOOL_NAME & ".xlam", xlOpenXMLAddIn
77+
78+
'Close excel. Open excel with a new workbook, then open the just saved vbaDeveloper.xlam
79+
NewWB.Close savechanges:=False
80+
81+
'Add the Add-in (if not already present)
82+
If IsAddinInstalled(TOOL_NAME & ".xlam") = False Then
83+
Call Application.AddIns2.Add(strLocationXLAM & "\" & TOOL_NAME & ".xlam", CopyFile:=False)
84+
End If
85+
86+
'Continue to step 2
87+
Application.OnTime Now + TimeValue("00:00:02"), "AutoInstaller_step2"
88+
89+
End Sub
90+
91+
Sub AutoInstaller_step2()
92+
93+
'Install the Addin (This should open the file)
94+
Application.AddIns2(AddinName2index(TOOL_NAME & ".xlam")).Installed = True
95+
96+
Application.OnTime Now + TimeValue("00:00:02"), "AutoInstaller_step3"
97+
98+
End Sub
99+
100+
101+
Sub AutoInstaller_step3()
102+
103+
'Run the Build macro in vbaDeveloper
104+
Application.Run "vbaDeveloper.xlam!Build.testImport"
105+
106+
'Continue to step 4
107+
Application.OnTime Now + TimeValue("00:00:06"), "AutoInstaller_step4"
108+
109+
End Sub
110+
111+
Sub AutoInstaller_step4()
112+
113+
'Run the Workbook_Open macro from vbaDeveloper
114+
Application.Run "vbaDeveloper.xlam!Menu.createMenu"
115+
116+
Workbooks(TOOL_NAME & ".xlam").Save
117+
118+
MsgBox TOOL_NAME & " was successfully installed."
119+
120+
End Sub
121+
122+
Function IsAddinInstalled(ByVal addin_name As String) As Boolean
123+
'PURPOSE: Return true if the Add-in is installed
124+
If AddinName2index(addin_name) > 0 Then
125+
IsAddinInstalled = True
126+
ElseIf AddinName2index(addin_name) = 0 Then
127+
IsAddinInstalled = False
128+
End If
129+
End Function
130+
131+
Function AddinName2index(ByVal addin_name As String) As Integer
132+
'PURPOSE: Convert the name of an installed addin to its index
133+
Dim i As Variant
134+
For i = 1 To Excel.Application.AddIns2.Count
135+
If Excel.Application.AddIns2(i).Name = addin_name Then
136+
AddinName2index = i
137+
Exit Function
138+
End If
139+
Next
140+
'If we get to this line, it means no match was found
141+
AddinName2index = 0
142+
End Function
143+
144+
Sub AutoAssembler()
145+
146+
'Prepare variable
147+
Dim CurrentWB As Workbook, NewWB As Workbook
148+
149+
Dim textline As String, strPathOfBuild As String, strLocationXLAM As String
150+
151+
'Set the variables
152+
Set CurrentWB = ThisWorkbook
153+
Set NewWB = Workbooks.Add
154+
155+
'Import code form Build.bas to the new workbook
156+
strPathOfBuild = CurrentWB.Path & "\src\vbaDeveloper.xlam\Build.bas"
157+
NewWB.VBProject.VBComponents.Import strPathOfBuild
158+
159+
'Rename the project (in the VBA) to vbaDeveloper
160+
NewWB.VBProject.Name = TOOL_NAME & "_pkg"
161+
162+
'Add references to the library
163+
'Microsoft Scripting Runtime
164+
NewWB.VBProject.References.AddFromGuid "{420B2830-E718-11CF-893D-00A0C9054228}", 1, 0
165+
166+
'Microsoft Visual Basic for Applications Extensibility 5.3
167+
NewWB.VBProject.References.AddFromGuid "{0002E157-0000-0000-C000-000000000046}", 5, 3
168+
169+
'Save file as .xlsm
170+
strLocationXLAM = CurrentWB.Path
171+
NewWB.SaveAs strLocationXLAM & "\" & TOOL_NAME & "_pkg" & ".xlsm", xlOpenXMLWorkbookMacroEnabled
172+
173+
'Close excel. Open excel with a new workbook, then open the just saved vbaDeveloper.xlsm
174+
NewWB.Close savechanges:=False
175+
176+
'Run the Build macro in vbaDeveloper
177+
Application.Run "vbaDeveloper.xlsm!Build.testImport"
178+
179+
End Sub

0 commit comments

Comments
 (0)