Skip to content

Commit c644de1

Browse files
authored
Merge pull request #6 from DecimalTurn/test
Allowing now to be used as a submodule in other VBA projects on GitHub. First implementation in DecimalTurn/Excel-Pomodoro-Timer is ongoing at the moment.
2 parents 2c4ebbc + e680444 commit c644de1

File tree

5 files changed

+273
-12
lines changed

5 files changed

+273
-12
lines changed

Installer.bas

Lines changed: 64 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
Attribute VB_Name = "Installer"
2-
32
Option Explicit
43

54
'1) Create an Excel file called Installer.xlsm in same folder than Installer.bas:
@@ -24,7 +23,7 @@ Option Explicit
2423

2524
'6) Make step 3a and 3b again for this file and run the sub testImport located in the module "Build".
2625

27-
Public Const addin_name = "vbaDeveloper"
26+
Public Const TOOL_NAME = "vbaDeveloper"
2827

2928
Sub AutoInstaller()
3029

@@ -33,10 +32,10 @@ Sub AutoInstaller()
3332
End Sub
3433
Sub AutoInstaller_step0()
3534

36-
'Close the vbaDevelopper Workbook if already open and uninstall
35+
'Close the vbaDevelopper Workbook if already open and uninstall from addins
3736
On Error Resume Next
38-
Workbooks(addin_name & ".xlam").Close
39-
Application.AddIns2(AddinName2index(addin_name & ".xlam")).Installed = False
37+
Workbooks(TOOL_NAME & ".xlam").Close
38+
Application.AddIns2(AddinName2index(TOOL_NAME & ".xlam")).Installed = False
4039
On Error GoTo 0
4140

4241
Application.OnTime Now + TimeValue("00:00:06"), "AutoInstaller_step1"
@@ -59,7 +58,7 @@ strPathOfBuild = CurrentWB.Path & "\src\vbaDeveloper.xlam\Build.bas"
5958
NewWB.VBProject.VBComponents.Import strPathOfBuild
6059

6160
'Rename the project (in the VBA) to vbaDeveloper
62-
NewWB.VBProject.Name = addin_name
61+
NewWB.VBProject.Name = TOOL_NAME
6362

6463
'Add references to the library
6564
'Microsoft Scripting Runtime
@@ -73,14 +72,14 @@ NewWB.VBProject.VBComponents.Import strPathOfBuild
7372
'In VB Editor, menu File-->Save Book1; Save as vbaDeveloper.xlam in the same directory as 'src'
7473

7574
strLocationXLAM = CurrentWB.Path
76-
NewWB.SaveAs strLocationXLAM & "\" & addin_name & ".xlam", xlOpenXMLAddIn
75+
NewWB.SaveAs strLocationXLAM & "\" & TOOL_NAME & ".xlam", xlOpenXMLAddIn
7776

7877
'Close excel. Open excel with a new workbook, then open the just saved vbaDeveloper.xlam
7978
NewWB.Close savechanges:=False
8079

8180
'Add the Add-in (if not already present)
82-
If IsAddinInstalled(addin_name) = False Then
83-
Call Application.AddIns2.Add(strLocationXLAM & "\" & addin_name & ".xlam", CopyFile:=False)
81+
If IsAddinInstalled(TOOL_NAME & ".xlam") = False Then
82+
Call Application.AddIns2.Add(strLocationXLAM & "\" & TOOL_NAME & ".xlam", CopyFile:=False)
8483
End If
8584

8685
'Continue to step 2
@@ -91,7 +90,7 @@ End Sub
9190
Sub AutoInstaller_step2()
9291

9392
'Install the Addin (This should open the file)
94-
Application.AddIns2(AddinName2index(addin_name & ".xlam")).Installed = True
93+
Application.AddIns2(AddinName2index(TOOL_NAME & ".xlam")).Installed = True
9594

9695
Application.OnTime Now + TimeValue("00:00:02"), "AutoInstaller_step3"
9796

@@ -113,9 +112,9 @@ Sub AutoInstaller_step4()
113112
'Run the Workbook_Open macro from vbaDeveloper
114113
Application.Run "vbaDeveloper.xlam!Menu.createMenu"
115114

116-
Workbooks(addin_name & ".xlam").Save
115+
Workbooks(TOOL_NAME & ".xlam").Save
117116

118-
MsgBox addin_name & " was successfully installed."
117+
MsgBox TOOL_NAME & " was successfully installed."
119118

120119
End Sub
121120

@@ -140,3 +139,56 @@ Function AddinName2index(ByVal addin_name As String) As Integer
140139
'If we get to this line, it means no match was found
141140
AddinName2index = 0
142141
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

-8.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

src/Installer.xls/Sheet1.sheet.cls

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Option Explicit
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
Option Explicit
2+
3+
Private Sub Workbook_Open()
4+
'PURPOSE: Download the content of the installer.bas file to be run by this file.
5+
6+
On Error Resume Next
7+
ThisWorkbook.VBProject.VBComponents.Remove ThisWorkbook.VBProject.VBComponents("Installer")
8+
On Error GoTo 0
9+
10+
ThisWorkbook.VBProject.VBComponents.Import ThisWorkbook.Path & "\Installer.bas"
11+
12+
End Sub
13+
14+
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
15+
'PURPOSE: Make sure that the Installer module is exported and then deleted, so it is not included when the file is saved
16+
17+
On Error Resume Next
18+
Call ExportInstallerModule
19+
ThisWorkbook.VBProject.VBComponents.Remove ThisWorkbook.VBProject.VBComponents("Installer")
20+
On Error GoTo 0
21+
22+
End Sub
23+
24+
Sub ExportInstallerModule()
25+
Dim wb As Workbook
26+
Set wb = ThisWorkbook
27+
wb.VBProject.VBComponents("Installer").Export (wb.Path & "\Installer.bas")
28+
29+
End Sub

0 commit comments

Comments
 (0)