PDA

View Full Version : Solved: Trust Access to VB Project



MrRhodes2004
08-31-2006, 08:34 AM
I have written code that uses some of the VB libraries but it requires that the user all access to the VB Project libraries.

To turn this on, the user must go the the Security dialog box and the Trusted Publishers tab and check the box next to the "Trust access to Visual Basic Project".

For security purposes, I do not believe that this can be turned on programatically.

But, I would like code to check to see if it is checked. If it is NOT checked, it will open the security dialog box to the trusted publishers tab after telling the user they need to check the box.

If the user attempts to run the code they get the error, "Run-time error '1004'. Programmatic access to Visual Basic Project is not trusted.

How does one go about writing the code for this?

mvidas
08-31-2006, 09:14 AM
MrRhodes,

I do not have xl2002 or 2003, so I can't say for sure this will work, but I do know when this error appears for others. Perhaps you can use a function like:Function IsVBATrusted() As Boolean
Dim VBC As Object
Application.DisplayAlerts = False
On Error Resume Next
Set VBC = ThisWorkbook.VBProject.VBComponents.Item(1)
On Error GoTo 0
Application.DisplayAlerts = True
IsVBATrusted = Not VBC Is Nothing
End Function
I am not aware of any way to get that dialog to pop-up, though you may want to take a look at the Application.Dialogs collection, it may be in there for your version.

As to your question about what is/isn't possible in VBA, I would like to point you to http://www.google.com (http://www.google.com/)
:)

Matt

Bob Phillips
08-31-2006, 09:30 AM
Works fine in XP.

Zack Barresse
08-31-2006, 09:38 AM
Hi there,

There are ways, but they won't be posted to an open board like this. That would be like throwing candy out on the streets for hackers and telling them to come and get it; it'd only be a matter of time before everybody knew how to hack all projects! There would be pandemonium!!

Anyway, I was in the middle of making a function quite like Matt's to test this property, but it's about the same anyway. :)

Ken Puls
08-31-2006, 09:42 AM
Works fine in 2003 as well. :)

johnske
08-31-2006, 02:03 PM
Private Sub Workbook_Open()
Call AddRefsIfAccessAllowed
End Sub


Private Sub AddRefsIfAccessAllowed()

Dim Response As VbMsgBoxResult

'Test to ensure access is allowed
If Application.Version > 9 Then
Dim VisualBasicProject As Object
On Error Resume Next
Set VisualBasicProject = ActiveWorkbook.VBproject
If Not Err.Number = 0 Then
Response = Msgbox("Your current security settings do not allow the code in this workbook " & vbNewLine & _
" to work as designed and you will get some error messages." & vbNewLine & vbNewLine & _
"To allow the code to function correctly and without errors you need" & vbNewLine & _
" to change your security setting as follows:" & vbNewLine & vbNewLine & _
" 1. Select Tools - Macro - Security to show the security dialog" & vbNewLine & _
" 2. Click the 'Trusted Sources' tab" & vbNewLine & _
" 3. Place a checkmark next to 'Trust Access to Visual Basic Project'" & vbNewLine & _
" 4. Save - then Close and re-open the workbook" & vbNewLine & vbNewLine & _
"Do you want the security dialog shown now?", vbYesNoCancel + vbCritical)
If Response = vbYes Then Application.CommandBars("Macro").Controls("Security...").Execute
Exit Sub
End If
End If

End Sub