-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvbs_access_export.vbs
639 lines (440 loc) · 14.4 KB
/
vbs_access_export.vbs
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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
' ========================================================
'
' Author: Christophe Avonture
' Date : December 2018
'
' Open a MS Access database and export every forms, macros,
' modules, queries and reports code
'
' Include the somes VBS classes from
' https://github.com/cavo789/vbs_scripts
'
' ========================================================
Option Explicit
Class clsFiles
Dim objFSO, objFile
Private bVerbose
Public Property Let verbose(ByVal bYesNo)
bVerbose = bYesNo
End Property
Private Sub Class_Initialize()
bVerbose = False
Set objFSO = CreateObject("Scripting.FileSystemObject")
End Sub
Private Sub Class_Terminate()
Set objFSO = Nothing
End Sub
' --------------------------------------------------
' Create a text file
' --------------------------------------------------
Public Sub CreateText(ByVal sFileName, ByVal sContent)
If bVerbose Then
' wScript.echo "Create file " & sFileName
End If
Set objFile = objFSO.CreateTextFile(sFileName, 2, True)
objFile.Write sContent
objFile.Close
Set objFile = Nothing
End Sub
' --------------------------------------------------
' Verifies the existence of the file
' --------------------------------------------------
Public Function Exists(ByVal sFileName)
Exists = objFSO.FileExists(sFileName)
End Function
' --------------------------------------------------
' Remove some characters like a wildcard (*) if
' present in the suggested filename
' --------------------------------------------------
Public Function MakeSafe(ByVal sFileName)
' Don't allow * in a filename
sFileName = Replace(sFileName, "*", "_")
MakeSafe = sFileName
End Function
' --------------------------------------------------
' Return the file extension (f.i. "accdb")
' --------------------------------------------------
Public Function GetExtensionName(ByVal sFileName)
GetExtensionName = objFSO.GetExtensionName(sFileName)
End Function
' --------------------------------------------------
' Return only the file name (f.i. "db1")
' --------------------------------------------------
Public Function GetBaseName(ByVal sFileName)
GetBaseName = objFSO.GetBaseName(sFileName)
End Function
' --------------------------------------------------
' Return the folder where the file is stored (f.i. c:\temp)
' --------------------------------------------------
Public Function GetParentFolderName(ByVal sFileName)
Dim sPath
Dim objShell
sPath = ""
If (Exists(sFileName)) Then
sPath = objFSO.GetParentFolderName(sFileName)
' sPath is empty when sFileName was just a filename
' like f.i. "db1.accdb". So, in that case, get the current
' folder and concatenate
If (sPath = "") Then
Set objShell = WScript.CreateObject("WScript.Shell")
sPath = objShell.CurrentDirectory
Set objShell = Nothing
End If
End If
GetParentFolderName = sPath
End Function
End Class
Class clsFolders
Dim objFSO
Private bVerbose
Private Sub Class_Initialize()
bVerbose = False
Set objFSO = CreateObject("Scripting.FileSystemObject")
End Sub
Private Sub Class_Terminate()
Set objFSO = Nothing
End Sub
Public Property Let verbose(ByVal bYesNo)
bVerbose = bYesNo
End Property
' -----------------------------------------------------------
'
' Create a folder structure; create parents folder if not found
' MakeFolder("c:\temp\a\b\c\d\e") will create the
' full structure in one call
'
' -----------------------------------------------------------
Public Sub MakeFolder(ByVal sFolderName)
Dim arrPart, sBaseName, sDirName
If Not (objFSO.FolderExists(sFolderName)) Then
' Explode the folder name in parts
arrPart = split(sFolderName, "\")
sDirName = ""
For Each sBaseName In arrPart
If sDirName <> "" Then
sDirName = sDirName & "\"
End If
sDirName = sDirName & sBaseName
If (objFSO.FolderExists(sDirName) = False) Then
objFSO.CreateFolder(sDirName & "\")
End If
Next
End If
End Sub
End Class
Class clsMSAccess
Private oApplication
Private bVerbose
Private sExportPath
Private cFiles
Private cFolders
Private sDatabaseName
Public Property Let verbose(ByVal bYesNo)
bVerbose = bYesNo
cFiles.Verbose = bYesNo
cFolders.Verbose = bYesNo
End Property
Public Property Let DatabaseName(ByVal sFileName)
sDatabaseName = sFileName
End Property
' -----------------------------------------------------------
' Folder where files will be generated
' -----------------------------------------------------------
Public Property Let ExportPath(ByVal sPath)
If bVerbose Then
wScript.echo "Exporting sources to " & sPath & vbCRLF
End If
sExportPath = sPath
End Property
Private Sub Class_Initialize()
Set cFiles = new clsFiles
Set cFolders = new clsFolders
Set oApplication = Nothing
bVerbose = False
sDatabaseName = ""
sExportPath = ""
End Sub
Private Sub Class_Terminate()
If Not (oApplication Is Nothing) Then
' Quit MS Access only when it was opened by this script
' This is the case when UserControl is equal to False
If (oApplication.UserControl = false) then
oApplication.Quit
End If
Set oApplication = Nothing
End If
Set cFiles = Nothing
Set cFolders = Nothing
End Sub
' -----------------------------------------------------------
' Open the database
' -----------------------------------------------------------
Public Sub OpenDatabase()
If (oApplication is Nothing) Then
On Error Resume Next
' Perhaps the database is already opened (manually opened by the user)
Set oApplication = GetObject(sDatabaseName)
If Err.Number <> 0 Then
' No, so start Access
Set oApplication = CreateObject("Access.Application")
If (Right(sDatabaseName,4) = ".adp") Then
oApplication.OpenAccessProject sDatabaseName, false
Else
oApplication.OpenCurrentDatabase sDatabaseName, false
End If
oApplication.visible = true
' MS Access has been opened by code, not under
' the control of the user.
' So UserControl = False will be used to detect that
' we can close the DB and Quit Access when the job is done
oApplication.UserControl = False
End If
On Error Goto 0
If (oApplication is Nothing) then
' Ouch! Something goes wrong with the automation
wScript.echo "ERROR - It was impossible to open the database by automation"
wScript.echo "Please manually open MS Access and open the database, do not close "
wScript.echo "Access and start this script again."
wScript.quit
End If
End If
End Sub
' -----------------------------------------------------------
' Close the database
' -----------------------------------------------------------
Public Sub CloseDatabase()
If Not (oApplication is Nothing) then
' Close the database only when it was opened by this script
' This is the case when UserControl is equal to False
If (oApplication.UserControl = false) then
oApplication.CloseCurrentDatabase
End If
End If
End Sub
' -----------------------------------------------------------
' Export forms as .frm files
' -----------------------------------------------------------
Private Sub ExportForms()
Dim j, k
Dim sOutFileName, sSQL
Dim obj
k = oApplication.CurrentProject.AllForms.Count
If (k > 0) Then
If bVerbose Then
wScript.Echo vbCrLf & "Export all forms"
End if
j = 0
Call cFolders.MakeFolder(sExportPath & "Forms\")
For Each obj In oApplication.CurrentProject.AllForms
j = j + 1
sOutFileName = cFiles.MakeSafe(obj.FullName) & ".frm"
sOutFileName = "Forms\" & sOutFileName
If bVerbose Then
wScript.echo " Export form " & j & "/" & k & " - " & _
obj.FullName & " to " & sOutFileName
End If
' 2 = acForm
oApplication.SaveAsText 2, obj.FullName, sExportpath & sOutFileName
oApplication.DoCmd.Close 2, obj.FullName
Next
End If
End Sub
' -----------------------------------------------------------
' Export macros as .txt files
' -----------------------------------------------------------
Private Sub ExportMacros()
Dim j, k
Dim sOutFileName, sSQL
Dim obj
k = oApplication.CurrentProject.AllMacros.Count
If (k > 0) Then
If bVerbose Then
wScript.Echo vbCrLf & "Export all macros"
End if
j = 0
Call cFolders.MakeFolder(sExportPath & "Macros\")
For Each obj In oApplication.CurrentProject.AllMacros
j = j + 1
sOutFileName = cFiles.MakeSafe(obj.FullName) & ".txt"
sOutFileName = "Macros\" & sOutFileName
If bVerbose Then
wScript.echo " Export macro " & j & "/" & k & " - " & _
obj.FullName & " to " & sOutFileName
End If
' 4 = acMacro
oApplication.SaveAsText 4, obj.FullName, sExportpath & sOutFileName
Next
End If
End Sub
' -----------------------------------------------------------
' Export modules as .bas files
' -----------------------------------------------------------
Private Sub ExportModules()
Dim j, k
Dim sOutFileName, sSQL
Dim obj
k = oApplication.CurrentProject.AllModules.Count
If (k > 0) Then
If bVerbose Then
wScript.Echo vbCrLf & "Export all modules"
End if
j = 0
Call cFolders.MakeFolder(sExportPath & "Modules\")
For Each obj In oApplication.CurrentProject.AllModules
j = j + 1
' Don't allow * in a filename
sOutFileName = cFiles.MakeSafe(obj.FullName) & ".bas"
sOutFileName = "Modules\" & sOutFileName
If bVerbose Then
wScript.echo " Export module " & j & "/" & k & " - " & _
obj.FullName & " to " & sOutFileName
End If
' 5 = acModule
On Error Resume Next
oApplication.SaveAsText 5, obj.FullName, sExportpath & sOutFileName
If Err.number <> 0 Then
wScript.echo " An error has occured: " & Err.Description
wScript.echo " If a password is needed to see the code, please open the "
wScript.echo " database manually and open a module so you can specify the "
wScript.echo " password. Then, without closing the database, restart this script"
Err.clear
' And stop
Exit For
End If
On Error Goto 0
Next
End If
End sub
' -----------------------------------------------------------
' Export queries as .sql files
' -----------------------------------------------------------
Private Sub ExportQueries()
Dim j, k
Dim sOutFileName, sSQL
Dim obj
k = oApplication.CurrentDb.QueryDefs.Count
If (k > 0) Then
If bVerbose Then
wScript.Echo vbCrLf & "Export all queries"
End if
j = 0
Call cFolders.MakeFolder(sExportPath & "Queries\")
' Process all objects
For Each obj In oApplication.CurrentDb.QueryDefs
j = j + 1
sOutFileName = cFiles.MakeSafe(obj.Name) & ".sql"
sOutFileName = "Queries\" & sOutFileName
If bVerbose Then
wScript.echo " Export query " & j & "/" & k & " - " & _
obj.Name & " to " & sOutFileName
End If
' Get the query SQL statement
sSQL = Trim(obj.SQL)
' replace carriage return by a single space
sSQL = Replace(sSQL, vbCrLf, " ")
Call cFiles.CreateText(sExportPath & sOutFileName, sSQL)
Next
End If
End Sub
' -----------------------------------------------------------
' Export reports as .txt files
' -----------------------------------------------------------
Private Sub ExportReports()
Dim j, k
Dim sOutFileName, sSQL
Dim obj
k = oApplication.CurrentProject.AllReports.Count
If (k > 0) Then
If bVerbose Then
wScript.Echo vbCrLf & "Export all reports"
End if
j = 0
Call cFolders.MakeFolder(sExportPath & "Reports\")
For Each obj In oApplication.CurrentProject.AllReports
j = j + 1
sOutFileName = cFiles.MakeSafe(obj.FullName) & ".txt"
sOutFileName = "Reports\" & sOutFileName
If bVerbose Then
wScript.echo " Export report " & j & "/" & k & " - " & _
obj.FullName & " to " & sOutFileName
End If
' 3 = acReport
oApplication.SaveAsText 3, obj.FullName, sExportpath & sOutFileName
Next
End If
End Sub
' -----------------------------------------------------------
' Open a database and export every forms, macros, modules
' and reports code to flat files
'
' Example
' cMSAccess.Decompose("c:\temp\db1.accdb")
'
' -----------------------------------------------------------
Public Sub Decompose(sDBName)
Dim sDBExtension, sDBParentFolder
' Before starting, just verify that files exists
' If no, show an error message and stop
If cFiles.Exists(sDBName) Then
sDBExtension = cFiles.GetExtensionName(sDBName)
sDBName = cFiles.GetBaseName(sDBName) & "." & sDBExtension
sDBParentFolder = cFiles.GetParentFolderName(sDBName)
' Full path
sDatabaseName = sDBParentFolder & "\" & sDBName
If bVerbose Then
wScript.echo "Process database " & sDatabaseName
End If
' Open the database, start MS Access if not yet opened
Call OpenDatabase()
If (sExportPath = "") then
ExportPath = sDBParentFolder & "\src\" & sDBName & "\"
End If
Call cFolders.MakeFolder(sExportPath)
If (oApplication.CurrentProject.AllForms.Count > 0) Then
Call ExportForms()
End if
If (oApplication.CurrentProject.AllMacros.Count > 0) Then
Call ExportMacros()
End if
If (oApplication.CurrentProject.AllModules.Count > 0) Then
Call ExportModules()
End if
If (oApplication.CurrentDb.QueryDefs.Count > 0) Then
Call ExportQueries()
End if
If (oApplication.CurrentProject.AllReports.Count > 0) Then
Call ExportReports()
End if
' Close the database (only if it was opened by automation)
Call CloseDatabase
Else
wScript.echo "Error, the file " & sDBName & " is not found"
End If
End Sub
End Class
Sub ShowHelp()
wScript.echo " ======================================"
wScript.echo " = Export MS Access code in flatfiles ="
wScript.echo " ======================================"
wScript.echo ""
wScript.echo " Please specify the name of the database to process; f.i. : "
wScript.echo " " & Wscript.ScriptName & " 'C:\Temp\db1.accdb'"
wScript.echo ""
wScript.echo "To get more info, please read https://github.com/cavo789/vbs_access_export"
wScript.echo ""
wScript.quit
End sub
Dim cMSAccess
Dim sFile
Dim arrDBNames(0)
' Get the first argument (f.i. "C:\Temp\db1.accdb")
If (wScript.Arguments.Count = 0) Then
Call ShowHelp
Else
' Get the path specified on the command line
sFile = Wscript.Arguments.Item(0)
Set cMSAccess = New clsMSAccess
cMSAccess.Verbose = True
Call cMSAccess.Decompose(sFile)
Set cMSAccess = Nothing
End If