从 VBA 读取注册表子项
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8667800/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Read registry subkeys from VBA
提问by KMC
I have the following piece of VB code to get the registry subkey (NOTthe key or the value of a registry). I just need to list out applications in Microsoft subkey (e.g. Office, Notepad, Keyboard etc.).
我有以下一段 VB 代码来获取注册表子项(不是注册表的键或值)。我只需要列出 Microsoft 子项中的应用程序(例如 Office、记事本、键盘等)。
It worked in VB.NETbut I'm trying to apply the same code to VBAin 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.NETand VBA.
Can anyone please explain?
它曾在VB.NET,但我想同样的代码适用于VBA中宏,我说弄一个运行时错误"Object variable or With block variable not set"
上线GetOBject
和EmumKey
。我虽然下面的代码应该兼容VB.NET和VBA。谁能解释一下?
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
回答by brettdj
For VBA try it like this
对于 VBA,试试这样
temp
is an object and needs to be used with Set- the
temp.Enum
syntax istemp.EnumKey HKEY_CURRENT_USER, rPath, arrSubKeys
nottemp.EnumKey(HKEY_CURRENT_USER, rPath, arrSubKeys)
Dim
your variables at the top of your code for neatness :)
temp
是一个对象,需要和 Set 一起使用- 该
temp.Enum
语法是temp.EnumKey HKEY_CURRENT_USER, rPath, arrSubKeys
不是temp.EnumKey(HKEY_CURRENT_USER, rPath, arrSubKeys)
Dim
您的变量在代码顶部以保持整洁:)
This code lists all the folders under HKEY_CURRENT_USER\Software\Microsoft\
to the Immediate window of the VBE
此代码列出HKEY_CURRENT_USER\Software\Microsoft\
了 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