Accessing the registry


The first step in accessing the registry collections is to declare a node object variable and connect it to a registry node:

Dim nodesTop As New CRegNode
‘ Connect to first-level node by name
nodesTop.Create “Software\VB and VBA Program Settings”

The Create method can be used in several ways. Its signature looks like this:

Sub Create(vIndex As Variant, _
Optional RootKey As Long = HKEY_CURRENT_USER, _
Optional AccessRights As Long = KEY_ALL_ACCESS)

You’ll need to provide a RootKey parameter to connect to anything other than HKEY_CURRENT_USER. The vIndex parameter is a Variant so that it can take either a string or numeric argument. It can be a key name (as shown above), a remote computer name, or the handle of a previously opened key.


You can also connect a registry node by using the default Key property. Here are some examples:

‘ Connect HKEY_CLASSES_ROOT node
nodesTop.Key = HKEY_CLASSES_ROOT
‘ Connect VBCore.CAbout node in current node (HKEY_CLASSES_ROOT)
nodesTop.Key = “VBCore.CAbout”
‘ Connect Software node in specified root HKEY_LOCAL_MACHINE
nodesTop.Key(HKEY_LOCAL_MACHINE) = “Software”
‘ Open first node of current node
nodesTop.Key(nodesTop.Key) = 1

Once you’ve connected to a node, you can read its item values by name:

v = node.Items(“Bytes”)

Or you can get an item’s value by the item’s position number:

v = node.Items(1)

Either way you get back a Variant that could contain a Long, a String, or an
array of Bytes. You have to check the type with VarType to decide what to do with it. The sample simply converts any type to a string, but real programs might have to do something more sophisticated—especially with binary data stored in Byte arrays.


You can also set item values of any registry type. Here’s how to add binary data named Bytes:

Dim ab() As Byte
‘ Add bytes item
ab = “The bytes”
node.AddItem ab, “Bytes”

You can also add strings containing environment variables such as this string named ExpandString:

node.AddItem “A %TEMP% string”, “ExpandString”

The string will be saved in the registry as is, with the percent signs intact, but it will be extracted with the TEMP environment variable expanded. Here are a few more examples showing how item values can be added to named nodes:

node(“SecondLevel1”).AddItem “DefaultString”
node(“SecondLevel1”).AddItem “Stuff”, “Value1”
node(“SecondLevel2”).AddItem 689, “Value1”

The first argument is the value of the item, the second is its name. If you don’t give a name, the value will become the default value, which will always be stored as a string even if it isn’t one.


You can remove items by name or by position:

node.RemoveItem 1
node.RemoveItem “String”

You can also remove nodes by name or position, but whether you succeed will depend on whether the node has children. Here’s an attempt to remove a childless node:

f = nodesTop.RemoveNode(“FirstLevel”, AllChild:=False)

This call will fail if the specified node has children. You might want to specify the optional argument as False to avoid accidentally deleting large branches from the registry. The default is to remove the node and all its children. Note that a node can’t remove itself.