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)