VBA Developer Macro Setting - Trust Access to the VBA project object model - Toggle to trust or not trust

Status
Not open for further replies.

TkdKidSnake

Board Regular
Joined
Nov 27, 2012
Messages
245
Office Version
  1. 365
Platform
  1. Windows
Good afternoon all,

I am after some help with some VBA coding to do the following:

1. Toggle trust access to trust i.e. checked
2. Delete all macros/VBA code
3. Toggle trust access to NOT trust i.e. unchecked

I have searched for specific code to do this without much luck, so if anyone could help it would be greatly appreciated.
 

Attachments

  • VBATrust.JPG
    VBATrust.JPG
    31.7 KB · Views: 58

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
I very much doubt that that is possible. After all, if it was possible then there would be no point in having it, as any malicious code could give itself access to the project.
 
Upvote 0
I very much doubt that that is possible. After all, if it was possible then there would be no point in having it, as any malicious code could give itself access to the project.
I have managed to allow this in the trust centre with the following code, however if possible I would like to be able to turn trust on run the code and then turn it off to enable the protection again, with-out the need for notification.

VBA Code:
#If VBA7 Then
    Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
#Else
    Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
#End If

Sub CheckTrustAccess()
Dim strStatus, strOpp, strCheck As String
Dim bEnabled As Boolean
If Not VBAIsTrusted Then
'ask the user if they want me to try to programatically toggle trust access. If I fail, give them directions.
    bEnabled = False
    strStatus = "DISABLE"
    strOpp = "ENABLE"
    v = MsgBox("Trust Access to the VBA Project Object Model is " & strStatus & "D." & Chr(10) & Chr(10) & _
     "Would you like me to attempt to " & strOpp & " it?", vbYesNo, strOpp & " Trust Access?")
Else
    bEnabled = True
    strStatus = "ENABLE"
    strOpp = "DISABLE"
    'v = MsgBox("Trust Access to the VBA Project Object Model is " & strStatus & "D." & Chr(10) & Chr(10) & _
     "Would you like me to attempt to " & strOpp & " it?", vbYesNo, strOpp & " Trust Access?")
Call HideBlanks

End If

    If v = 6 Then
        'try to toggle trust
        Call ToggleTrust(bEnabled)
        If VBAIsTrusted = bEnabled Then
            'if ToggleTrust fails to programatically toggle trust
            MsgBox "I failed to " & strOpp & " Trust Access." & Chr(10) & Chr(10) & _
            "To " & strOpp & " this setting yourself:" & Chr(10) & Chr(10) & _
            Space(5) & "1) Click " & Chr(145) & "File-> Options-> Trust Center-> Trust Center Settings" & Chr(146) & Chr(10) & _
            Space(5) & "2) Click Macro Settings" & Chr(10) & _
            Space(5) & "3) Toggle the box next to ""Trust Access to the VBA project object model""", vbOKOnly, "Auto " & strOpp & " Failed"
            End
        Else
            MsgBox "I successfully " & strOpp & "D Trust Access." & Chr(10) & Chr(10) & _
            "To " & strStatus & " this setting yourself:" & Chr(10) & Chr(10) & _
            Space(5) & "1) Click " & Chr(145) & "File-> Options-> Trust Center-> Trust Center Settings" & Chr(146) & Chr(10) & _
            Space(5) & "2) Click Macro Settings" & Chr(10) & _
            Space(5) & "3) Toggle the box next to ""Trust Access to the VBA project object model""", vbOKOnly, "Auto " & strOpp & " Failed"
        End If
    Else
        MsgBox "To manually " & strOpp & " Trust Access:" & Chr(10) & Chr(10) & _
            Space(5) & "1) Click " & Chr(145) & "File-> Options-> Trust Center-> Trust Center Settings" & Chr(146) & Chr(10) & _
            Space(5) & "2) Click Macro Settings" & Chr(10) & _
            Space(5) & "3) Toggle the box next to ""Trust Access to the VBA project object model""", vbOKOnly, "How to " & strOpp & " Trust Access"
        End
    End If

If VBAIsTrusted Then
    'if you want to write your own macro, do it here. You only get here if access is trusted
    Call HideBlanks
End If

End Sub

Private Function VBAIsTrusted() As Boolean
Dim a1 As Integer
On Error GoTo Label1
a1 = ActiveWorkbook.VBProject.VBComponents.Count
VBAIsTrusted = True
Exit Function
Label1:
VBAIsTrusted = False
End Function

Private Sub ToggleTrust(bEnabled As Boolean)
Dim b1 As Integer, i As Integer
Dim strkeys As String
On Error Resume Next
    Do While i <= 2 'try to sendkeys 3 times
        Sleep 100
    strkeys = "%tms%v{ENTER}"
        Call SendKeys(Trim(strkeys)) 'ST%V{ENTER}")
        DoEvents
        If VBAIsTrusted <> bEnabled Then Exit Do 'successfully toggled trust
        Sleep (100)
        i = i + 1
    Loop
End Sub
 
Upvote 0
This topic is basically, however well-intentioned, about circumventing security measures, and is therefore against forum rules, I'm afraid. This thread is therefore closed, and will probably be removed in due course.
 
Upvote 0
Status
Not open for further replies.

Forum statistics

Threads
1,214,429
Messages
6,119,433
Members
448,897
Latest member
ksjohnson1970

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top