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.).
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
For VBA try it like this
temp
is an object and needs to be used with Settemp.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 :)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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With