Link to home
Start Free TrialLog in
Avatar of qeng
qeng

asked on

VBA - Unable to Read Windows 10 Registry Key

Why does the Function below always return 'False' when testing an existing Windows 10 Registry Key.  E.g.:


'================================================

Public Function REG_Key_Exists(regKeyStr As String) As Boolean
'
' Returns True if a Windows Registry Key exists
'
' 2021-11-30
'
' ref:  https://stackoverflow.com/questions/32345238/read-and-write-from-to-registry-in-vba

' define error handler
    On Error GoTo ErrorHandler
   
    CreateObject("WScript.Shell").RegRead (regKeyStr) ' will go to ErrorHandler if regKeyStr doesn't exist
   
    REG_Key_Exists = True

Exit Function
 
ErrorHandler:
    REG_Key_Exists = False
 
End Function

Sub REG_Key_Exists_TEST()
' declare test variables
    Dim regKeyStr As String
    Dim testResult As Boolean

' test an existing registry key
    regKeyStr = "HKEY_CLASSES_ROOT\Applications\excel.exe"
    testResult = REG_Key_Exists(regKeyStr)
   
    MsgBox "Windows Registry Key:" & vbCr & "  " & _
            regKeyStr & vbCr & vbCr & _
            "exists:" & vbCr & "  " & _
            testResult
Exit Sub

'===========================================


Do I need to add an additional reference to the VBA project in order to access the registry?

User generated image


I copied regKeyStr = "HKEY_CLASSES_ROOT\Applications\excel.exe"  using the Registry Editor's > 'Copy Key Name' function (so a) key exists and b) spelling should be correct)


Avatar of ste5an
ste5an
Flag of Germany image

Cause you need to specify the hive as abbreviation. Thus HKCR should work.
Avatar of qeng
qeng

ASKER

Hi Ste5an,
Do you mean replacing:
    regKeyStr = "HKEY_CLASSES_ROOT\Applications\excel.exe"
with
    regKeyStr = "HKCR_CLASSES_ROOT\Applications\excel.exe"
in the function call?
Nope. "HKCR\Applications\excel.exe"
Are you sure that you have enough privileges to read the key ?
Avatar of qeng

ASKER

Ste5an:
Using "HKCR\Applications\excel.exe" still returned a False value (ie Function is unable to 'read' that key and behaves as though the key doesn't exist).  
Assuming you have Excel installed on a WindowsOS computer, what happens if you run the code on your PC?  Does Function REG_Key_Exists (as I provided in my post) return True for you?

Fabrice:
As the User, I am able to use RegEdit to open the Registry and read or edit registry keys.  So  this  'User' has enough privileges to read the key when in the Windows environment.

What I don't know is if my VBA code within Excel (which is run under the same User account which can use RegEdit to successfully read the key) has enough privileges to read the registry.  In my original post, I'm showing which references the VBA project has.  I was asking if those are sufficient?  How can I check if the VBA project has sufficient privileges to read the registry?

With RegEdit I can see that the key whose existence I am trying to verify with Function REG_Key_Exists does in fact exist.  The function should be returning True but it returns False.

Example, if I open the registry and scroll to the target key, right click on it, and use 'Copy Key Name', I get:
HKEY_CLASSES_ROOT\Applications\excel.exe.  See screengrab below.

Sending this (existing) key name as a string to Function REG_Key_Exists returns False (it should return True)
Similarly sending string "HKCR\Applications\excel.exe", as Ste5an suggests, to Function REG_KEY_Exists also returns False.  See screengrabs below.

User generated image
User generated image
User generated image
ASKER CERTIFIED SOLUTION
Avatar of ste5an
ste5an
Flag of Germany 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