Excel VBA 操作注册表的模块 实现代码
发布时间:2023-06-23 14:37:36
VBA 自带的注册表操作功能很弱,只能操作固定的键,很是不方便。这里收集了一个在 VBA 中操作注册表的模块,我对模块进行了一定的修改,使它可以用在 64 位的 Office 上面。
Excel VBA 操作注册表的模块 实现代码
Option Explicit
'---注册表主键---
Public Enum enumRegMainKey
iHKEY_CLASSES_ROOT = &H80000000
iHKEY_CURRENT_USER = &H80000001
iHKEY_LOCAL_MACHINE = &H80000002
iHKEY_USERS = &H80000003
iHKEY_PERFORMANCE_DATA = &H80000004
iHKEY_CURRENT_CONFIG = &H80000005
iHKEY_DYN_DATA = &H80000006
End Enum
'---注册表数据类型---
Public Enum enumRegSzType
iREG_SZ = &H1
iREG_EXPAND_SZ = &H2
iREG_BINARY = &H3
iREG_DWORD = &H4
iREG_NONE = 0&
iREG_DWORD_LITTLE_ENDIAN = 4&
iREG_DWORD_BIG_ENDIAN = 5&
iREG_LINK = 6&
iREG_MULTI_SZ = 7&
iREG_RESOURCE_LIST = 8&
iREG_FULL_RESOURCE_DEscrīptOR = 9&
iREG_RESOURCE_REQUIREMENTS_LIST = 10&
End Enum
'---返回值和访问权限常数---
Private Const ERROR_SUCCESS = 0&
Private Const ERROR_BADDB = 1009&
Private Const ERROR_BADKEY = 1010&
Private Const ERROR_CANTOPEN = 1011&
Private Const ERROR_CANTREAD = 1012&
Private Const ERROR_CANTWRITE = 1013&
Private Const ERROR_OUTOFMEMORY = 14&
Private Const ERROR_INVALID_PARAMETER = 87&
Private Const ERROR_ACCESS_DENIED = 5&
Private Const ERROR_NO_MORE_ITEMS = 259&
Private Const ERROR_MORE_DATA = 234&
Private Const KEY_QUERY_VALUE = &H1&
Private Const KEY_SET_VALUE = &H2&
Private Const KEY_CREATE_SUB_KEY = &H4&
Private Const KEY_ENUMERATE_SUB_KEYS = &H8&
Private Const KEY_NOTIFY = &H10&
Private Const KEY_CREATE_LINK = &H20&
Private Const SYNCHRONIZE = &H100000
Private Const READ_CONTROL = &H20000
Private Const WRITE_DAC = &H40000
Private Const WRITE_OWNER = &H80000
Private Const STANDARD_RIGHTS_REQUIRED = &HF0000
Private Const STANDARD_RIGHTS_READ = READ_CONTROL
Private Const STANDARD_RIGHTS_WRITE = READ_CONTROL
Private Const STANDARD_RIGHTS_EXECUTE = READ_CONTROL
Private Const KEY_READ = STANDARD_RIGHTS_READ Or KEY_QUERY_VALUE Or KEY_ENUMERATE_SUB_KEYS Or KEY_NOTIFY
Private Const KEY_WRITE = STANDARD_RIGHTS_WRITE Or KEY_SET_VALUE Or KEY_CREATE_SUB_KEY
Private Const KEY_EXECUTE = ((KEY_READ) And (Not SYNCHRONIZE))
Private Const KEY_ALL_ACCESS = ((STANDARD_RIGHTS_ALL Or KEY_QUERY_VALUE Or KEY_SET_VALUE Or KEY_CREATE_SUB_KEY Or KEY_ENUMERATE_SUB_KEYS Or KEY_NOTIFY Or KEY_CREATE_LINK) And (Not SYNCHRONIZE))
'---API 函数---
#If Win64 Then '64位
Private Declare PtrSafe Function RegCreateKey Lib "advapi32.dll" Alias "RegCreateKeyA" (ByVal hKey As LongPtr, ByVal lpSubKey As String, phkResult As LongPtr) As Long
Private Declare PtrSafe Function RegCreateKeyEx Lib "advapi32.dll" Alias "RegCreateKeyExA" (ByVal hKey As LongPtr, 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 LongPtr, lpdwDisposition As Long) As Long
Private Declare PtrSafe Function RegEnumValue Lib "advapi32.dll" Alias "RegEnumValueA" (ByVal hKey As LongPtr, ByVal dwIndex As Long, ByVal lpValueName As String, lpcbValueName As Long, ByVal lpReserved As LongPtr, lpType As Long, lpData As Byte, lpcbData As Long) As Long
Private Declare PtrSafe Function RegEnumKeyEx Lib "advapi32.dll" Alias "RegEnumKeyExA" (ByVal hKey As LongPtr, ByVal dwIndex As Long, ByVal lpName As String, lpcbName As Long, ByVal lpReserved As LongPtr, ByVal lpClass As String, lpcbClass As Long, lpftLastWriteTime As FILETIME) As Long
Private Declare PtrSafe Function RegOpenKeyEx Lib "advapi32.dll" Alias "RegOpenKeyExA" (ByVal hKey As LongPtr, ByVal lpSubKey As String, ByVal ulOptions As Long, ByVal samDesired As Long, phkResult As LongPtr) As Long
Private Declare PtrSafe Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As LongPtr) As Long
Private Declare PtrSafe Function RegDeleteValue Lib "advapi32.dll" Alias "RegDeleteValueA" (ByVal hKey As LongPtr, ByVal lpValueName As String) As Long
Private Declare PtrSafe Function RegSetValueEx Lib "advapi32.dll" Alias "RegSetValueExA" (ByVal hKey As LongPtr, 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.
Private Declare PtrSafe Function RegSetValueExLong Lib "advapi32.dll" Alias "RegSetValueExA" (ByVal hKey As LongPtr, ByVal lpValueName As String, ByVal Reserved As Long, ByVal dwType As Long, lpData As LongPtr, ByVal cbData As LongPtr) As Long
Private Declare PtrSafe Function RegSetValueExA Lib "advapi32.dll" (ByVal hKey As LongPtr, ByVal lpValueName As String, ByVal Reserved As Long, ByVal dwType As Long, ByRef lpData As LongPtr, ByVal cbData As LongPtr) As Long
Private Declare PtrSafe Function RegQueryValueEx Lib "advapi32.dll" Alias "RegQueryValueExA" (ByVal hKey As LongPtr, ByVal lpValueName As String, ByVal lpReserved As LongPtr, 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.
Private Declare PtrSafe Function RegDeleteKey Lib "advapi32.dll" Alias "RegDeleteKeyA" (ByVal hKey As LongPtr, ByVal lpSubKey As String) As Long
Private Declare PtrSafe Function RegSaveKey Lib "advapi32.dll" Alias "RegSaveKeyA" (ByVal hKey As LongPtr, ByVal lpFile As String, lpSecurityAttributes As SECURITY_ATTRIBUTES) As Long
Private Declare PtrSafe Function RegRestoreKey Lib "advapi32.dll" Alias "RegRestoreKeyA" (ByVal hKey As LongPtr, ByVal lpFile As String, ByVal dwFlags As Long) As Long
#Else
Private Declare Function RegCreateKey Lib "advapi32.dll" Alias "RegCreateKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long
Private 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
Private Declare Function RegEnumValue Lib "advapi32.dll" Alias "RegEnumValueA" (ByVal hKey As Long, ByVal dwIndex As Long, ByVal lpValueName As String, lpcbValueName As Long, ByVal lpReserved As Long, lpType As Long, lpData As Byte, lpcbData As Long) As Long
Private Declare Function RegEnumKeyEx Lib "advapi32.dll" Alias "RegEnumKeyExA" (ByVal hKey As Long, ByVal dwIndex As Long, ByVal lpName As String, lpcbName As Long, ByVal lpReserved As Long, ByVal lpClass As String, lpcbClass As Long, lpftLastWriteTime As FILETIME) As Long
Private 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
Private Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long
Private Declare Function RegDeleteValue Lib "advapi32.dll" Alias "RegDeleteValueA" (ByVal hKey As Long, ByVal lpValueName As String) As Long
Private Declare Function RegSetValueEx Lib "advapi32.dll" Alias "RegSetValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal Reserved As Long, ByVal dwType As Long, ByVal lpData As String, ByVal cbData As Long) As Long
Private Declare Function RegSetValueExLong Lib "advapi32.dll" Alias "RegSetValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal Reserved As Long, ByVal dwType As Long, lpData As Long, ByVal cbData As Long) As Long
Private Declare Function RegSetValueExA Lib "advapi32.dll" (ByVal hKey As Long, ByVal lpValueName As String, ByVal Reserved As Long, ByVal dwType As Long, ByRef lpData As Long, ByVal cbData As Long) As Long
Private 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.
Private Declare Function RegDeleteKey Lib "advapi32.dll" Alias "RegDeleteKeyA" (ByVal hKey As Long, ByVal lpSubKey As String) As Long
Private Declare Function RegSaveKey Lib "advapi32.dll" Alias "RegSaveKeyA" (ByVal hKey As Long, ByVal lpFile As String, ByVal lpSecurityAttributes As Long) As Long
Private Declare Function RegRestoreKey Lib "advapi32.dll" Alias "RegRestoreKeyA" (ByVal hKey As Long, ByVal lpFile As String, ByVal dwflags As Long) As Long
#End If
Private Type FILETIME
dwLowDateTime As Long
dwHighDateTime As Long
End Type
#If Win64 Then '64位
Private Type SECURITY_ATTRIBUTES
nLength As Long
lpSecurityDescrīptor As LongPtr
bInheritHandle As Boolean
End Type
#Else
Private Type SECURITY_ATTRIBUTES
nLength As Long
lpSecurityDescriptor As Long
bInheritHandle As Long
End Type
#End If
'---取得值---
Public Function GetValue(ByVal mainKey As enumRegMainKey, _
ByVal subKey As String, _
ByVal keyV As String, _
ByRef sValue As Variant, _
Optional ByRef rlngErrNum As Long, _
Optional ByRef rstrErrDescr As String) As Boolean
Dim hKey As Long, lType As Long, lBuffer As Long, sBuffer As String, lData As Long
On Error GoTo GetValueErr
GetValue = False
If RegOpenKeyEx(mainKey, subKey, 0, KEY_READ, hKey) <> ERROR_SUCCESS Then
Err.Raise vbObjectError + 1, , "获取注册表值时出错"
End If
If RegQueryValueEx(hKey, keyV, 0, lType, ByVal 0, lBuffer) <> ERROR_SUCCESS Then
Err.Raise vbObjectError + 1, , "获取注册表值时出错"
End If
Select Case lType
Case iREG_SZ
lBuffer = 255
sBuffer = Space(lBuffer)
If RegQueryValueEx(hKey, keyV, 0, lType, ByVal sBuffer, lBuffer) <> ERROR_SUCCESS Then
Err.Raise vbObjectError + 1, , "获取注册表值时出错"
End If
sValue = Left(sBuffer, InStr(sBuffer, Chr(0)) - 1)
Case iREG_EXPAND_SZ
sBuffer = Space(lBuffer)
If RegQueryValueEx(hKey, keyV, 0, lType, ByVal sBuffer, lBuffer) <> ERROR_SUCCESS Then
Err.Raise vbObjectError + 1, , "获取注册表值时出错"
End If
sValue = Left(sBuffer, InStr(sBuffer, Chr(0)) - 1)
Case iREG_DWORD
If RegQueryValueEx(hKey, keyV, 0, lType, lData, lBuffer) <> ERROR_SUCCESS Then
Err.Raise vbObjectError + 1, , "获取注册表值时出错"
End If
sValue = lData
Case iREG_BINARY
If RegQueryValueEx(hKey, keyV, 0, lType, lData, lBuffer) <> ERROR_SUCCESS Then
Err.Raise vbObjectError + 1, , "获取注册表值时出错"
End If
sValue = lData
End Select
If RegCloseKey(hKey) <> ERROR_SUCCESS Then
Err.Raise vbObjectError + 1, , "获取注册表值时出错"
End If
GetValue = True
Err.Clear
GetValueErr:
rlngErrNum = Err.Number
rstrErrDescr = Err.Descrīption
End Function
'----赋值---
Public Function SetValue(ByVal mainKey As enumRegMainKey, _
ByVal subKey As String, _
ByVal keyV As String, _
ByVal lType As enumRegSzType, _
ByVal sValue As Variant, _
Optional ByRef rlngErrNum As Long, _
Optional ByRef rstrErrDescr As String) As Boolean
Dim S As Long, lBuffer As Long, hKey As Long
Dim ss As SECURITY_ATTRIBUTES
On Error GoTo SetValueErr
SetValue = False
ss.nLength = Len(ss)
ss.lpSecurityDescrīptor = 0
ss.bInheritHandle = True
If RegCreateKeyEx(mainKey, subKey, 0, "", 0, KEY_WRITE, ss, hKey, S) <> ERROR_SUCCESS Then
Err.Raise vbObjectError + 1, , "设置注册表时出错"
End If
Select Case lType
Case iREG_SZ
lBuffer = LenB(sValue)
If RegSetValueEx(hKey, keyV, 0, lType, ByVal sValue, lBuffer) <> ERROR_SUCCESS Then
Err.Raise vbObjectError + 1, , "设置注册表时出错"
End If
Case iREG_EXPAND_SZ
lBuffer = LenB(sValue)
If RegSetValueEx(hKey, keyV, 0, lType, ByVal sValue, lBuffer) <> ERROR_SUCCESS Then
Err.Raise vbObjectError + 1, , "设置注册表时出错"
End If
Case iREG_DWORD
lBuffer = 4
If RegSetValueExA(hKey, keyV, 0, lType, sValue, lBuffer) <> ERROR_SUCCESS Then
Err.Raise vbObjectError + 1, , "设置注册表时出错"
End If
Case iREG_BINARY
lBuffer = 4
If RegSetValueExA(hKey, keyV, 0, lType, sValue, lBuffer) <> ERROR_SUCCESS Then
Err.Raise vbObjectError + 1, , "设置注册表时出错"
End If
Case Else
Err.Raise vbObjectError + 1, , "不支持该参数类型"
End Select
If RegCloseKey(hKey) <> ERROR_SUCCESS Then
Err.Raise vbObjectError + 1, , "设置注册表时出错"
End If
SetValue = True
Err.Clear
SetValueErr:
rlngErrNum = Err.Number
rstrErrDescr = Err.Descrīption
End Function
'---删除值---
Public Function DeleteValue(ByVal mainKey As enumRegMainKey, _
ByVal subKey As String, _
ByVal keyV As String, _
Optional ByRef rlngErrNum As Long, _
Optional ByRef rstrErrDescr As String) As Boolean
Dim hKey As Long
On Error GoTo DeleteValueErr
DeleteValue = False
If RegOpenKeyEx(mainKey, subKey, 0, KEY_WRITE, hKey) <> ERROR_SUCCESS Then
Err.Raise vbObjectError + 1, , "删除注册表值时出错"
End If
If RegDeleteValue(hKey, keyV) <> ERROR_SUCCESS Then
Err.Raise vbObjectError + 1, , "删除注册表值时出错"
End If
If RegCloseKey(hKey) <> ERROR_SUCCESS Then
Err.Raise vbObjectError + 1, , "删除注册表值时出错"
End If
DeleteValue = True
Err.Clear
DeleteValueErr:
rlngErrNum = Err.Number
rstrErrDescr = Err.Descrīption
End Function
'---删除项---
Public Function DeleteKey(ByVal mainKey As enumRegMainKey, _
ByVal subKey As String, _
ByVal keyV As String, _
Optional ByRef rlngErrNum As Long, _
Optional ByRef rstrErrDescr As String) As Boolean
Dim hKey As Long
On Error GoTo DeleteKeyErr
DeleteKey = False
If RegOpenKeyEx(mainKey, subKey, 0, KEY_WRITE, hKey) <> ERROR_SUCCESS Then
Err.Raise vbObjectError + 1, , "删除注册表值时出错"
End If
If RegDeleteKey(hKey, keyV) <> ERROR_SUCCESS Then
Err.Raise vbObjectError + 1, , "删除注册表值时出错"
End If
If RegCloseKey(hKey) <> ERROR_SUCCESS Then
Err.Raise vbObjectError + 1, , "删除注册表值时出错"
End If
DeleteKey = True
Err.Clear
DeleteKeyErr:
rlngErrNum = Err.Number
rstrErrDescr = Err.Descrīption
End Function
Excel VBA 操作注册表的模块 实现代码的下载地址:


猜你喜欢
- 升级Win10创意者更新需要注意哪些事项?继微软发布Win10创意者更新正式版已有10天了,很多伙伴都准备升级Win10创意者,正所谓知己知
- 当您想将 iPhone 或 iPad 中的一些照片或视频分享给好友时,您可以隐藏照片或视频中的位置数据或其他信息,从而保护个人的隐私安全。这
- Win10系统中有个非常好用的分屏功能,使用分屏功能之后屏幕就会自动分屏,帮助用户有效提高工作效率。但是使用完这个功能要如何关闭呢?那么下面
- 怎样进行Win11系统的重装?相信有小伙伴听说过Win11重装,这种情况适用于电脑出现卡顿和运行速度或者出现系统故障的时候使用的。那要如何进
- 360官方正式推出360安全桌面,360安全桌面整合了“社区聊天、视频、音乐、新闻、游戏、小说、购物”等日常必备软件。360安全桌面,玩电脑
- Microsoft Edge浏览器上有个“个性化仪表板”Web Widget,用户可以通过Web部件来获取信息、发现新闻、以及将天气等信息。
- 鼠标的滚轮容易坏,看文章的时候自动滚动就是个问题,拉右边的垂直滚动条很麻烦,怎么办呢?教你高手是如何用按钮实现文档自动滚动。其实,从word
- word/excel中怎么快速的打出乘除符号?很多的时候我们在录入公式的时候需要使用乘除符号,但是,电脑中方便输入的乘除符号确实*/这种,该
- 随着电脑现在的功能越来越到之后,随之出现的电脑问题也越来愈多,但是很多小伙伴不是很清楚究竟该怎么办,因为自己的电脑知识也不是特别丰富,那现在
- 多项选择题是考卷中经常遇见的题型之一,它比单选题更加难做,因为你少选或者多选都不给分,如果说单选题每题还有25%的几率正确,那么对于多选来说
- 设计讲义母版的格式主要用于更改PPT幻灯片的打印设计和版式,例。图1单击“讲义母版”按钮步骤2:系统自动切换到“讲义母版”选项卡,默认情况下
- 众所周知,浏览器在使用过程中会产生大量的缓存,尤其是观看网络视频后,缓存的体积可以在短时间内达到几个G。对此,我们应该怎么办呢?下面,我们就
- 系统升级对于电脑来说是常有的事,但是因为系统升级的安装包又大,需要安装的时间也很长,还要经常重启,所以遇到错误也是一件很正常的事情。今天小编
- Windows 7的界面有很多新的变化,比如Windows 7 RC版中有一些新的组合快捷键,活用这些快捷组合能让WIndows 7的操作更
- Word中经常会遇到超大表格,跨越好几页,每页的标题行都要手动设置太麻烦,其实,只需鼠标轻轻一点,就能让每页都有标题行。第一步:打开word
- win10怎么删除多余打印机和扫描仪?最近收到一些小伙伴们的反映,说不知道在Win 10系统里该如何删除多余的打印机和扫描仪,那么下面就一起
- 电脑使用的时间长了,难免会出现各种各样的问题,而往往这些问题导致的就是系统受损,电脑无法正常开机。那么,Win7系统受损怎么恢复呢?其实,很
- 撤消和恢复,是我们日常使用文档、表格非常常见的操作,一般用ctrl+z就可以轻松搞定,而且在 Microsoft Office Excel
- 在美图秀秀中有很多非常强大的功能,比如我们可以使用美图秀秀进行局部变色,进行抠图换背景,进行拼图设计,制作海报,制作GIF动图等等。如果我们
- Win11telnet服务怎么开启?当我们在进行远程连接操作时,可能需要开启telnet服务才可以继续,但是在更新系统后,不少人用户不知道W