-
Notifications
You must be signed in to change notification settings - Fork 0
/
GitImport.bas
80 lines (72 loc) · 2.95 KB
/
GitImport.bas
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
Attribute VB_Name = "GitImport"
Sub pImport()
Dim ModulePath As String
Dim objFSO As Scripting.FileSystemObject
Dim objFile As Scripting.File
Dim cmpComponent As VBIDE.VBComponent
If ThisWorkbook.VBProject.Protection = 1 Then
MsgBox "Workbook is protected against the importing codes!!" & _
"go to the Tools>Macros>Security(in Excel), click on the Trusted Publishers tab and check the Trust access to the Visual Basic Project setting"
Exit Sub
End If
ModulePath = ThisWorkbook.Path & "\"
'Deleting Existing Modules before importing
For Each cmpComponent In ActiveWorkbook.VBProject.VBComponents
If cmpComponent.Type = vbext_ct_StdModule And cmpComponent.Name <> "GitImport" Then
ThisWorkbook.VBProject.VBComponents.Remove cmpComponent
End If
Next cmpComponent
Set objFSO = New Scripting.FileSystemObject
' Import the VBA code required
If objFSO.GetFolder(ModulePath).Files.Count = 0 Then
MsgBox "There are no files to import"
Exit Sub
Else
For Each objFile In objFSO.GetFolder(ModulePath).Files
If (objFSO.GetExtensionName(objFile.Name) = "cls") Or _
(objFSO.GetExtensionName(objFile.Name) = "frm") Or _
(objFSO.GetExtensionName(objFile.Name) = "bas") And _
objFile.Name <> "GitImport.bas" Then
ThisWorkbook.VBProject.VBComponents.Import objFile.Path
End If
Next objFile
End If
' Save the workbook
ActiveWorkbook.Save
MsgBox "Import Successful!"
End Sub
Sub pExport()
Dim bExport As Boolean
Dim ModulePath As String
Dim cmpComponent As VBIDE.VBComponent
Dim szFileName As String
ModulePath = ThisWorkbook.Path
Kill ModulePath & "\*.bas"
If ThisWorkbook.VBProject.Protection = 1 Then
MsgBox "Workbook is protected against the importing codes!!" & _
"go to the Tools>Macros>Security(in Excel), click on the Trusted Publishers tab and check the Trust access to the Visual Basic Project setting"
Exit Sub
End If
For Each cmpComponent In ThisWorkbook.VBProject.VBComponents
bExport = True
szFileName = cmpComponent.Name
''' Concatenate the correct filename for export.
Select Case cmpComponent.Type
Case vbext_ct_ClassModule
szFileName = szFileName & ".cls"
Case vbext_ct_MSForm
szFileName = szFileName & ".frm"
Case vbext_ct_StdModule
szFileName = szFileName & ".bas"
Case vbext_ct_Document
''' This is a worksheet or workbook object.
''' Don't try to export.
bExport = False
End Select
If bExport Then
''' Export the component to a text file.
cmpComponent.Export ModulePath & "\" & szFileName
End If
Next cmpComponent
MsgBox "Export Successful!"
End Sub