Creating keys in the Registry from VBA

crewaustin66

Board Regular
Joined
Jun 11, 2014
Messages
82
Friends,

I am having trouble creating a key in the registry from VBA. I have the proper declaration for RegCreateKeyEx. I am able to use the following code to attempt to create HKEY_CLASSES_ROOT\Admin (which already exists). The return value is 0 with a status of 2 meaning "already exists". That's fine but I want to insert/create a GUID key underneath /AppID and I get a return value of 5 which is permissions related. Here is the test code that I am using. Does anyone know what I am doing wrong? I seem to have the right permissions for /AppID but this code fails anyway.

Code:
Sub TestRegCreateKey()
    Dim secAttr As SECURITY_ATTRIBUTES
    Dim subkey As String
    Dim retval As Long
    Dim section As Long
    
    section = HKEY_CLASSES_ROOT
    
    'subkey = "AppID"
    subkey = "AppID\\{5B076C03-2F26-11CF-9AE5-0800096E19F4}"
    'subkey = "AppID\\Crew"
    
    secAttr.nLength = Len(secAttr) ' size of the structure
    secAttr.lpSecurityDescriptor = 0 ' default security level
    secAttr.bInheritHandle = True ' the default value for this setting


    retval = RegCreateKey(section, subkey, secAttr)


End Sub


Public Function RegCreateKey(section As Long, subkey As String, secAttr As SECURITY_ATTRIBUTES) As Long
    '12/29/14 2064 -car- Created function to add new keys to registry


    Dim hregkey As Long ' receives handle to the newly created or opened registry key
    Dim neworused As Long ' receives 1 if new key was created or 2 if an existing key was opened
    Dim retval As Long ' return value
    
    ' Create or open the registry key
    retval = OSRegCreateKeyEx(section, subkey, 0, "", 0, KEY_WRITE, secAttr, hregkey, neworused)
    If retval <> 0 Then ' error during open
        Debug.Print "Error " & retval & " -- aborting."
        End ' terminate the program
    Else
        Debug.Print "Registry key " & section & "\" & subkey & " created or pre-existing."
    End If
End Function

Additional info:
I have a .reg file with this key in it that I can run that works under my userID. That tells me that I have the right permissions.
 
Last edited:

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
Hi,

Please would you post your declaration of RegCreateKeyEx() and also confirm your OS (eg. Win7 64 bit) and version of Office (eg. 2010 64 bit)?

Thanks
 
Last edited:
Upvote 0
Code:
Private Declare Function OSRegCreateKeyEx Lib "advapi32.dll" Alias "RegCreateKeyExA" (ByVal hkey As Long, ByVal lpSubKey As String, ByVal Reserved As Long, ByVal lpClass As String, ByVal dwOptions As Long, ByVal samDesired As Long, lpSecurityAttributes As SECURITY_ATTRIBUTES, phkResult As Long, lpdwDisposition As Long) As Long

Win7 32 bit. Office 2013 32 bit.

Thanks for the assist,
Crew
 
Upvote 0
Hi,

Your code works for me - I was able to create keys under HKEY_CLASSES_ROOT\AppId\ - tested on Win7 64bit Office 2010 32 bit.
I am a local admin on my machine. If you are not a local admin then my guess is your Win7 user account control settings is preventing you from creating a new key there programmatically. Try turning it off and then running your code?
 
Upvote 0

Forum statistics

Threads
1,214,516
Messages
6,119,979
Members
448,934
Latest member
audette89

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top