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
|
Attribute VB_Name = "Office10Issues"
'/*************************************************************************
' *
' * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
' *
' * Copyright 2008 by Sun Microsystems, Inc.
' *
' * OpenOffice.org - a multi-platform office productivity suite
' *
' * $RCSfile: Office10Issues.bas,v $
' * $Revision: 1.8.148.1 $
' *
' * This file is part of OpenOffice.org.
' *
' * OpenOffice.org is free software: you can redistribute it and/or modify
' * it under the terms of the GNU Lesser General Public License version 3
' * only, as published by the Free Software Foundation.
' *
' * OpenOffice.org is distributed in the hope that it will be useful,
' * but WITHOUT ANY WARRANTY; without even the implied warranty of
' * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
' * GNU Lesser General Public License version 3 for more details
' * (a copy is included in the LICENSE file that accompanied this code).
' *
' * You should have received a copy of the GNU Lesser General Public License
' * version 3 along with OpenOffice.org. If not, see
' * <http://www.openoffice.org/license.html>
' * for a copy of the LGPLv3 License.
' *
' ************************************************************************/
'Disable Option Explicit so this will compile on earlier Office versions
'Option Explicit
Public Declare Function RegCloseKey Lib "advapi32.dll" _
(ByVal hKey As Long) As Long
Public Declare Function RegQueryValueEx Lib "advapi32.dll" _
Alias "RegQueryValueExA" (ByVal hKey As Long, ByVal lpValueName As String, _
ByVal lpReserved As Long, lpType As Long, lpData As Any, _
lpcbData As Long) As Long
Public Declare Function RegSetValueEx Lib "advapi32.dll" _
Alias "RegSetValueExA" (ByVal hKey As Long, ByVal lpValueName As String, _
ByVal Reserved As Long, ByVal dwType As Long, lpData As Any, _
ByVal cbData As Long) As Long
Public Declare Function RegCreateKeyEx Lib "advapi32.dll" Alias "RegCreateKeyExA" (ByVal _
hKey As Long, ByVal lpSubKey As String, ByVal Reserved As Long, ByVal lpClass _
As String, ByVal dwOptions As Long, ByVal samDesired As Long, lpSecurityAttributes _
As SECURITY_ATTRIBUTES, phkResult As Long, lpdwDisposition As Long) As Long
Public Declare Function RegOpenKey Lib "advapi32.dll" _
Alias "RegOpenKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, _
phkResult As Long) As Long
Public Declare Function RegCreateKey Lib "advapi32.dll" _
Alias "RegCreateKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, _
phkResult As Long) As Long
Public Declare Function RegDeleteValue Lib "advapi32.dll" _
Alias "RegDeleteValueA" (ByVal hKey As Long, _
ByVal lpValueName As String) As Long
Public Declare Function RegOpenKeyEx Lib "advapi32.dll" Alias "RegOpenKeyExA" (ByVal _
hKey As Long, ByVal lpSubKey As String, ByVal ulOptions As Long, ByVal samDesired _
As Long, phkResult As Long) As Long
Type SECURITY_ATTRIBUTES
nLength As Long
lpSecurityDescriptor As Long
bInheritHandle As Long
End Type
Enum RegHive
'HKEY_CLASSES_ROOT = &H80000000
HK_CR = &H80000000
HKEY_CURRENT_USER = &H80000001
HK_CU = &H80000001
HKEY_LOCAL_MACHINE = &H80000002
HK_LM = &H80000002
HKEY_USERS = &H80000003
HK_US = &H80000003
HKEY_CURRENT_CONFIG = &H80000005
HK_CC = &H80000005
HKEY_DYN_DATA = &H80000006
HK_DD = &H80000006
End Enum
Enum RegType
REG_SZ = 1 'Unicode nul terminated string
REG_BINARY = 3 'Free form binary
REG_DWORD = 4 '32-bit number
End Enum
Const ERROR_SUCCESS = 0
Const KEY_WRITE = &H20006
Const APP_EXCEL = "Excel"
Const APP_WORD = "Word"
Const APP_PP = "PowerPoint"
Public Function CreateRegKey(hKey As RegHive, strPath As String)
On Error GoTo HandleErrors
Dim currentFunctionName As String
currentFunctionName = "CreateRegKey"
Dim heKey As Long
Dim secattr As SECURITY_ATTRIBUTES ' security settings for the key
Dim subkey As String ' name of the subkey to create or open
Dim neworused As Long ' receives flag for if the key was created or opened
Dim stringbuffer As String ' the string to put into the registry
Dim retval As Long ' return value
' Set the name of the new key and the default security settings
secattr.nLength = Len(secattr)
secattr.lpSecurityDescriptor = 0
secattr.bInheritHandle = 1
retval = RegCreateKeyEx(hKey, strPath, 0, "", 0, KEY_WRITE, _
secattr, heKey, neworused)
If retval = 0 Then
retval = RegCloseKey(hKey)
Exit Function
End If
HandleErrors:
WriteDebug currentFunctionName & " : " & Err.Number & " " & Err.Description & " " & Err.Source
End Function
Public Function CreateRegKey2(hKey As RegHive, strPath As String) As Long
On Error GoTo HandleErrors
Dim currentFunctionName As String
currentFunctionName = "CreateRegKey"
CreateRegKey2 = 0
Dim heKey As Long
Dim secattr As SECURITY_ATTRIBUTES ' security settings for the key
Dim subkey As String ' name of the subkey to create or open
Dim neworused As Long ' receives flag for if the key was created or opened
Dim stringbuffer As String ' the string to put into the registry
Dim retval As Long ' return value
' Set the name of the new key and the default security settings
secattr.nLength = Len(secattr)
secattr.lpSecurityDescriptor = 0
secattr.bInheritHandle = 1
retval = RegCreateKeyEx(hKey, strPath, 0, "", 0, KEY_WRITE, _
secattr, heKey, neworused)
If retval = ERROR_SUCCESS Then
CreateRegKey2 = heKey
Exit Function
End If
FinalExit:
Exit Function
HandleErrors:
WriteDebug currentFunctionName & " : " & Err.Number & " " & Err.Description & " " & Err.Source
CreateRegKey2 = 0
GoTo FinalExit
End Function
Public Function GetRegLong(ByVal hKey As RegHive, ByVal strPath As String, ByVal strValue As String) As Long
On Error GoTo HandleErrors
Dim currentFunctionName As String
currentFunctionName = "GetRegLong"
Dim lRegResult As Long
Dim lValueType As Long
Dim lBuffer As Long
Dim lDataBufferSize As Long
Dim hCurKey As Long
GetRegLong = 0
lRegResult = RegOpenKey(hKey, strPath, hCurKey)
lDataBufferSize = 4 '4 bytes = 32 bits = long
lRegResult = RegQueryValueEx(hCurKey, strValue, 0, REG_DWORD, lBuffer, lDataBufferSize)
If lRegResult = ERROR_SUCCESS Then
GetRegLong = lBuffer
End If
lRegResult = RegCloseKey(hCurKey)
Exit Function
HandleErrors:
WriteDebug currentFunctionName & " : " & Err.Number & " " & Err.Description & " " & Err.Source
End Function
Public Function SaveRegLong(ByVal hKey As RegHive, ByVal strPath As String, ByVal strValue As String, ByVal lData As Long)
On Error GoTo HandleErrors
Dim currentFunctionName As String
currentFunctionName = "SaveRegLong"
Const NumofByte = 4
Dim hCurKey As Long
Dim lRegResult As Long
lRegResult = RegCreateKey(hKey, strPath, hCurKey)
lRegResult = RegSetValueEx(hCurKey, strValue, 0&, REG_DWORD, lData, NumofByte)
If lRegResult = ERROR_SUCCESS Then
lRegResult = RegCloseKey(hCurKey)
Exit Function
End If
HandleErrors:
WriteDebug currentFunctionName & " : " & Err.Number & " " & Err.Description & " " & Err.Source
End Function
Public Function GiveAccessToMacroProject(application As String, sVersion As String, oldvalue As Long) As Boolean
On Error GoTo HandleErrors
Dim currentFunctionName As String
currentFunctionName = "SaveRegLong"
GiveAccessToMacroProject = False
Const OfficePath = "Software\Policies\Microsoft\Office\"
Const security = "\Security"
Const AccessVBOM = "AccessVBOM"
Const AccessVBOMValue = 1
Dim subpath As String
Dim RegistryValue As Long
subpath = OfficePath & sVersion & "\" & application & security
CreateRegKey HKEY_CURRENT_USER, subpath
RegistryValue = GetRegLong(HKEY_CURRENT_USER, subpath, AccessVBOM)
oldvalue = RegistryValue
SaveRegLong HKEY_CURRENT_USER, subpath, AccessVBOM, AccessVBOMValue
GiveAccessToMacroProject = True
Exit Function
HandleErrors:
GiveAccessToMacroProject = False
WriteDebug currentFunctionName & " : " & Err.Number & " " & Err.Description & " " & Err.Source
End Function
Public Function SetDefaultRegValue(application As String, sVersion As String, sValue As Long)
On Error GoTo HandleErrors
Dim currentFunctionName As String
currentFunctionName = "SaveRegLong"
Const OfficePath = "Software\Policies\Microsoft\Office\"
Const security = "\Security"
Const AccessVBOM = "AccessVBOM"
Dim subpath As String
subpath = OfficePath & sVersion & "\" & application & security
SaveRegLong HKEY_CURRENT_USER, subpath, AccessVBOM, sValue
Exit Function
HandleErrors:
WriteDebug currentFunctionName & " : " & Err.Number & " " & Err.Description & " " & Err.Source
End Function
Public Function DeleteRegValue(application As String, sVersion As String)
On Error GoTo HandleErrors
Dim currentFunctionName As String
currentFunctionName = "SaveRegLong"
Const OfficePath = "Software\Policies\Microsoft\Office\"
Const security = "\Security"
Const AccessVBOM = "AccessVBOM"
Dim subpath As String
Dim retval As Long
Dim hKey As Long
subpath = OfficePath & sVersion & "\" & application & security
retval = RegOpenKeyEx(HKEY_CURRENT_USER, subpath, 0, KEY_WRITE, hKey)
If retval = ERROR_SUCCESS Then
retval = RegDeleteValue(hKey, AccessVBOM)
retval = RegCloseKey(hKey)
Exit Function
End If
HandleErrors:
WriteDebug currentFunctionName & " : " & Err.Number & " " & Err.Description & " " & Err.Source
End Function
Public Function CheckForAccesToWordVBProject1(wrd As Word.application, RestoreValue As Long) As Boolean
On Error Resume Next
CheckForAccesToWordVBProject1 = True
RestoreValue = -1
If val(wrd.Version) < 10# Then Exit Function
Set myProject = wrd.ActiveDocument.VBProject
If Err.Number <> 0 Then
Dim RegValue As Long
If GiveAccessToMacroProject(APP_WORD, wrd.Version, RegValue) Then
CheckForAccesToWordVBProject1 = True
RestoreValue = RegValue
Else
CheckForAccesToWordVBProject1 = False
End If
End If
End Function
Public Function CheckForAccesToWordVBProject(wrd As Word.application) As Boolean
On Error Resume Next
CheckForAccesToWordVBProject = True
If val(wrd.Version) < 10# Then Exit Function
Set myProject = wrd.ActiveDocument.VBProject
If Err.Number <> 0 Then
CheckForAccesToWordVBProject = False
End If
End Function
Public Function CheckForAccesToExcelVBProject1(xl As Excel.application, RestoreValue As Long) As Boolean
On Error Resume Next
CheckForAccesToExcelVBProject1 = True
RestoreValue = -1
If val(xl.Version) < 10# Then Exit Function
Dim displayAlerts As Boolean
displayAlerts = xl.displayAlerts
xl.displayAlerts = False
Set myProject = xl.ActiveWorkbook.VBProject
If Err.Number <> 0 Then
Dim RegValue As Long
If GiveAccessToMacroProject(APP_EXCEL, xl.Version, RegValue) Then
CheckForAccesToExcelVBProject1 = True
RestoreValue = RegValue
Else
CheckForAccesToExcelVBProject1 = False
End If
End If
xl.displayAlerts = displayAlerts
End Function
Public Function CheckForAccesToExcelVBProject(xl As Excel.application) As Boolean
On Error Resume Next
CheckForAccesToExcelVBProject = True
If val(xl.Version) < 10# Then Exit Function
Dim displayAlerts As Boolean
displayAlerts = xl.displayAlerts
xl.displayAlerts = False
Set myProject = xl.ActiveWorkbook.VBProject
If Err.Number <> 0 Then
CheckForAccesToExcelVBProject = False
End If
xl.displayAlerts = displayAlerts
End Function
Public Function CheckForAccesToPPVBProject1(pp As PowerPoint.application, pres As PowerPoint.Presentation, RestoreValue As Long) As Boolean
On Error Resume Next
CheckForAccesToPPVBProject1 = True
RestoreValue = -1
If val(pp.Version) < 10# Then Exit Function
Set myProject = pres.VBProject
If Err.Number <> 0 Then
Dim RegValue As Long
If GiveAccessToMacroProject(APP_PP, pp.Version, RegValue) Then
CheckForAccesToPPVBProject1 = True
RestoreValue = RegValue
Else
CheckForAccesToPPVBProject1 = False
End If
End If
End Function
Public Function CheckForAccesToPPVBProject(pp As PowerPoint.application, pres As PowerPoint.Presentation) As Boolean
On Error Resume Next
CheckForAccesToPPVBProject = True
If val(pp.Version) < 10# Then Exit Function
Set myProject = pres.VBProject
If Err.Number <> 0 Then
CheckForAccesToPPVBProject = False
End If
End Function
|