Add Trusted Locations via Code (1 Viewer)

riti90

Registered User.
Local time
Today, 15:07
Joined
Dec 20, 2017
Messages
44
Hi All,

I fount a piece of code for adding Trusted Locations via Code and did a few edits.
Hope it will come handy to someone.

Code:
Option Compare Database
Option Explicit

'Reading from the Registry:

'reads the value for the registry key i_RegKey
'if the key cannot be found, the return value is ""
Function RegKeyRead(i_RegKey As String) As String
Dim myWS As Object

  On Error Resume Next
  'access Windows scripting
  Set myWS = CreateObject("WScript.Shell")
  'read key from registry
  RegKeyRead = myWS.RegRead(i_RegKey)
End Function

'Checking if a Registry key exists:

'returns True if the registry key i_RegKey was found
'and False if not
Function RegKeyExists(i_RegKey As String) As Boolean
Dim myWS As Object

  On Error GoTo ErrorHandler
  'access Windows scripting
  Set myWS = CreateObject("WScript.Shell")
  'try to read the registry key
  myWS.RegRead i_RegKey
  'key was found
  RegKeyExists = True
  Exit Function
  
ErrorHandler:
  'key was not found
  RegKeyExists = False
End Function

'Saving a Registry key:

'sets the registry key i_RegKey to the
'value i_Value with type i_Type
'if i_Type is omitted, the value will be saved as string
'if i_RegKey wasn't found, a new registry key will be created
Sub RegKeySave(i_RegKey As String, _
               i_Value As String, _
      Optional i_Type As String)
Dim myWS As Object

  'access Windows scripting
  Set myWS = CreateObject("WScript.Shell")
  'write registry key
  myWS.RegWrite i_RegKey, i_Value, i_Type

End Sub

'Deleting a key from the Registry:

'deletes i_RegKey from the registry
'returns True if the deletion was successful,
'and False if not (the key couldn't be found)
Function RegKeyDelete(i_RegKey As String) As Boolean
Dim myWS As Object

  On Error GoTo ErrorHandler
  'access Windows scripting
  Set myWS = CreateObject("WScript.Shell")
  'delete registry key
  myWS.RegDelete i_RegKey
  'deletion was successful
  RegKeyDelete = True
  Exit Function

ErrorHandler:
  'deletion wasn't successful
  RegKeyDelete = False
End Function


Function TestRegistry()
Dim myRegKey As String
Dim myRegDate As String
Dim myRegAllowFolders As String
Dim myRegDescription As String
Dim myRegKeyValue As String
Dim myRegAllowFoldersValue As String
Dim myRegDateValue As String
Dim myRegDescriptionValue As String
Dim myRegistryKey As String
Dim myDataType As String
Dim myDataTypeFolder As String
Dim strVersion As String

strVersion = Application.version

myRegistryKey = "HKEY_CURRENT_USER\Software\Microsoft\Office\" & strVersion & "\Access\Security\Trusted Locations\MyTrustedPath"   '16.0 is Office 16/365, 12.0 is Office 2007
myRegKey = "HKEY_CURRENT_USER\Software\Microsoft\Office\" & strVersion & "\Access\Security\Trusted Locations\MyTrustedPath\Path"
myRegAllowFolders = "HKEY_CURRENT_USER\Software\Microsoft\Office\" & strVersion & "\Access\Security\Trusted Locations\MyTrustedPath\AllowSubFolders"
myRegDate = "HKEY_CURRENT_USER\Software\Microsoft\Office\" & strVersion & "\Access\Security\Trusted Locations\MyTrustedPath\Date"
myRegDescription = "HKEY_CURRENT_USER\Software\Microsoft\Office\" & strVersion & "\Access\Security\Trusted Locations\MyTrustedPath\Description"

    myDataType = "REG_SZ"
    myDataTypeFolder = "REG_DWORD"
    
myRegKeyValue = Application.CurrentProject.Path & "\"
myRegAllowFoldersValue = "00000001"
myRegDateValue = (Now())
myRegDescriptionValue = "Your Description...."


  If myRegistryKey = "" Then Exit Function
  'check if key exists
  If RegKeyExists(myRegistryKey) = True Or RegKeyExists(myRegKey) = True Or RegKeyExists(myRegAllowFolders) = True Or RegKeyExists(myRegDate) = True Then
 
  Exit Function
  
  Else
  
     If myRegKeyValue <> "" Then
      'save/create registry key with new value
      RegKeySave myRegKey, myRegKeyValue, myDataType
      RegKeySave myRegAllowFolders, myRegAllowFoldersValue, myDataTypeFolder
      RegKeySave myRegDate, myRegDateValue, myDataType
      RegKeySave myRegDescription, myRegDescriptionValue, myDataType

    End If
  End If
  
End Function

Regards,

Margarit :):)
 

Ranman256

Well-known member
Local time
Today, 10:07
Joined
Apr 9, 2015
Messages
4,337
Instead of hard coding the paths, put them in a table THEN cycle thru the records adding them to the registry.

Path,
TrustedPath
AllowSubFolders,
Etc...
 

isladogs

MVP / VIP
Local time
Today, 15:07
Joined
Jan 14, 2017
Messages
18,236
@riti90
I think there is a catch-22 to using this code
Functions can't be run until the Enable Content button is clicked.
Doing so will automatically make the document trusted though not the location itself

I have similar (but much shorter) code which is run from an installer script for apps distributed from my website. This means the app location is trusted before the app is run for the first time so the security bar is never seen

Other points:
1. Using the separate EXE file available from Gunter Avenius' website (see link in arnel's post) works in the same way as my installer script. However be aware that the messages are in German. Not sure if an English language version is available.

2. Because this code should only need to be run once for the location the database is saved to, I see little point saving the values to a table as ranman suggests.

3. Currently this writes to a reg key MyTrustedPath. If the same code is used in several apps, it will exit the function without setting the location as trusted. Suggest you check if the key exists and if so, change the path to MyTrustedPath2 or Location2 or similar

HTH
 

AccessBlaster

Registered User.
Local time
Today, 07:07
Joined
May 22, 2010
Messages
5,953
@riti90
I think there is a catch-22 to using this code
Functions can't be run until the Enable Content button is clicked.
I was thinking the exact same thing, you usually get the error before you can run the code.
 

JoeNoEskimo

New member
Local time
Today, 09:07
Joined
Mar 29, 2019
Messages
3
Hi All,

I fount a piece of code for adding Trusted Locations via Code and did a few edits.
Hope it will come handy to someone.

Code:
Option Compare Database
Option Explicit

'Reading from the Registry:

'reads the value for the registry key i_RegKey
'if the key cannot be found, the return value is ""
Function RegKeyRead(i_RegKey As String) As String
Dim myWS As Object

  On Error Resume Next
  'access Windows scripting
  Set myWS = CreateObject("WScript.Shell")
  'read key from registry
  RegKeyRead = myWS.RegRead(i_RegKey)
End Function

'Checking if a Registry key exists:

'returns True if the registry key i_RegKey was found
'and False if not
Function RegKeyExists(i_RegKey As String) As Boolean
Dim myWS As Object

  On Error GoTo ErrorHandler
  'access Windows scripting
  Set myWS = CreateObject("WScript.Shell")
  'try to read the registry key
  myWS.RegRead i_RegKey
  'key was found
  RegKeyExists = True
  Exit Function
 
ErrorHandler:
  'key was not found
  RegKeyExists = False
End Function

'Saving a Registry key:

'sets the registry key i_RegKey to the
'value i_Value with type i_Type
'if i_Type is omitted, the value will be saved as string
'if i_RegKey wasn't found, a new registry key will be created
Sub RegKeySave(i_RegKey As String, _
               i_Value As String, _
      Optional i_Type As String)
Dim myWS As Object

  'access Windows scripting
  Set myWS = CreateObject("WScript.Shell")
  'write registry key
  myWS.RegWrite i_RegKey, i_Value, i_Type

End Sub

'Deleting a key from the Registry:

'deletes i_RegKey from the registry
'returns True if the deletion was successful,
'and False if not (the key couldn't be found)
Function RegKeyDelete(i_RegKey As String) As Boolean
Dim myWS As Object

  On Error GoTo ErrorHandler
  'access Windows scripting
  Set myWS = CreateObject("WScript.Shell")
  'delete registry key
  myWS.RegDelete i_RegKey
  'deletion was successful
  RegKeyDelete = True
  Exit Function

ErrorHandler:
  'deletion wasn't successful
  RegKeyDelete = False
End Function


Function TestRegistry()
Dim myRegKey As String
Dim myRegDate As String
Dim myRegAllowFolders As String
Dim myRegDescription As String
Dim myRegKeyValue As String
Dim myRegAllowFoldersValue As String
Dim myRegDateValue As String
Dim myRegDescriptionValue As String
Dim myRegistryKey As String
Dim myDataType As String
Dim myDataTypeFolder As String
Dim strVersion As String

strVersion = Application.version

myRegistryKey = "HKEY_CURRENT_USER\Software\Microsoft\Office\" & strVersion & "\Access\Security\Trusted Locations\MyTrustedPath"   '16.0 is Office 16/365, 12.0 is Office 2007
myRegKey = "HKEY_CURRENT_USER\Software\Microsoft\Office\" & strVersion & "\Access\Security\Trusted Locations\MyTrustedPath\Path"
myRegAllowFolders = "HKEY_CURRENT_USER\Software\Microsoft\Office\" & strVersion & "\Access\Security\Trusted Locations\MyTrustedPath\AllowSubFolders"
myRegDate = "HKEY_CURRENT_USER\Software\Microsoft\Office\" & strVersion & "\Access\Security\Trusted Locations\MyTrustedPath\Date"
myRegDescription = "HKEY_CURRENT_USER\Software\Microsoft\Office\" & strVersion & "\Access\Security\Trusted Locations\MyTrustedPath\Description"

    myDataType = "REG_SZ"
    myDataTypeFolder = "REG_DWORD"
   
myRegKeyValue = Application.CurrentProject.Path & "\"
myRegAllowFoldersValue = "00000001"
myRegDateValue = (Now())
myRegDescriptionValue = "Your Description...."


  If myRegistryKey = "" Then Exit Function
  'check if key exists
  If RegKeyExists(myRegistryKey) = True Or RegKeyExists(myRegKey) = True Or RegKeyExists(myRegAllowFolders) = True Or RegKeyExists(myRegDate) = True Then

  Exit Function
 
  Else
 
     If myRegKeyValue <> "" Then
      'save/create registry key with new value
      RegKeySave myRegKey, myRegKeyValue, myDataType
      RegKeySave myRegAllowFolders, myRegAllowFoldersValue, myDataTypeFolder
      RegKeySave myRegDate, myRegDateValue, myDataType
      RegKeySave myRegDescription, myRegDescriptionValue, myDataType

    End If
  End If
 
End Function

Regards,

Margarit :):)
Thanks, it worked well for what I wanted to do.
 

mloucel

Member
Local time
Today, 07:07
Joined
Aug 5, 2020
Messages
156
@riti90
I think there is a catch-22 to using this code
Functions can't be run until the Enable Content button is clicked.
Doing so will automatically make the document trusted though not the location itself

I have similar (but much shorter) code which is run from an installer script for apps distributed from my website. This means the app location is trusted before the app is run for the first time so the security bar is never seen

Other points:
1. Using the separate EXE file available from Gunter Avenius' website (see link in arnel's post) works in the same way as my installer script. However be aware that the messages are in German. Not sure if an English language version is available.

2. Because this code should only need to be run once for the location the database is saved to, I see little point saving the values to a table as ranman suggests.

3. Currently this writes to a reg key MyTrustedPath. If the same code is used in several apps, it will exit the function without setting the location as trusted. Suggest you check if the key exists and if so, change the path to MyTrustedPath2 or Location2 or similar

HTH
Where is in your website, I tried to find it but I got lost..
"I have similar (but much shorter) code which is run from an installer script for apps"
 

moke123

AWF VIP
Local time
Today, 10:07
Joined
Jan 11, 2013
Messages
3,920
 

isladogs

MVP / VIP
Local time
Today, 15:07
Joined
Jan 14, 2017
Messages
18,236
Where is in your website, I tried to find it but I got lost..
"I have similar (but much shorter) code which is run from an installer script for apps"

Did you try doing a site search for trusted locations?
 

mloucel

Member
Local time
Today, 07:07
Joined
Aug 5, 2020
Messages
156
Did you try doing a site search for trusted locations?
Thanks that helped a lot, I didn't know there was code to do that, which is great, I can deploy Access runtime, and create a routine that runs the first time to check and add trusted locations.

Thanks so much @isladogs your help is really appreciated.
 

isladogs

MVP / VIP
Local time
Today, 15:07
Joined
Jan 14, 2017
Messages
18,236
Thanks that helped a lot, I didn't know there was code to do that, which is great, I can deploy Access runtime, and create a routine that runs the first time to check and add trusted locations.

Thanks so much @isladogs your help is really appreciated.
No. That won't work as the code can't run until the location is trusted as previously explained. Catch-22!
You need to set the registry entry outside of Access before running the app.

Alternatively purchase a code signing certificate and use it to sign your projects.
Doing that means the file can be saved to any location and will be automatically trusted
 

mloucel

Member
Local time
Today, 07:07
Joined
Aug 5, 2020
Messages
156
No. That won't work as the code can't run until the location is trusted as previously explained. Catch-22!
You need to set the registry entry outside of Access before running the app.

Alternatively purchase a code signing certificate and use it to sign your projects.
Doing that means the file can be saved to any location and will be automatically trusted
Got it, I will google for a the certificate, thanks for your help.
 

isladogs

MVP / VIP
Local time
Today, 15:07
Joined
Jan 14, 2017
Messages
18,236
Try K Software who are reseller for Sectigo certificates (and more)
The certificates aren't cheap and you will need to 'jump through hoops' in order to pass the validation checks - so it may take a few weeks to get up & running
 

mloucel

Member
Local time
Today, 07:07
Joined
Aug 5, 2020
Messages
156
Try K Software who are reseller for Sectigo certificates (and more)
The certificates aren't cheap and you will need to 'jump through hoops' in order to pass the validation checks - so it may take a few weeks to get up & running
My boss, told me not to buy it, so I guess, I will have to do it manually, I'll test your code as much as I can but so far I think it worked, I added a setup database with a field called TrustedLocation = false, so as soon as the app runs the first time your code runs and sets TL to TRUE so it won't run again, it worked on my computer I will test in a couple more that I have with runtime, see what happens, if it works then I'll go to some of the computers and will deploy and see what happens.
 

isladogs

MVP / VIP
Local time
Today, 15:07
Joined
Jan 14, 2017
Messages
18,236
In that case, wouldn't it be easier to just run a registry script from yet file before opening the app?
 

mloucel

Member
Local time
Today, 07:07
Joined
Aug 5, 2020
Messages
156
In that case, wouldn't it be easier to just run a registry script from yet file before opening the app?
I thought about it, but I don't know how nor my expertise go that far, I tried already with a full install of Access 365 and a runtime edition, and it worked, I added a little trap, so that it does it at Splash and if the the TL is False do its thing and displays a message to the End User [BS] your Database has been Registered, please open the app again. Quits and once the EU starts again all is honky dory.
Worked in 3 computers, mine and the 2 others, I'm going to try with WIN 10 as well, but so far looks promissory.
What I did:
Form Record source = SetupT
On Load event
check on setup for TL
If TL= False
-- do the registry routine
make TL = TRUE and save
endif

Simple easy and it worked.
 

Users who are viewing this thread

Top Bottom