Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Delete a registry key from Excel

2 views
Skip to first unread message

Ann Hession

unread,
Nov 28, 1999, 3:00:00 AM11/28/99
to
Hi,

How do you delete a registry key using VBA code? I guess you need to
declare some DLLs, right? But which ones?

Thanks,

Ann

Venkata Krishna

unread,
Nov 29, 1999, 3:00:00 AM11/29/99
to
Ann

As a guideline, you should always try to create user registry entries only
in:

HKEY_CURRENT_USER\Software\VB and VBA Program Settings
Excel2000 provides commands to manupulate directly the entries in above.

You can create an entry as follows
SaveSetting appname := "ExcelApp", section := "MySection", key := "Top",
setting := 50

The above creates a section as HKEY_CURRENT_USER\Software\VB and VBA Program
Settings\ExcelApp\MySection which is visible in the left pane of Registry
editor.
Also the above will create an entry Top, 50 which will be displayed in the
right pane of registry editor.

To delete the above key use
DeleteSetting "ExcelApp", "MySection"

If you want to delete a setting is some other tree node of registry, you
need to use API's which is a bit complicated. You can try using API
functions - RegDeleteKey and RegDeleteValue.

Regards
Venkata Krishna

Ann Hession <@> wrote in message
news:eWw9TYfO$GA....@cppssbbsa02.microsoft.com...

Heinz Aigner

unread,
Jan 8, 2000, 3:00:00 AM1/8/00
to
Hi

Attached you find a solution. Please note, that this code was NOT written
by
me, so that I can not give answers to your questions. I just translated
the
comments to english and tested it. In Win95 it runs without any problem.

Thanks to the unknown writer!

Heinz Aigner

Option Explicit
Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As
Long
Declare Function RegConnectRegistry Lib "advapi32.dll" Alias
"RegConnectRegistryA" (ByVal lpMachineName As String, ByVal hKey As Long,

phkResult As Long) As Long
Declare Function RegCreateKey Lib "advapi32.dll" Alias "RegCreateKeyA"
(ByVal
hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long
Declare Function RegCreateKeyEx Lib "advapi32.dll" Alias
"RegCreateKeyExA"
(ByVal hKey As Long, ByVal lpSubKey As String, ByVal Reserved As Long,
ByVal
lpClass As String, ByVal dwOptions As Long, ByVal samDesired As Long,
lpSecurityAttributes As SECURITY_ATTRIBUTES, phkResult As Long,
lpdwDisposition As Long) As Long
Declare Function RegDeleteKey Lib "advapi32.dll" Alias "RegDeleteKeyA"
(ByVal
hKey As Long, ByVal lpSubKey As String) As Long
Declare Function RegDeleteValue Lib "advapi32.dll" Alias
"RegDeleteValueA"
(ByVal hKey As Long, ByVal lpValueName As String) As Long
Declare Function RegEnumKey Lib "advapi32.dll" Alias "RegEnumKeyA" (ByVal
hKey
As Long, ByVal dwIndex As Long, ByVal lpName As String, ByVal cbName As
Long)
As Long
Declare Function RegEnumKeyEx Lib "advapi32.dll" Alias "RegEnumKeyExA"
(ByVal
hKey As Long, ByVal dwIndex As Long, ByVal lpName As String, lpcbName As
Long,
lpReserved As Long, ByVal lpClass As String, lpcbClass As Long,
lpftLastWriteTime As FILETIME) As Long
Declare Function RegEnumValue Lib "advapi32.dll" Alias "RegEnumValueA"
(ByVal
hKey As Long, ByVal dwIndex As Long, ByVal lpValueName As String,
lpcbValueName As Long, lpReserved As Long, lpType As Long, lpData As Any,

lpcbData As Long) As Long
Declare Function RegFlushKey Lib "advapi32.dll" (ByVal hKey As Long) As
Long
Declare Function RegGetKeySecurity Lib "advapi32.dll" (ByVal hKey As
Long,
ByVal SecurityInformation As Long, pSecurityDescriptor As
SECURITY_DESCRIPTOR,
lpcbSecurityDescriptor As Long) As Long
Declare Function RegLoadKey Lib "advapi32.dll" Alias "RegLoadKeyA" (ByVal
hKey
As Long, ByVal lpSubKey As String, ByVal lpFile As String) As Long
Declare Function RegNotifyChangeKeyValue Lib "advapi32.dll" (ByVal hKey
As
Long, ByVal bWatchSubtree As Long, ByVal dwNotifyFilter As Long, ByVal
hEvent
As Long, ByVal fAsynchronus As Long) As Long
Declare Function RegOpenKey Lib "advapi32.dll" Alias "RegOpenKeyA" (ByVal
hKey
As Long, ByVal lpSubKey As String, phkResult As Long) As Long
Declare Function RegOpenKeyEx Lib "advapi32.dll" Alias "RegOpenKeyExA"
(ByVal
hKey As Long, ByVal lpSubKey As String, ByVal ulOptions As Long, ByVal
samDesired As Long, phkResult As Long) As Long
Declare Function RegQueryInfoKey Lib "advapi32.dll" Alias
"RegQueryInfoKeyA"
(ByVal hKey As Long, ByVal lpClass As String, lpcbClass As Long,
lpReserved As
Long, lpcSubKeys As Long, lpcbMaxSubKeyLen As Long, lpcbMaxClassLen As
Long,
lpcValues As Long, lpcbMaxValueNameLen As Long, lpcbMaxValueLen As Long,
lpcbSecurityDescriptor As Long, lpftLastWriteTime As FILETIME) As Long
Declare Function RegQueryValue Lib "advapi32.dll" Alias "RegQueryValueA"
(ByVal hKey As Long, ByVal lpSubKey As String, ByVal lpValue As String,
lpcbValue As Long) As Long
Declare Function RegQueryValueEx Lib "advapi32.dll" Alias
"RegQueryValueExA"
(ByVal hKey As Long, ByVal lpValueName As String, ByVal lpReserved As
Long,
lpType As Long, lpData As Any, lpcbData As Long) As Long ' Note
that
if you declare the lpData parameter as String, you must pass it By Value.

Declare Function RegReplaceKey Lib "advapi32.dll" Alias "RegReplaceKeyA"
(ByVal hKey As Long, ByVal lpSubKey As String, ByVal lpNewFile As String,

ByVal lpOldFile As String) As Long
Declare Function RegRestoreKey Lib "advapi32.dll" Alias "RegRestoreKeyA"
(ByVal hKey As Long, ByVal lpFile As String, ByVal dwFlags As Long) As
Long
Declare Function RegSaveKey Lib "advapi32.dll" Alias "RegSaveKeyA" (ByVal
hKey
As Long, ByVal lpFile As String, lpSecurityAttributes As
SECURITY_ATTRIBUTES)
As Long
Declare Function RegSetKeySecurity Lib "advapi32.dll" (ByVal hKey As
Long,
ByVal SecurityInformation As Long, pSecurityDescriptor As
SECURITY_DESCRIPTOR)
As Long
Declare Function RegSetValue Lib "advapi32.dll" Alias "RegSetValueA"
(ByVal
hKey As Long, ByVal lpSubKey As String, ByVal dwType As Long, ByVal
lpData As
String, ByVal cbData As Long) As Long
Declare Function RegSetValueEx Lib "advapi32.dll" Alias "RegSetValueExA"
(ByVal hKey As Long, ByVal lpValueName As String, ByVal Reserved As Long,

ByVal dwType As Long, lpData As Any, ByVal cbData As Long) As
Long '
Note that if you declare the lpData parameter as String, you must pass it
By
Value.
Declare Function RegUnLoadKey Lib "advapi32.dll" Alias "RegUnLoadKeyA"
(ByVal
hKey As Long, ByVal lpSubKey As String) As Long
'User defined typs, which are needed in the declarations:
Type SECURITY_ATTRIBUTES
nLength As Long
lpSecurityDescriptor As Long
bInheritHandle As Boolean
End Type
Type FILETIME
dwLowDateTime As Long
dwHighDateTime As Long
End Type
Type ACL
AclRevision As Byte
Sbz1 As Byte
AclSize As Integer
AceCount As Integer
Sbz2 As Integer
End Type
Type SECURITY_DESCRIPTOR
Revision As Byte
Sbz1 As Byte
Control As Long
Owner As Long
Group As Long
Sacl As ACL
Dacl As ACL
End Type

Const HKEY_CLASSES_ROOT = &H80000000
Const HKEY_CURRENT_USER = &H80000001
Const HKEY_LOCAL_MACHINE = &H80000002
Const HKEY_USERS = &H80000003
Const REG_SZ = 1

Function fktRegDeleteKey(szKey As String)
fktRegDeleteKey = RegDeleteKey(HKEY_CURRENT_USER, szKey)
End Function

Function fktRegWrite(szKey As String, szWert As String) As Long
Dim hkeyTemp As Long
Dim Rück As Long

hkeyTemp = 0
Rück = RegOpenKey(HKEY_CURRENT_USER, szKey, hkeyTemp)
If Rück = False Then
Rück = RegSetValue(hkeyTemp, "Test", REG_SZ, szWert, Len(szWert))
End If
fktRegWrite = Rück
End Function
Function fktRegCreateKey(szKey As String, szWert As String) As Long
Dim hkeyTemp As Long
Dim Rück As Long

hkeyTemp = 0
Rück = RegCreateKey(HKEY_CURRENT_USER, szKey, hkeyTemp)
If Rück = False Then
Rück = RegSetValue(hkeyTemp, "", REG_SZ, szWert, Len(szWert))
End If
fktRegCreateKey = Rück
End Function


Sub Main()
Dim a
'Makes 'HKEY_CURRENT_USER\NewKey'
a = fktRegCreateKey("NewKey", "StandartWert")
MsgBox "The Key 'NewKey' in HKEY_CURRENT_USER was made. OK for
Continue",
, "Last Value: " & a
'Changes the Standart- Value to 'Wert'
a = fktRegWrite("NewKey", "Wert")
MsgBox "The Standard-Value of 'NewKey' has been changed. OK for
Continue",
, "Last Value: " & a
'Delet 'HKEY_CURRENT_USER\NewKey'
a = fktRegDeleteKey("NewKey")
MsgBox "'NewKey' was deleted. OK für finisch", , "Last Value: " & a
End Sub


Heinz Aigner

unread,
Jan 8, 2000, 3:00:00 AM1/8/00
to
0 new messages