Checking if a key exists in Registry (1 Viewer)

JohnPapa

Registered User.
Local time
Today, 19:38
Joined
Aug 15, 2010
Messages
954
The following works if the key exists in the Registry
Function RegKeyExists2(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
RegKeyExists2 = True
Exit Function
ErrorHandler:
'key was not found
RegKeyExists2 = False
End Function

Private Sub Command11_Click()
If RegKeyExists2("HKEY_CURRENT_USER\Software\Microsoft\Office\15.0\Access\Security\Trusted Locations\Location10\") = False Then
MsgBox ("Code for adding registry key")
Else
MsgBox ("Key exists - do nothing")
End If
If the key does not exist I get the error seen in the attachment.


Thanks
John
 

Attachments

  • Registry.png
    Registry.png
    19.6 KB · Views: 144

spikepl

Eledittingent Beliped
Local time
Today, 18:38
Joined
Nov 3, 2010
Messages
6,142
Is there question/issue here?
 

JohnPapa

Registered User.
Local time
Today, 19:38
Joined
Aug 15, 2010
Messages
954
It gives the runtime error and does not enter the ErrorHandler.

The error occurs at line

Code:
   myWS.RegRead i_RegKey
 

JohnPapa

Registered User.
Local time
Today, 19:38
Joined
Aug 15, 2010
Messages
954
Thanks for the reply,

Code:
Your code does not follow this exactly: [URL="http://vba-corner.livejournal.com/3054.html"][COLOR=#0066cc]http://vba-corner.livejournal.com/3054.html[/COLOR][/URL]

Can you please point out what you mean?

Code:
besides, if this is supposed to be used to set trusted location, you could just set it, whether or not it had been set before, using the AddPAthXXX.exe from [URL="http://www.accessribbon.de/en/?Trust_Center:Trusted_Locations"][COLOR=#0066cc]http://www.accessribbon.de/en/?Trust...sted_Locations[/COLOR][/URL]

Yes, it is supposed to be used to set a trusted location and would like to be able to check whether it has been set already.
 

JohnPapa

Registered User.
Local time
Today, 19:38
Joined
Aug 15, 2010
Messages
954
Also, AddPath did not work for a folder which corresponds to a mapped drive.

In any case AddPath would not be practical in deployment of a commercial system.
 

spikepl

Eledittingent Beliped
Local time
Today, 18:38
Joined
Nov 3, 2010
Messages
6,142
In teh code I linked to it returns a string or a "" if key does not exist. This may have not much to do with anything, but your declarations do not exist or are not shown.

Also, AddPath did not work for a folder which corresponds to a mapped drive.
Ah s it only works on local drives?
 

JohnPapa

Registered User.
Local time
Today, 19:38
Joined
Aug 15, 2010
Messages
954
Code:
Ah s it only works on local drives?

I tried executing AddPath inside a mapped drive and it came back with a big X and something in German, I believe.

Code:
In teh code I linked to it returns a string or a "" if key does not exist. This may have not much to do with anything, but your declarations do not exist or are not shown.

I believe that if it does not find what is requested a Null is returned. In my case as I mentioned the code execution stops at

Code:
myWS.RegRead i_RegKey

and does not enter the Error handling.

Can you think of a reason why it does not?
 

JohnPapa

Registered User.
Local time
Today, 19:38
Joined
Aug 15, 2010
Messages
954
Hi all,

The problem of the Security related messages was solved when the following key was created

Code:
    HKEY_CURRENT_USER\Software\Microsoft\Office\15.0\Access\Security\Trusted Locations\AllowNetworkLocations

and set to 1

Thanks for your help
 

Users who are viewing this thread

Top Bottom