Using Windows Management Instrumentation (WMI) we can read, write and create registry keys anywhere in the registry.
The following is a series of procedures you can use to do so. These procedures are based on code found at Microsoft Access VBA Uses WMI to Read the Registry. This page will only cover methods for reading, writing and creating string values. See the above link for methods to do the same with binary and DWORD values.
First, the obligatory disclaimer: Write to your registry with caution. Overwriting existing keys can render your Windows installation unusable.
We'll start the code by defining a custom enumeration; the list of possible hives from which you can read.
Public Enum Hive HKEY_CLASSES_ROOT HKEY_CURRENT_USER HKEY_LOCAL_MACHINE HKEY_USERS HKEY_CURRENT_CONFIG End Enum
And we'll need a way for our functions to determine which hive was chosen (based on the custom enum):
Function GetHive(hivetype As Hive) As Variant
' return enumerated value depending on the hive chosen
Select Case hivetype
Case 0
GetHive = &H80000000 ' HKEY_CLASSES_ROOT
Case 1
GetHive = &H80000001 ' HKEY_CURRENT_USER
Case 2
GetHive = &H80000002 ' HKEY_LOCAL_MACHINE
Case 3
GetHive = &H80000003 ' HKEY_USERS
Case 4
GetHive = &H80000005 ' HKEY_CURRENT_CONFIG
End Select
End FunctionCheck Any String Value from the Registry
The function GetStringValFromRegistry takes our custom hive type, the registry key to retrieve, and the name of the entry whose value we want to retrieve. It also includes another function, GetStdRegProv, which calls the WMI service and returns the appropriate object.
Function GetStringValFromRegistry(hivetype As Hive, registryKey As String, _
keyValue As String) As String
Dim objReg As Object
Dim strKeyPath As String
Dim ValueName As String
Dim strValue As String
Set objReg = GetStdRegProv
strKeyPath = registryKey
ValueName = keyValue
' put key value into strValue variable
objReg.GetStringValue GetHive(hivetype), strKeyPath, ValueName, strValue
GetStringValFromRegistry = strValue
End Function
Function GetStdRegProv() As Object
' http://msdn.microsoft.com/en-us/library/aa394600(VS.85).aspx
Dim strComputer As String
strComputer = "."
Set GetStdRegProv = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" _
& strComputer & "\root\default:StdRegProv")
End FunctionSample usage
Let's call the function by checking the installation path for Office 2003. The registry key is located at
HKEY_LOCAL_MACHINE\Software\Microsoft\Office\11.0\Common\InstallRoot
and the key name is
Path
Sub TestRegistryReadAndWrite() Dim registryKey As String Dim keyName As String registryKey = "Software\Microsoft\Office\11.0\Common\InstallRoot" keyName = "Path" ' get string value from registry Debug.Print GetStringValFromRegistry(HKEY_LOCAL_MACHINE, registryKey, keyName) End Sub
Create Registry Keys
We can create registry keys anywhere in the registry we want. This is different from SaveSetting which only uses HKEY_CURRENT_USER\Software\VB and VBA Program Settings.
The CreateKey function takes the hive location where we want the new key, and the full key name and path where it should be placed.
Function CreateKey(hivetype As Hive, registryKey As String) Dim objReg As Object Dim strKeyPath As String Set objReg = GetStdRegProv strKeyPath = registryKey ' create the key objReg.CreateKey GetHive(hivetype), strKeyPath End Function
Notice how the GetHive function is used to return the constant value needed by WMI to determine which hive to use.
Sample usage
The following sample procedure will create the key "NewKey" in HKEY_LOCAL_MACHINE\Software.
Sub TestRegistryReadAndWrite() Dim registryKey As String registryKey = "Software\NewKey" ' create new key CreateKey HKEY_LOCAL_MACHINE, registryKey End Sub
Create Registry (String) Values
Finally, we can create new registry values anywhere in the registry. This is as simple as calling the SetStringValue function and passing in the appropriate values.
Function CreateKeyValue(hivetype As Hive, registryKey As String, _
keyName As String, keyValue As String)
Dim objReg As Object
Dim strKeyPath As String
Dim strValueName As String
Dim strValue As String
Set objReg = GetStdRegProv
strKeyPath = registryKey
strValueName = keyName
strValue = keyValue
' create the key value
objReg.SetStringValue GetHive(hivetype), strKeyPath, strValueName, strValue
End FunctionSample usage:
Sub TestRegistryReadAndWrite() Dim registryKey As String Dim keyValue As String Dim keyName As String registryKey = "Software\NewKey" keyName = "NewName" keyValue = "This is a value" ' add name/value to new key CreateKeyValue HKEY_LOCAL_MACHINE, registryKey, keyName, keyValue End Sub
Putting it all together
Let's see how we can read, create and write to the registry all in one procedure. Here are all of the sample procedures rolled into one. After checking the install path for Office 2003, we'll create a new registry key, populate it with a name/value, and then retrieve the new value to verify that it was added.
Sub TestRegistryReadAndWrite() Dim registryKey As String Dim keyValue As String Dim keyName As String registryKey = "Software\Microsoft\Office\11.0\Common\InstallRoot" keyName = "Path" ' get string value from registry Debug.Print GetStringValFromRegistry(HKEY_LOCAL_MACHINE, registryKey, keyName) registryKey = "Software\NewKey" ' create new key CreateKey HKEY_LOCAL_MACHINE, registryKey keyName = "NewName" keyValue = "This is a value" ' add name/value to new key CreateKeyValue HKEY_LOCAL_MACHINE, registryKey, keyName, keyValue ' check our newly added value Debug.Print GetStringValFromRegistry(HKEY_LOCAL_MACHINE, registryKey, keyName) End Sub
