excel - 使用 Excel VBA 查找注册表项

标签 excel vba registry

我想检查用户是否安装了 Microsoft SQL Native Client 2008 (sqlncli10) 或 Native Client 2012 (sqlncli11)。

Dim key As String
Dim myShell As Object
Set myShell = CreateObject("WScript.Shell")

key = "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Microsoft SQL Server\SQLNCLI11\CurrentVersion\Version"
'key = "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\CurrentVersion"

Dim strKey As String
strKey = myShell.RegReadkey(key)

在寻找 native 客户端时,我收到错误:

Invalid root in registry key "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft SQL Server\SQLNCLI11\CurrentVersion\Version"

但是当尝试 Windows NT 版本时它工作正常。

我用 HKLM 替换了 HKEY_LOCAL_MACHINE:但这也不起作用。

编辑

我想我刚刚想通了。当我检查注册表时,它存在于 64 位部分。然而,VBA 会检查 32 位,并且 SOFTWARE 的根目录变为 Wow6432Node。因此它检查 Wow6432Node\Microsoft\Microsoft SQL Server 并且该 key 不存在。但我发现了另一个路径 SOFTWARE\Microsoft\Microsoft SQL Server Native Client 10 或者 11,它们在 32 位和 64 位中都存在。

最佳答案

这段代码对我有用:

Dim key As String
Dim objShell As Object

Set objShell = CreateObject("WScript.Shell")
key = objShell.RegRead _
    ("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Microsoft SQL Server\SQLNCLI11\CurrentVersion\Version")

我刚刚在 Excel 2010 VBA 编辑器中完成了此操作。

关于excel - 使用 Excel VBA 查找注册表项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35936735/

相关文章:

R:从 xlsx 导入日期和时间

vba - 使用 VBA 在 Excel 中动态生成表单

Excel.Application.Cells.SpecialCells(xlCellTypeLastCell) 返回工作表的底部,而不是最后一个数据单元格

Python arrow包时区错误?

vba - 跳过循环中的空单元格

vba - VBA Excel 中的消息框

vba - 如何从 Excel 中的单元格中删除空行(VBA)

c# - 创建注册表项时访问被拒绝

registry - 如何正确地将文件关联添加到 Windows 注册表?

excel - 数据一致性检查