'************************************
'*
'* Registry Key Exists (Function)
'* Version 1.1
'*
'************************************
'This function checks to see if a passed registry key exists, and
'returns true if it does
'
'Requirements: The registry key you are looking for (RegistryKey)
Function RegistryKeyExists (fncStrRegistryKey)
'Ensure the last character is a backslash (\) - if it isn't, add one
If (Right(fncStrRegistryKey, 1) <> "\") Then
'Add a backslash
fncStrRegistryKey = fncStrRegistryKey & "\"
End If
'If there isnt the key when we read it, it will return an error, so we need to resume
On Error Resume Next
'Try reading the key
WshShell.RegRead fncStrRegistryKey
'Catch the error
Select Case Err
'Error Code 0 = 'success'
Case 0
fncStrRegistryKeyExists = true
'This checks for the (Default) value existing (but being blank); as well as key's not existing at all (same error code)
Case &h80070002
'Read the error description, removing the registry key from that description
strErrDescription = Replace(Err.description, fncStrRegistryKey, "")
'Clear the error
Err.clear
'Read in a registry entry we know doesn't exist (to create an error description for something that doesnt exist)
WshShell.RegRead "HKEY_ERROR\"
'The registry key exists if the error description from the HKEY_ERROR RegRead attempt doesn't match the error
'description from our fncStrRegistryKey RegRead attempt
If (strErrDescription <> Replace(Err.description, "HKEY_ERROR\", "")) Then
RegistryKeyExists = true
Else
RegistryKeyExists = false
End If
'Any other error code is a failure code
Case Else
RegistryKeyExists = false
End Select
'Turn error reporting back on
On Error Goto 0
End Function