unable to write to HKLM

  • I have looked at various websites and found all kinds of entries to add entries to HKLM none worked with success.


    Using my spreadsheet and in VBA code I want to be able to add a key and put entries into HKLM (local machine)
    (error says I do not have the permissions)


    Rather than writing to HKCU for every user, I want to write the registry in HKLM so as the entries are global to the local machine.


    Is there a way to programmatically allow my specific spreadsheet to have the permissions to access HKLM via Excel VBA code?
    I am using Windows 7, Office 2010

  • Of course you could write the .REG text file and then Shell() to execute it.
    \
    Or, use an API method. e.g.

  • It depends on what you want to do: Add a key, a value, or check for key, value, or delete a key, value, etc.


    Do not use registry methods if you have not backed it up and/or you don't understand it.


    There are other API's like those here: http://www.cpearson.com/excel/Registry.htm


    There are other methods like the method I first explained and this which is a combination of WScript.Shell and a Shell() method. e.g.

    Code
    'https://stackoverflow.com/questions/32345238/read-and-write-from-to-registry-in-vba
    Sub DoUSB_Control()
        If CreateObject("WScript.Shell").RegRead("HKLM\SYSTEM\CurrentControlSet\Services\USBSTOR\Start") = 3 Then
            ShellExecute 0, "runas", "C:\Windows\System32\cmd.exe", "/k %windir%\System32\reg.exe ADD HKLM\SYSTEM\CurrentControlSet\Services\USBSTOR /f /v Start /t REG_DWORD /d 4", "C:\", 0
        Else
            ShellExecute 0, "runas", "C:\Windows\System32\cmd.exe", "/k %windir%\System32\reg.exe ADD HKLM\SYSTEM\CurrentControlSet\Services\USBSTOR /f /v Start /t REG_DWORD /d 3", "C:\", 0
        End If
    End Sub


    In the example above, I would use /c rather than /k. That method uses an API ShellExecute().


    One way to learn REG command syntax is to run RegEdit in a command shell (Win+R, cmd, Return) and copy the keys as needed from an existing one. Then paste to text editor like Notepad or one of many others.

  • Kenneth,


    In answer to your question, my program has parameters that I want to store such as version, license
    I want to be able to add key(s) ,read and write values to it to hklm and if an uninstall is needed to delete the Key(s)


    Eg:


    Rabsoftware - Key
    FinancialStatement - Sub Key
    sysparm - SubKey
    License abcdefgh - Parm
    version 4.5 - Parm


    JobTime - Key
    sysparm - SubKey
    License abcdxyz - Parm
    version 2.3 - Parm


    By putting it in hklm, I only have to modify it once. whereas in hkcu I have to modify it for each user.

Participate now!

Don’t have an account yet? Register yourself now and be a part of our community!