Skip to content

Commit 634a804

Browse files
committed
Installation Process Streamline
1 parent 25bc3a8 commit 634a804

File tree

8 files changed

+143
-368
lines changed

8 files changed

+143
-368
lines changed

Installer.bas

Lines changed: 114 additions & 131 deletions
Original file line numberDiff line numberDiff line change
@@ -1,194 +1,177 @@
11
Attribute VB_Name = "Installer"
22
Option Explicit
33

4-
'1) Create an Excel file called Installer.xlsm in same folder than Installer.bas:
5-
' *\GIT\vbaDeveloper-master\
4+
'1. Create an Excel file called Installer.xlsm (for example) in same folder than Installer.bas:
5+
' *\vbaDeveloper-master\
66

7-
'2) Open the VB Editor (Alt+F11) right click on the active project and choose Import a file and chose:
8-
' *\GIT\vbaDeveloper-master\Installer.bas
7+
'2. Open the VB Editor (Alt+F11) right click on the Installer VB Project and choose Import a file and chose:
8+
' *\vbaDeveloper-master\Installer.bas
99

10-
11-
'3a) Go in Tools--> References and activate:
12-
' - Microsoft Scripting Runtime
13-
' - Microsoft Visual Basic for Application Extensibility X.X
14-
15-
'3b) Enable programatic access to VBA:
10+
'3. Enable programatic access to VBA:
1611
' File -> Options -> Trust Center, Trust Center Settings, -> Macros,
1712
' tick the box: 'Enable programatic access to VBA' (In excel 2010: 'Trust access to the vba project object model')
1813

19-
'4) Run the Sub AutoInstaller in the module Installer
20-
21-
22-
'5) Create a new excel file and also open the file vbaDeveloper.xlam located in the folder: *\GIT\vbaDeveloper-master\
14+
'4. Run AutoInstaller from the module Installer (Click somewhere inside the macro and press F5.
15+
' Make sure to wait for confirmation message at the end before doing anything with Excel.
2316

24-
'6) Make step 3a and 3b again for this file and run the sub testImport located in the module "Build".
25-
26-
Public Const TOOL_NAME = "vbaDeveloper"
17+
Public Const SHORT_NAME = "vbaDeveloper"
18+
Public Const EXT = ".xlam"
2719

2820
Sub AutoInstaller()
2921

30-
AutoInstaller_step0
31-
22+
Call AutoInstaller_Uninstall(RunNextStepOnTime:=True)
23+
'Call AutoInstaller_Generate_File
24+
'Call AutoInstaller_Install_as_Addin
25+
'Call AutoInstaller_Additional_Addin_Components
26+
'Call AutoInstaller_Final_Step
27+
3228
End Sub
33-
Sub AutoInstaller_step0()
29+
Sub AutoInstaller_Uninstall(Optional RunNextStepOnTime As Boolean)
30+
'PURPOSE: Uninstall previous version
3431

35-
'Close the vbaDevelopper Workbook if already open and uninstall from addins
32+
'Close the file if already open
33+
Dim oTwb As Workbook
3634
On Error Resume Next
37-
Workbooks(TOOL_NAME & ".xlam").Close
38-
Application.AddIns2(AddinName2index(TOOL_NAME & ".xlam")).Installed = False
35+
Set oTwb = Workbooks(SHORT_NAME & EXT)
3936
On Error GoTo 0
40-
41-
Application.OnTime Now + TimeValue("00:00:06"), "AutoInstaller_step1"
37+
38+
Dim LongerPause As Boolean
39+
If Not oTwb Is Nothing Then
40+
oTwb.Close Savechanges:=False
41+
LongerPause = True
42+
End If
43+
44+
'Uninstall add-in if applicable
45+
If IsAddinListed(SHORT_NAME & EXT) Then
46+
Application.AddIns2(AddinName2index(SHORT_NAME & EXT)).Installed = False
47+
End If
48+
49+
If RunNextStepOnTime = True And LongerPause Then
50+
'Wait 5 seconds to allow the file to close properly and update the ribbon
51+
Application.OnTime Now + TimeValue("00:00:05"), "'AutoInstaller_Generate_File " & Chr(34) & RunNextStepOnTime & Chr(34) & "'"
52+
ElseIf RunNextStepOnTime = True Then
53+
Application.OnTime Now + TimeValue("00:00:01"), "'AutoInstaller_Generate_File " & Chr(34) & RunNextStepOnTime & Chr(34) & "'"
54+
End If
4255

4356
End Sub
4457

45-
Sub AutoInstaller_step1()
58+
Sub AutoInstaller_Generate_File(Optional RunNextStepOnTime As Boolean)
59+
'PURPOSE: Generate the file as an Add-in
4660

47-
'Prepare variable
48-
Dim CurrentWB As Workbook, NewWB As Workbook
49-
50-
Dim textline As String, strPathOfBuild As String, strLocationXLAM As String
51-
52-
'Set the variables
53-
Set CurrentWB = ThisWorkbook
54-
Set NewWB = Workbooks.Add
55-
56-
'Import code form Build.bas to the new workbook
57-
strPathOfBuild = CurrentWB.Path & "\src\vbaDeveloper.xlam\Build.bas"
58-
NewWB.VBProject.VBComponents.Import strPathOfBuild
61+
'Prepare variable
62+
Dim CurrentWB As Workbook, NewWB As Workbook
63+
Dim textline As String, strPathOfBuild As String
64+
Dim ErrMsg As String
65+
66+
'Set the variables
67+
Set CurrentWB = ThisWorkbook
68+
69+
'Test if this workbook has been saved
70+
Dim FileEverSaved As Boolean
71+
If ThisWorkbook.Path = "" Then
72+
ErrMsg = "Please save the file that contains the Installer module in same folder than Installer.bas and try again"
73+
MsgBox ErrMsg, vbCritical
74+
Exit Sub
75+
End If
76+
77+
'Test if the src folder contains a folder with the right name
78+
Dim SourceFolderExist As Boolean
79+
If Dir(CurrentWB.Path & "\src\" & SHORT_NAME & EXT, vbDirectory) = "" Then
80+
ErrMsg = "Make sure the you saved the file in a location where the source folder (src) contains a folder named " & SHORT_NAME
81+
MsgBox ErrMsg, vbCritical
82+
Exit Sub
83+
End If
84+
85+
'Create the new workbook
86+
Set NewWB = Workbooks.Add
87+
88+
'Import code from Build.bas to the new workbook
89+
strPathOfBuild = CurrentWB.Path & "\src\vbaDeveloper.xlam\Build.bas"
90+
NewWB.VBProject.VBComponents.Import strPathOfBuild
5991

60-
'Rename the project (in the VBA) to vbaDeveloper
61-
NewWB.VBProject.Name = TOOL_NAME
92+
'Rename the VBAProject to the tool name
93+
NewWB.VBProject.name = SHORT_NAME
6294

63-
'Add references to the library
95+
'Add references to the requiered libraries
6496
'Microsoft Scripting Runtime
65-
NewWB.VBProject.References.AddFromGuid "{420B2830-E718-11CF-893D-00A0C9054228}", 1, 0
66-
97+
NewWB.VBProject.References.AddFromGuid "{420B2830-E718-11CF-893D-00A0C9054228}", 1, 0
6798
'Microsoft Visual Basic for Applications Extensibility 5.3
68-
NewWB.VBProject.References.AddFromGuid "{0002E157-0000-0000-C000-000000000046}", 5, 3
99+
NewWB.VBProject.References.AddFromGuid "{0002E157-0000-0000-C000-000000000046}", 5, 3
69100

70-
'In VB Editor, press F4, then under Microsoft Excel Objects, select ThisWorkbook.Set the property 'IsAddin' to TRUE
101+
'Set the file as an add-in, save and close (closing makes sure the file will have the correct name)
71102
NewWB.IsAddin = True
72-
'In VB Editor, menu File-->Save Book1; Save as vbaDeveloper.xlam in the same directory as 'src'
73-
74-
strLocationXLAM = CurrentWB.Path
75-
NewWB.SaveAs strLocationXLAM & "\" & TOOL_NAME & ".xlam", xlOpenXMLAddIn
76-
77-
'Close excel. Open excel with a new workbook, then open the just saved vbaDeveloper.xlam
78-
NewWB.Close savechanges:=False
103+
NewWB.SaveAs CurrentWB.Path & "\" & SHORT_NAME & EXT, xlOpenXMLAddIn
104+
NewWB.Close Savechanges:=False
79105

80-
'Add the Add-in (if not already present)
81-
If IsAddinInstalled(TOOL_NAME & ".xlam") = False Then
82-
Call Application.AddIns2.Add(strLocationXLAM & "\" & TOOL_NAME & ".xlam", CopyFile:=False)
106+
If RunNextStepOnTime = True Then
107+
'No real need to wait here
108+
Application.OnTime Now + TimeValue("00:00:01") / 10, "'AutoInstaller_Install_as_Addin " & Chr(34) & CurrentWB.Path & Chr(34) & "," & Chr(34) & RunNextStepOnTime & Chr(34) & "'"
83109
End If
84110

85-
'Continue to step 2
86-
Application.OnTime Now + TimeValue("00:00:02"), "AutoInstaller_step2"
87-
88111
End Sub
89112

90-
Sub AutoInstaller_step2()
91-
113+
Sub AutoInstaller_Install_as_Addin(ByVal sFolderPath As String, Optional RunNextStepOnTime As Boolean)
114+
'PURPOSE: Install the file as an Add-in
115+
116+
'Add the Add-in to the list of available add-ins (if not already present)
117+
If IsAddinListed(SHORT_NAME & EXT) = False Then
118+
Call Application.AddIns2.Add(sFolderPath & "\" & SHORT_NAME & EXT, CopyFile:=False)
119+
End If
120+
92121
'Install the Addin (This should open the file)
93-
Application.AddIns2(AddinName2index(TOOL_NAME & ".xlam")).Installed = True
122+
Application.AddIns2(AddinName2index(SHORT_NAME & EXT)).Installed = True
94123

95-
Application.OnTime Now + TimeValue("00:00:02"), "AutoInstaller_step3"
124+
If RunNextStepOnTime = True Then
125+
'Wait 2 seconds to allow the change to the ribbon to take place.
126+
Application.OnTime Now + TimeValue("00:00:02"), "'AutoInstaller_Additional_Addin_Components " & Chr(34) & RunNextStepOnTime & Chr(34) & "'"
127+
End If
96128

97129
End Sub
98130

131+
Sub AutoInstaller_Additional_Addin_Components(Optional RunNextStepOnTime As Boolean)
132+
'PURPOSE:Let vbaDeveloper tool build itself
99133

100-
Sub AutoInstaller_step3()
101-
102-
'Run the Build macro in vbaDeveloper
103-
Application.Run "vbaDeveloper.xlam!Build.testImport"
104-
105-
'Continue to step 4
106-
Application.OnTime Now + TimeValue("00:00:06"), "AutoInstaller_step4"
134+
'Run the Build macro in vbaDeveloper (Note that this will trigger a another Application.OnTime command)
135+
Application.Run SHORT_NAME & EXT & "!Build.testImport"
136+
137+
If RunNextStepOnTime = True Then
138+
'It is important to wait 5-6 seconds to let it process the VBA modules in the background
139+
Application.OnTime Now + TimeValue("00:00:06"), "'AutoInstaller_Final_Step'"
140+
End If
107141

108142
End Sub
109143

110-
Sub AutoInstaller_step4()
144+
Sub AutoInstaller_Final_Step()
145+
'PURPOSE: Save and run the Workbook on open event
111146

112-
'Run the Workbook_Open macro from vbaDeveloper
113-
Application.Run "vbaDeveloper.xlam!Menu.createMenu"
147+
Workbooks(SHORT_NAME & EXT).Save
114148

115-
Workbooks(TOOL_NAME & ".xlam").Save
149+
'Run the Workbook_Open macro from vbaDeveloper
150+
Application.Run "vbaDeveloper.xlam!Thisworkbook.Workbook_Open"
151+
'Application.Run "vbaDeveloper.xlam!Menu.createMenu"
116152

117-
MsgBox TOOL_NAME & " was successfully installed."
153+
MsgBox SHORT_NAME & EXT & " was successfully installed."
118154

119155
End Sub
120156

121-
Function IsAddinInstalled(ByVal addin_name As String) As Boolean
157+
Function IsAddinListed(ByVal addin_name As String) As Boolean
122158
'PURPOSE: Return true if the Add-in is installed
123159
If AddinName2index(addin_name) > 0 Then
124-
IsAddinInstalled = True
160+
IsAddinListed = True
125161
ElseIf AddinName2index(addin_name) = 0 Then
126-
IsAddinInstalled = False
162+
IsAddinListed = False
127163
End If
128164
End Function
129165

130166
Function AddinName2index(ByVal addin_name As String) As Integer
131167
'PURPOSE: Convert the name of an installed addin to its index
132168
Dim i As Variant
133169
For i = 1 To Excel.Application.AddIns2.Count
134-
If Excel.Application.AddIns2(i).Name = addin_name Then
170+
If Excel.Application.AddIns2(i).name = addin_name Then
135171
AddinName2index = i
136172
Exit Function
137173
End If
138174
Next
139175
'If we get to this line, it means no match was found
140176
AddinName2index = 0
141177
End Function
142-
143-
Sub AutoAssembler()
144-
145-
'Prepare variable
146-
Dim CurrentWB As Workbook, NewWB As Workbook
147-
148-
Dim textline As String, strPathOfBuild As String, strLocationXLAM As String
149-
150-
'Set the variables
151-
Set CurrentWB = ThisWorkbook
152-
Set NewWB = Workbooks.Add
153-
154-
'Import code form Build.bas to the new workbook
155-
strPathOfBuild = CurrentWB.Path & "\src\vbaDeveloper.xlam\Build.bas"
156-
NewWB.VBProject.VBComponents.Import strPathOfBuild
157-
158-
'Rename the project (in the VBA) to vbaDeveloper
159-
NewWB.VBProject.Name = TOOL_NAME
160-
161-
'Add references to the library
162-
'Microsoft Scripting Runtime
163-
NewWB.VBProject.References.AddFromGuid "{420B2830-E718-11CF-893D-00A0C9054228}", 1, 0
164-
165-
'Microsoft Visual Basic for Applications Extensibility 5.3
166-
NewWB.VBProject.References.AddFromGuid "{0002E157-0000-0000-C000-000000000046}", 5, 3
167-
168-
'In VB Editor, press F4, then under Microsoft Excel Objects, select ThisWorkbook.Set the property 'IsAddin' to TRUE
169-
NewWB.IsAddin = True
170-
'In VB Editor, menu File-->Save Book1; Save as vbaDeveloper.xlam in the same directory as 'src'
171-
172-
'Save file as .xlam
173-
strLocationXLAM = CurrentWB.Path
174-
Application.DisplayAlerts = False
175-
NewWB.SaveAs strLocationXLAM & "\" & TOOL_NAME & ".xlam", xlOpenXMLAddIn
176-
Application.DisplayAlerts = True
177-
178-
'Close and reopen the file
179-
NewWB.Close savechanges:=False
180-
Set NewWB = Workbooks.Open(strLocationXLAM & "\" & TOOL_NAME & ".xlam")
181-
182-
Application.Wait TimeSerial(Hour(Now()), Minute(Now()), Second(Now()) + 5)
183-
184-
'Run the Build macro in vbaDeveloper
185-
Application.OnTime Now + TimeValue("00:00:05"), "vbaDeveloper.xlam!Build.testImport"
186-
Application.OnTime Now + TimeValue("00:00:12"), "installer.xls!SaveFile"
187-
188-
End Sub
189-
190-
Sub SaveFile()
191-
Workbooks("vbaDeveloper.xlam").Save
192-
ThisWorkbook.Saved = True
193-
Application.Quit
194-
End Sub

Installer.xls

-43 KB
Binary file not shown.

README.md

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,32 @@ Building the add-in
2323
This repository does not contain the add-in itself which is an excel add-in in binary format, only the files needed to build it. In short it come downs to these steps:
2424

2525
**Automatically**
26-
- To install the add-in automatically, simply open the Installer.xls file and press the install button. Please wait for the confirmation message that the process has ended before interacting with Excel.
27-
28-
**Manually**
29-
- Manually import the Build module into a new excel workbook.
30-
- Add the required vba references.
31-
- Save the workbook as an excel add-in.
32-
- Close it, then open it again and let the Build module import the other modules.
26+
- Import the Installer module into a new excel workbook.
27+
- Save the workbook in the same location as Installer.bas
28+
- Run the AutoInstaller macro
29+
30+
Read the detailed instructions in Installer.bas.
31+
32+
**Manually**
33+
1. Open a new workbook in excel, then open the VB editor (Alt+F11) and from the menu File->Import, import this file:
34+
* src/vbaDeveloper.xlam/Build.bas
35+
2. From tools references... add
36+
* Microsoft Visual Basic for Applications Extensibility 5.3
37+
* Microsoft Scripting Runtime
38+
3. Rename the project to 'vbaDeveloper'
39+
4. Enable programatic access to VBA:
40+
File -> Options -> Trust Center, Trust Center Settings, -> Macros,
41+
tick the box: 'Enable programatic access to VBA' (In excel 2010: 'Trust access to the vba project object model')
42+
If you get 'path not found' exception in Excel 2013, include the following step:
43+
In 'Trust Center' settings, go to 'File Block Settings' and check 'open' and/or 'save'
44+
for 'Excel 2007 and later Macro-Enabled Workbooks and Templates'.
45+
5. If using a non-English version of Excel, rename your current workbook into ThisWorkbook (in VB Editor, press F4,
46+
then under the local name for Microsoft Excel Objects, select the workbook. Set the property '(Name)' to ThisWorkbook)
47+
6. In VB Editor, press F4, then under Microsoft Excel Objects, select ThisWorkbook.Set the property 'IsAddin' to TRUE
48+
7. In VB Editor, menu File-->Save Book1; Save as vbaDeveloper.xlam in the same directory as 'src'
49+
8. Close excel. Open excel with a new workbook, then open the just saved vbaDeveloper.xlam
50+
9. Let vbaDeveloper import its own code. Put the cursor in the function 'testImport' and press F5
51+
10.If necessary rename module 'Build1' to Build. Menu File-->Save vbaDeveloper.xlam
52+
11.Maybe it will necessary add the add-in at menu File -> Options -> Addins.
3353

3454
Read the detailed instructions in *src/vbaDeveloper.xlam/Build.bas*.

0 commit comments

Comments
 (0)