-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodExport.bas
More file actions
331 lines (263 loc) · 10.9 KB
/
modExport.bas
File metadata and controls
331 lines (263 loc) · 10.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
Attribute VB_Name = "modExport"
' MIT License
' Copyright (c) 2025 Jeffrey Long
' Permission is hereby granted, free of charge, to any person obtaining a copy
' of this software and associated documentation files (the "Software"), to deal
' in the Software without restriction, including without limitation the rights
' to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
' copies of the Software, and to permit persons to whom the Software is
' furnished to do so, subject to the following conditions:
' The above copyright notice and this permission notice shall be included in all
' copies or substantial portions of the Software.
' THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
' IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
' FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
' AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
' LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
' OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
' SOFTWARE.
'@IgnoreModule ModuleWithoutFolder
Option Explicit
' https://docs.microsoft.com/en-us/office/vba/language/reference/visual-basic-add-in-model/properties-visual-basic-add-in-model#type
Private Enum VBComponent
vbext_ct_StdModule = 1
vbext_ct_ClassModule = 2
vbext_ct_MSForm = 3
vbext_ct_ActiveXDesigner = 11
vbext_ct_Document = 100
End Enum
'@EntryPoint
Public Sub ExportVBA()
Dim fileName As String
fileName = ExportSheet.Range("FILENAME").Value
Dim exportFolder As String
exportFolder = ExportSheet.Range("EXPORT_FOLDER").Value
If fileName <> vbNullString And exportFolder <> vbNullString Then
ExportComponents fileName, exportFolder
Application.StatusBar = "Done!"
End If
End Sub
Private Sub ExportComponents(ByVal filePathIn As String, ByVal exportFolder As String)
' ================================================================
' Create the directory tree structure
' ================================================================
Dim baseName As String
Dim fileName As String
' Break the input file path down into pieces
With CreateObject("Scripting.FileSystemObject")
fileName = .GetFileName(filePathIn)
baseName = .GetBaseName(filePathIn)
End With
' Create the base export directory
Dim exportDir As String
exportDir = exportFolder & Application.PathSeparator & baseName
DeleteDirAndContents exportDir
CreateDirectory exportDir
' Create a directory to hold all the exported source code
Dim exportSrcDir As String
exportSrcDir = exportDir & Application.PathSeparator & "src"
CreateDirectory exportSrcDir
' Create a directory to hold a copy of the workbook
Dim exportWorkbookDir As String
exportWorkbookDir = exportSrcDir & Application.PathSeparator & "excel"
CreateDirectory exportWorkbookDir
' Create a directory tree to organize the exported VBA components
Dim exportSrcVbaDir As String
exportSrcVbaDir = exportSrcDir & Application.PathSeparator & "vba"
CreateDirectory exportSrcVbaDir
CreateDirectory exportSrcVbaDir & Application.PathSeparator & "Modules"
CreateDirectory exportSrcVbaDir & Application.PathSeparator & "Class Modules"
CreateDirectory exportSrcVbaDir & Application.PathSeparator & "Forms"
CreateDirectory exportSrcVbaDir & Application.PathSeparator & "ActiveX"
CreateDirectory exportSrcVbaDir & Application.PathSeparator & "Microsoft Excel Objects"
' Create a directory to hold the contents of the unzipped spreadsheet
Dim exportSrcZipDir As String
exportSrcZipDir = exportSrcDir & Application.PathSeparator & "xlsm"
CreateDirectory exportSrcZipDir
' ================================================================
' Step 1 - Create a copy of the workbook
' ================================================================
' Remove any file with the same name from the destination directory
Dim exportWorkbookFile As String
exportWorkbookFile = exportWorkbookDir & Application.PathSeparator & fileName
If FileExists(exportWorkbookFile) Then
Kill exportWorkbookFile
End If
' Copy the workbook to the export directory
Application.StatusBar = "Copying " & filePathIn & " to " & exportWorkbookDir
Dim FSO As Object
Set FSO = CreateObject("Scripting.FileSystemObject")
FSO.CopyFile (filePathIn), exportWorkbookDir & Application.PathSeparator, True
Set FSO = Nothing
' ================================================================
' Step 2 - Export the VBA Code
' ================================================================
' Open the workbook, but don't display it
Dim wrkbook As Excel.Workbook
Application.ScreenUpdating = False
Set wrkbook = Workbooks.Open(filePathIn)
ActiveWindow.Visible = False
ThisWorkbook.Activate
Application.ScreenUpdating = True
Dim visualBasicProject As Object
Set visualBasicProject = wrkbook.VBProject
Dim NumComponents As Long
NumComponents = visualBasicProject.VBComponents.Count
Dim ext As String
Dim subdir As String
Dim filePathOut As String
Dim i As Long
For i = 1 To NumComponents
Select Case visualBasicProject.VBComponents(i).Type
Case VBComponent.vbext_ct_StdModule ' = 1
ext = "bas"
subdir = "Modules"
Case VBComponent.vbext_ct_ClassModule ' = 2
ext = "cls"
subdir = "Class Modules"
Case VBComponent.vbext_ct_MSForm ' = 3
ext = "frm"
subdir = "Forms"
Case VBComponent.vbext_ct_ActiveXDesigner ' = 11
ext = "ocx"
subdir = "ActiveX"
Case VBComponent.vbext_ct_Document ' = 100
ext = "cls"
subdir = "Microsoft Excel Objects"
End Select
filePathOut = exportSrcVbaDir & Application.PathSeparator & subdir & Application.PathSeparator & visualBasicProject.VBComponents(i).Name & "." & ext
Application.StatusBar = "Exporting " & filePathOut
visualBasicProject.VBComponents(i).Export (filePathOut)
DoEvents
Next i
' Cleanup
Set visualBasicProject = Nothing
wrkbook.Close SaveChanges:=False
Set wrkbook = Nothing
' ================================================================
' Step 3 - Unzip the workbook
' ================================================================
' Rename the copy of the workbook to have a "zip" extension
Dim filePathZip As String
filePathZip = exportWorkbookDir & Application.PathSeparator & baseName & ".zip"
If FileExists(filePathZip) Then
Kill filePathZip
End If
If FileExists(exportWorkbookFile) Then
Name exportWorkbookFile As filePathZip
End If
Dim shellApplication As Shell
Set shellApplication = CreateObject("Shell.Application")
Dim zipFileItems As FolderItems
Set zipFileItems = shellApplication.Namespace(CVar(filePathZip)).Items
' Unzip all files to the "source" folder
Application.StatusBar = "Unzipping spreadsheet contents to " & exportSrcZipDir
shellApplication.Namespace(exportSrcZipDir).CopyHere zipFileItems
' Rename the zip file back to the original filename
Name filePathZip As exportWorkbookFile
' Cleanup
Set zipFileItems = Nothing
Set shellApplication = Nothing
End Sub
'@EntryPoint
Public Sub GetFileName()
Dim fileName As String
fileName = ChooseExcelFile()
ExportSheet.Range("FILENAME").Value = fileName
End Sub
'@EntryPoint
Public Sub ClearFileName()
ExportSheet.Range("FILENAME").ClearContents
End Sub
'@EntryPoint
Public Sub GetExportFolder()
Dim folderName As String
folderName = ExportSheet.Range("EXPORT_FOLDER").Value
folderName = ChooseDirectory(folderName)
ExportSheet.Range("EXPORT_FOLDER").Value = folderName
End Sub
'@EntryPoint
Public Sub ClearExportFolder()
ExportSheet.Range("EXPORT_FOLDER").ClearContents
End Sub
Private Function ChooseExcelFile() As String
Dim fdExcelFilePicker As Office.FileDialog
On Error GoTo ChooseExcelFile_error_handler
Set fdExcelFilePicker = Application.FileDialog(msoFileDialogOpen)
With fdExcelFilePicker
.Title = "Select Macro-enabled Excel File"
.ButtonName = "Open"
.Filters.Clear
.Filters.Add "Excel Files", "*.xlsm"
.Show
If .SelectedItems.Count > 0 Then
ChooseExcelFile = .SelectedItems.Item(1)
End If
End With
Exit_Point:
Exit Function
ChooseExcelFile_error_handler:
MsgBox "ChooseExcelFile - " & Err.Number & " - " & Err.Description
On Error GoTo 0: Resume Exit_Point
End Function
Private Function ChooseDirectory(ByVal startDir As String) As String
ChooseDirectory = startDir
Dim fileDialogHandle As FileDialog
Set fileDialogHandle = Application.FileDialog(msoFileDialogFolderPicker)
If fileDialogHandle Is Nothing Then
MsgBox "msgboxNoFolderPicker"
Else
If Trim$(startDir) <> vbNullString Then
If DirectoryExists(startDir) Then
fileDialogHandle.InitialFileName = startDir
End If
End If
' Get the number of the button chosen
Dim selected As Long
selected = fileDialogHandle.Show
If selected = -1 Then
' User clicked on CANCEL
' Set path of directory chosen
ChooseDirectory = fileDialogHandle.SelectedItems.Item(1)
End If
End If
Set fileDialogHandle = Nothing
End Function
Private Function DirectoryExists(ByVal dirPath As String) As Boolean
Dim FSO As Object
Set FSO = CreateObject("Scripting.FileSystemObject")
DirectoryExists = False
If Len(dirPath) > 0 Then
If FSO.FolderExists(dirPath) = True Then
DirectoryExists = True
End If
End If
Set FSO = Nothing
End Function
Private Sub CreateDirectory(ByVal directoryName As String)
On Error Resume Next
Application.StatusBar = "Creating directory " & directoryName
MkDir directoryName
On Error GoTo 0
End Sub
Private Function FileExists(ByVal filePath As String) As Boolean
FileExists = False
Dim FSO As Object
Set FSO = CreateObject("Scripting.FileSystemObject")
If Len(filePath) > 0 Then
If FSO.FileExists(filePath) = True Then
FileExists = True
End If
End If
Set FSO = Nothing
End Function
Private Sub DeleteDirAndContents(ByVal folderPath As String)
Dim FSO As Object
Set FSO = CreateObject("Scripting.FileSystemObject")
If Len(folderPath) > 0 Then
If FSO.FolderExists(folderPath) = True Then
FSO.deletefolder folderPath
End If
End If
Set FSO = Nothing
End Sub