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?


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)


VBAWindows 10

Avatar of undefined
Last Comment
ste5an

8/22/2022 - Mon
ste5an

Cause you need to specify the hive as abbreviation. Thus HKCR should work.
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?
ste5an

Nope. "HKCR\Applications\excel.exe"
Your help has saved me hundreds of hours of internet surfing.
fblack61
Fabrice Lambert

Are you sure that you have enough privileges to read the key ?
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.




ASKER CERTIFIED SOLUTION
ste5an

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.