Reading registry keys from VBA

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

It worked in VB.NET , but I try to apply the same code to VBA on Macro, I get a runtime error saying "Object variable or With block variable not set" on the GetOBject and EmumKey . I though the following code should be compatible for both VB.NET and VBA . Can someone 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 
+4
source share
1 answer

For VBA try this like

  • temp is an object and should be used with Set
  • Syntax temp.Enum 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 accuracy :)

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

 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 

example output

+9
source

Source: https://habr.com/ru/post/1388450/


All Articles