Problem : How can I switch on/off ‘Trust access to the VBA project object model’ in excel via VBA.

Problem : How can I switch on/off ‘Trust access to the VBA project object model’ in excel via VBA.

Hi,

I ahve written code in excel 2000 which copies a module from 1 workbook to another.
But when I want to execute this code in Excel 2007, then I get an error that this is not trusted.
I figured out that I have to enable the checkbox ‘Trust access to the VBA project object model’, but is it also possible to enable/disable this checkbox via VBA? Otherwise I have to ask all users to enable this checkbox themselves.

Thanks


Solution: How can I switch on/off ‘Trust access to the VBA project object model’ in excel via VBA.

Excel provides an option in the registry to allow programmatic access to VBA projects. The option is set on the Trusted Publishers tab of the Security dialog (Tools->Macros->Security, “Trust access to Visual Basic Project”). The option is stored in one of two places:

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Office\X.0\Excel\Security\AccessVBOM
HKEY_CURRENT_USER\SOFTWARE\Microsoft\Office\X.0\Excel\Security\AccessVBOM

The registry key value of the option is 0 (don’t allow access) or 1 (allow access). The key in the Local Machine hive has precedence and, if present, disables the option check box. If both keys are missing then the value of the option is off (don’t allow access).

The code below includes two functions, one for setting the option to allow programmatic access and another to retore the original setting.

The sample code below illustrates how to use these two routines.

Dim OriginalValue As Long
OriginalValue = AllowVBAccess
RestoreVBAccess OriginalValue

The two routines follow below.

[Begin Code Segment]

Public Function AllowVBAccess() As Long

‘ Allow VB access to VBA projects. The original setting is returned and should
‘ be stored in a long to be passed to RestoreVBAccess.

‘ Syntax

‘ AllowVBAccess()

Dim ScriptHost As Object
Dim OptionPath  As String
Dim LMOptionPath As String
Dim CUOptionPath As String
Dim OptionValue As Variant

OptionPath = Replace(“^1\SOFTWARE\Microsoft\Office\^2\Excel\Security\AccessVBOM”, “^2”, Application.Version)
LMOptionPath = Replace(OptionPath, “^1”, “HKLM”)
CUOptionPath = Replace(OptionPath, “^1”, “HKCU”)

AllowVBAccess = -1
Set ScriptHost = CreateObject(“WScript.Shell”)
On Error Resume Next
OptionValue = ScriptHost.RegRead(LMOptionPath)
On Error GoTo 0
If Not IsEmpty(OptionValue) Then
AllowVBAccess = OptionValue
ScriptHost.RegWrite LMOptionPath, 1, “REG_DWORD”
Else
On Error Resume Next
OptionValue = ScriptHost.RegRead(CUOptionPath)
On Error GoTo 0
If Not IsEmpty(OptionValue) Then
AllowVBAccess = OptionValue + 2
End If
ScriptHost.RegWrite CUOptionPath, 1, “REG_DWORD”
End If

End Function

Public Sub RestoreVBAccess( _
ByVal OriginalValue As Variant _
)

‘ Restore the VB access to VBA projects setting change by AllowVBAccess.

‘ Syntax

‘ RestoreVBAccess(OriginalValue)

‘ OriginalValue – The value returned from the routine AllowVBAccess.

Dim ScriptHost As Object
Dim OptionPath  As String
Dim LMOptionPath As String
Dim CUOptionPath As String
Dim OptionValue As Variant

OptionPath = Replace(“^1\SOFTWARE\Microsoft\Office\^2\Excel\Security\AccessVBOM”, “^2”, Application.Version)
LMOptionPath = Replace(OptionPath, “^1”, “HKLM”)
CUOptionPath = Replace(OptionPath, “^1”, “HKCU”)

Set ScriptHost = CreateObject(“WScript.Shell”)
Select Case OriginalValue
Case -1
ScriptHost.RegDelete CUOptionPath
Case 0
ScriptHost.RegWrite LMOptionPath, 0, “REG_DWORD”
Case 1
ScriptHost.RegWrite LMOptionPath, 1, “REG_DWORD”
Case 2
ScriptHost.RegWrite CUOptionPath, 0, “REG_DWORD”
Case 3
ScriptHost.RegWrite CUOptionPath, 1, “REG_DWORD”
End Select

End Sub

[End Code Segment]