从 VBA 读取注册表子项 [英] Read registry subkeys from VBA

查看:57
本文介绍了从 VBA 读取注册表子项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下 VB 代码来获取注册表子项(不是注册表项或值).我只需要列出 Microsoft 子项中的应用程序(例如 Office、记事本、键盘等).

I have the following piece of VB code to get the registry subkey (NOT the key or the value of a registry). I just need to list out applications in Microsoft subkey (e.g. Office, Notepad, Keyboard etc.).

它在 VB.NET 中工作,但我试图将相同的代码应用于宏中的 VBA,我收到一个运行时错误,说 "ObjectGetOBjectEmumKey 行上的变量或未设置块变量".我认为以下代码应该同时兼容 VB.NETVBA.谁能解释一下?

It worked in VB.NET but I'm trying to apply the same code to VBA in Macro, I get a run time error saying "Object variable or With block variable not set" on the line of GetOBject and EmumKey. I though the following code should be compatible for both VB.NET and VBA. Can anyone please explain?

Dim temp As Object
'On Error Resume Next
Const HKEY_CURRENT_USER = &H80000001
temp = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & "." & "\root\default:StdRegProv")

Dim rPath As String
rPath = "Software\Microsoft\IdentityCRL\UserExtendedProperties"

Dim arrSubKeys(5) As Object
temp.EnumKey(HKEY_CURRENT_USER, rPath, arrSubKeys)

For Each ask In arrSubKeys
    MsgBox(ask.ToString)
Next

推荐答案

对于 VBA 试试这样

For VBA try it like this

  • temp 是一个对象,需要和 Set 一起使用
  • temp.Enum 语法是 temp.EnumKey HKEY_CURRENT_USER, rPath, arrSubKeys 而不是 temp.EnumKey(HKEY_CURRENT_USER, rPath, arrSubKeys)
  • Dim 将变量放在代码顶部以保持整洁 :)
  • temp is an object and needs to be used with Set
  • the temp.Enum syntax is temp.EnumKey HKEY_CURRENT_USER, rPath, arrSubKeys not temp.EnumKey(HKEY_CURRENT_USER, rPath, arrSubKeys)
  • Dim your variables at the top of your code for neatness :)

这段代码列出了HKEY_CURRENT_USER\Software\Microsoft\下的所有文件夹到VBE的立即窗口

This code lists all the folders under HKEY_CURRENT_USER\Software\Microsoft\ to the Immediate window of the VBE

Const HKEY_CURRENT_USER = &H80000001
Sub TestME()
    Dim temp As Object
    Dim strComputer As String
    Dim rPath As String
    Dim arrSubKeys()
    Dim strAsk

    strComputer = "."
    Set temp = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & _
    strComputer & "\root\default:StdRegProv")

    rPath = "Software\Microsoft\"
    temp.EnumKey HKEY_CURRENT_USER, rPath, arrSubKeys
    For Each strAsk In arrSubKeys
        Debug.Print strAsk
    Next
End Sub

这篇关于从 VBA 读取注册表子项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆