Link to home
Start Free TrialLog in
Avatar of twarrenb
twarrenbFlag for United States of America

asked on

Code to determine whether a folder is a Trusted Location

The following code from ...

https://www.experts-exchange.com/questions/24197322/Script-for-adding-Trusted-Locations-in-MS-Office-2007.html?sfQueryTermInfo=1+locat+trust

makes a folder a Trusted Location.

Can this code be modified to first determine whether or not a folder is already a Trusted Location and, if not, then execute the code to make it a Trusted Location?

Const HKEY_CLASSES_ROOT = &H80000000
Const HKEY_CURRENT_USER = &H80000001
Const HKEY_LOCAL_MACHINE = &H80000002
Const HKEY_USERS = &H80000003
Const HKEY_CURRENT_CONFIG = &H80000005
 
strProgram = "Excel"
strFolder = "c:\files"
strDescription = "my custom folder"
blnAllowSubFolders = True
blnAllowNetworkLocations = True
 
strParentKey = "Software\Microsoft\Office\12.0\" & strProgram & "\Security\Trusted Locations"
intHighest = 0
 
Set objRegistry = GetObject("winmgmts:\\.\root\default:StdRegProv")
 
objRegistry.EnumKey HKEY_CURRENT_USER, strParentKey, arrChildKeys
 
For Each strChildKey in arrChildKeys
    If CInt(Mid(strChildKey, 9)) > intHighest Then
        intHighest = CInt(Mid(strChildKey, 9))
    End If
Next
 
strNewKey = strParentKey & "\Location" & CStr(intHighest + 1)
 
objRegistry.CreateKey HKEY_CURRENT_USER, strNewKey
objRegistry.SetStringValue HKEY_CURRENT_USER, strNewKey, "Path", strFolder
objRegistry.SetStringValue HKEY_CURRENT_USER, strNewKey, "Description", strDescription
 
If blnAllowSubFolders Then
    objRegistry.SetDWORDValue HKEY_CURRENT_USER, strNewKey, "AllowSubFolders", 1
End If
 
If blnAllowNetworkLocations Then
    objRegistry.SetDWORDValue HKEY_CURRENT_USER, strParentKey, "AllowNetworkLocations", 1
End If
ASKER CERTIFIED SOLUTION
Avatar of Robberbaron (robr)
Robberbaron (robr)
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of twarrenb

ASKER

This code works fine except for the messages.  At each of the following, run-time error '424' ("Object required) appears.
Sorry.  Meant to say:

This code works fine except for the script messages.  At each of the following, run-time error '424' ("Object required) appears.

WScript.Echo ">" & FolderPath & " is already trusted"
WScript.Echo ">" & FolderPath & " added as trusted"
WScript.Echo strSubPath & "=" & strValue
1. you are running this as a VBS ?
2. the variable names arent the same as mine which is ok but have you placed an option explicit at the top that would be throwing the exception for FolderPath ?

need to post more of your code to debug as my code worked fine on an XP SP3 box.
I'm running your code as Access (2010) VB on Windows 7.   Option Explicit is at the top of the module.

I ran your original code with only one modification, namely, strParentKey = ...\Office\14.0\... rather than
...\Office\12.0\...  I got the same error message.

However, the error (if it is such) is of no consequence.  The code works fine with the three WScript.Echo
lines removed.