9.9. Registry Routines
The Shell object provides Windows Registry access through the RegRead , RegWrite , and RegDelete methods .
When accessing a Registry key, you must specify the path. The path is built from the Registry hive name (the name of one of the major Registry branches described in Chapter 8), followed by the path to the key separated by backslash characters . Table 9-8 lists the hive names .
Short | Long |
---|---|
HKCU | HKEY_CURRENT_USER |
HKLM | HKEY_LOCAL_MACHINE |
HKCR | HKEY_CLASSES_ROOT |
HKEY_USERS | |
HKEY_CURRENT_CONFIG | |
HKEY_DYN_DATA |
For example, the path to the Windows version number would be represented as HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\CurrentVersion .
One way to easily get the path for registry values is to use the RegEdit application to search for Registry information and copy the key path to the clipboard using Edit Copy Key Name. The Registry routines do not provide the ability to list any values under a particular key, so you need to know the path to any Registry values you wish to reference.
RegRead reads the registry value from the specified Registry path:
strVal = objShell.RegRead( strKeyPath )
strKeyPath is a path to the Registry value you wish to read.
Set objShell = CreateObject("Wscript.Shell") Wscript.Echo "Your Windows Version Number is " _ & objshell.RegRead _ ( "HKLM\Software\Microsoft\Windows\CurrentVersion\VersionNumber")
RegWrite writes a value to a specified key value or creates a new key:
objShell.RegWrite strPath , anyValue [, strType ]
The strPath parameter is the path to the key to write. If the Registry path ends with a backslash, then RegWrite attempts to create a new key.
Table 9-9 lists the possible values for the optional strType parameter.
Registry type value | Description |
---|---|
REG_SZ | String value. This is the default value. |
REG_EXPAND_SZ | Expandable string. |
REG_DWORD | Integer value. |
'change the default document directory for Word 97 Set objShell = CreateObject("Wscript.Shell") objShell.RegWrite _ "HKCU\Software\Microsoft\Office.0\Word\Options\Doc-Path" _ , "H:\Data\Word" 'create new registry key objShell.RegWrite _ "HKCU\Software\Microsoft\Office.0\Word\Options\NewPath\" _ , ""
RegWrite will create a Registry path if it does not already exist.
RegDelete deletes an existing Registry key or value:
objShell.RegDelete strPath
The strPath parameter is the path to the value or key you want to delete. If the Registry path ends with a backslash, then RegDelete will attempt to delete the specified key; otherwise , it assumes it's a value.
If you specify a key to delete, RegDelete will delete all child values and keys, so exercise caution.
'delete the Newpath key. This will delete the NewPath key and all 'values and keys under it objShell.Delete _ "HKCU\Software\Microsoft\Office.0\Word\Options\NewPath\" , ""