-
Notifications
You must be signed in to change notification settings - Fork 0
/
VBVCS.cls
129 lines (111 loc) · 4.08 KB
/
VBVCS.cls
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
VERSION 1.0 CLASS
BEGIN
MultiUse = -1 'True
END
Attribute VB_Name = "VCSHelper"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
Option Explicit
' Code adapted from Jim Harte's answer at the following link:
' https://stackoverflow.com/questions/49724/programmatically-extract-macro-vba-code-from-word-2007-docs/49796#49796
' //FEATURE: Import code
Public Sub ImportCode()
' Exit if access to VB object model is not allowed
' Exit if VBA window is not open
' Check if workbook is in a VSC directory
' Check and make directories
' Import components
End Sub
Sub ExportCode()
If Not CanAccessVBOM Then Exit Sub ' Exit if access to VB object model is not allowed
If (ThisWorkbook.VBProject.VBE.ActiveWindow Is Nothing) Then
Exit Sub ' Exit if VBA window is not open
End If
Dim comp As VBComponent
Dim codeFolder As String
' //TODO: use FileHandler to determine if workbook is in a VCS directory (search for .git?)
codeFolder = "C:\Users\slj9prv\Desktop\DEV\CODE\working\AutoManifest\src"
On Error Resume Next
MkDir CombinePaths(codeFolder, "cls")
MkDir CombinePaths(codeFolder, "bas")
MkDir CombinePaths(codeFolder, "frm")
MkDir CombinePaths(codeFolder, "sht")
On Error GoTo 0
Dim fName As String
' Export components
For Each comp In ThisWorkbook.VBProject.VBComponents
Select Case comp.Type
Case vbext_ct_ClassModule
fName = CombinePaths(codeFolder, "cls\" & comp.name & ".cls")
DeleteFile fName
comp.Export fName
Case vbext_ct_StdModule
fName = CombinePaths(codeFolder, "bas\" & comp.name & ".bas")
DeleteFile fName
comp.Export fName
Case vbext_ct_MSForm
fName = CombinePaths(codeFolder, "frm\" & comp.name & ".frm")
DeleteFile fName
comp.Export fName
Case vbext_ct_Document
fName = CombinePaths(codeFolder, "sht\" & comp.name & ".cls")
DeleteFile fName
comp.Export fName
End Select
Next
End Sub
Public Sub BackupWorkbook(sPath As String, sName As String)
Dim checker As New DirectoryChecker
Dim wb As Workbook
Set wb = ThisWorkbook
checker.CreateInPath (sPath)
Set checker = Nothing
'//TODO: delete backups older than a month
wb.SaveCopyAs (sPath & "/" & sName)
End Sub
Function CanAccessVBOM() As Boolean
' Check resgistry to see if we can access the VB object model
Dim wsh As Object
Dim str1 As String
Dim AccessVBOM As Long
Set wsh = CreateObject("WScript.Shell")
str1 = "HKEY_CURRENT_USER\Software\Microsoft\Office\" & _
Application.Version & "\Excel\Security\AccessVBOM"
On Error Resume Next
AccessVBOM = wsh.RegRead(str1)
Set wsh = Nothing
CanAccessVBOM = (AccessVBOM = 1)
End Function
Sub DeleteFile(fileName As String)
On Error Resume Next
Kill fileName
End Sub
Function GetWorkbookPath() As String
Dim fullName As String
Dim wrkbookName As String
Dim pos As Long
wrkbookName = ThisWorkbook.name
fullName = ThisWorkbook.fullName
pos = InStr(1, fullName, wrkbookName, vbTextCompare)
GetWorkbookPath = Left$(fullName, pos - 1)
End Function
Function CombinePaths(ByVal Path1 As String, ByVal Path2 As String) As String
If Not EndsWith(Path1, "\") Then
Path1 = Path1 & "\"
End If
CombinePaths = Path1 & Path2
End Function
Function EndsWith(ByVal InString As String, ByVal TestString As String) As Boolean
EndsWith = (Right$(InString, Len(TestString)) = TestString)
End Function
Function GetUsername() As String
Set objAD = CreateObject("ADSystemInfo")
Set objUser = GetObject("LDAP://" & objAD.UserName)
strDisplayName = objUser.DisplayName
GetUsername = strDisplayName
End Function
Public Function IsUserAuthorized() As Boolean
If InStr(UCase(GetUsername), "SLJ9PRV") Then IsUserAuthorized = True Else IsUserAuthorized = False
End Function