|
1 | 1 | Attribute VB_Name = "Installer"
|
2 | 2 | Option Explicit
|
3 | 3 |
|
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\ |
6 | 6 |
|
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 |
9 | 9 |
|
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: |
16 | 11 | ' File -> Options -> Trust Center, Trust Center Settings, -> Macros,
|
17 | 12 | ' tick the box: 'Enable programatic access to VBA' (In excel 2010: 'Trust access to the vba project object model')
|
18 | 13 |
|
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. |
23 | 16 |
|
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" |
27 | 19 |
|
28 | 20 | Sub AutoInstaller()
|
29 | 21 |
|
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 | + |
32 | 28 | End Sub
|
33 |
| -Sub AutoInstaller_step0() |
| 29 | +Sub AutoInstaller_Uninstall(Optional RunNextStepOnTime As Boolean) |
| 30 | +'PURPOSE: Uninstall previous version |
34 | 31 |
|
35 |
| - 'Close the vbaDevelopper Workbook if already open and uninstall from addins |
| 32 | + 'Close the file if already open |
| 33 | + Dim oTwb As Workbook |
36 | 34 | 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) |
39 | 36 | 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 |
42 | 55 |
|
43 | 56 | End Sub
|
44 | 57 |
|
45 |
| -Sub AutoInstaller_step1() |
| 58 | +Sub AutoInstaller_Generate_File(Optional RunNextStepOnTime As Boolean) |
| 59 | +'PURPOSE: Generate the file as an Add-in |
46 | 60 |
|
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 |
59 | 91 |
|
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 |
62 | 94 |
|
63 |
| - 'Add references to the library |
| 95 | + 'Add references to the requiered libraries |
64 | 96 | '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 |
67 | 98 | '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 |
69 | 100 |
|
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) |
71 | 102 | 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 |
79 | 105 |
|
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) & "'" |
83 | 109 | End If
|
84 | 110 |
|
85 |
| - 'Continue to step 2 |
86 |
| - Application.OnTime Now + TimeValue("00:00:02"), "AutoInstaller_step2" |
87 |
| - |
88 | 111 | End Sub
|
89 | 112 |
|
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 | + |
92 | 121 | '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 |
94 | 123 |
|
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 |
96 | 128 |
|
97 | 129 | End Sub
|
98 | 130 |
|
| 131 | +Sub AutoInstaller_Additional_Addin_Components(Optional RunNextStepOnTime As Boolean) |
| 132 | +'PURPOSE:Let vbaDeveloper tool build itself |
99 | 133 |
|
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 |
107 | 141 |
|
108 | 142 | End Sub
|
109 | 143 |
|
110 |
| -Sub AutoInstaller_step4() |
| 144 | +Sub AutoInstaller_Final_Step() |
| 145 | +'PURPOSE: Save and run the Workbook on open event |
111 | 146 |
|
112 |
| - 'Run the Workbook_Open macro from vbaDeveloper |
113 |
| - Application.Run "vbaDeveloper.xlam!Menu.createMenu" |
| 147 | + Workbooks(SHORT_NAME & EXT).Save |
114 | 148 |
|
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" |
116 | 152 |
|
117 |
| - MsgBox TOOL_NAME & " was successfully installed." |
| 153 | + MsgBox SHORT_NAME & EXT & " was successfully installed." |
118 | 154 |
|
119 | 155 | End Sub
|
120 | 156 |
|
121 |
| -Function IsAddinInstalled(ByVal addin_name As String) As Boolean |
| 157 | +Function IsAddinListed(ByVal addin_name As String) As Boolean |
122 | 158 | 'PURPOSE: Return true if the Add-in is installed
|
123 | 159 | If AddinName2index(addin_name) > 0 Then
|
124 |
| - IsAddinInstalled = True |
| 160 | + IsAddinListed = True |
125 | 161 | ElseIf AddinName2index(addin_name) = 0 Then
|
126 |
| - IsAddinInstalled = False |
| 162 | + IsAddinListed = False |
127 | 163 | End If
|
128 | 164 | End Function
|
129 | 165 |
|
130 | 166 | Function AddinName2index(ByVal addin_name As String) As Integer
|
131 | 167 | 'PURPOSE: Convert the name of an installed addin to its index
|
132 | 168 | Dim i As Variant
|
133 | 169 | 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 |
135 | 171 | AddinName2index = i
|
136 | 172 | Exit Function
|
137 | 173 | End If
|
138 | 174 | Next
|
139 | 175 | 'If we get to this line, it means no match was found
|
140 | 176 | AddinName2index = 0
|
141 | 177 | 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 |
0 commit comments