Read/ write/ delete registry

Tested in Excel 365 (16.8730.2046) 64-bit

DISCLAIMER: altering the registry could make your computer unusable. Make sure you know what you are doing! In case you use this code and damage you computer is your sole responsibility.

 
 
  1.  
  2. Public Enum ENM_RegistryOperation
  3.     RegistryRead
  4.     RegistryWrite
  5.     RegistryDelete
  6. End Enum
  7. '
  8. Public Enum ENM_RegistryKeyType
  9.     REG_SZ
  10.     REG_EXPAND_SZ
  11.     REG_DWORD
  12. End Enum
  13. '
  14. Public Function ReadWriteDeleteRegistry(RegistryKeyAndPath As String, _
  15.     Optional RegValue, Optional RegValueType As ENM_RegistryKeyType = REG_SZ, _
  16.     Optional ReadWriteDelete As ENM_RegistryOperation = RegistryRead) As String
  17.    
  18.     Dim RegShell As Variant
  19.     Dim KeyType As String
  20.     Dim TempVal
  21.    
  22.     Select Case RegValueType
  23.         Case REG_SZ
  24.             KeyType = "REG_SZ"
  25.         Case REG_EXPAND_SZ
  26.             KeyType = "REG_EXPAND_SZ"
  27.         Case REG_DWORD
  28.             KeyType = "REG_DWORD"
  29.     End Select
  30.    
  31.     Set RegShell = CreateObject("WScript.Shell")
  32.     TempVal = ""
  33.    
  34.     Select Case ReadWriteDelete
  35.         Case RegistryRead
  36.             On Error Resume Next
  37.             TempVal = RegShell.RegRead(RegistryKeyAndPath)
  38.             On Error GoTo 0
  39.         Case RegistryWrite
  40.             RegShell.RegWrite RegistryKeyAndPath, RegValue, KeyType
  41.             On Error Resume Next
  42.             ' read it to confirm it was written and return as function value
  43.            TempVal = RegShell.RegRead(RegistryKeyAndPath)
  44.             On Error GoTo 0
  45.         Case RegistryDelete
  46.             TempVal = "Error"
  47.             On Error Resume Next
  48.             TempVal = RegShell.RegDelete(RegistryKeyAndPath)
  49.             ' read it to confirm it was deleted
  50.            TempVal = RegShell.RegRead(RegistryKeyAndPath)
  51.             On Error GoTo 0
  52.             ' the function will return "" for successful delete and "Error" for key not found.
  53.            If TempVal <> "Error" Then TempVal = ""
  54.     End Select
  55.    
  56.     ReadWriteDeleteRegistry = TempVal
  57.    
  58.     Set RegShell = Nothing
  59. End Function
  60. '
  61. Public Sub main()
  62.     Dim a
  63.     ' read example (Excel installation path)
  64.    a = ReadWriteDeleteRegistry("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Office\16.0\Excel\InstallRoot\Path")
  65.     ' or
  66.    a = ReadWriteDeleteRegistry("HKLM\SOFTWARE\Microsoft\Office\16.0\Excel\InstallRoot\Path")
  67.     Debug.Print a
  68.    
  69.     ' write example
  70.    ' WARNING: make sure you don't write in the wrong place
  71.    a = ReadWriteDeleteRegistry("HKEY_CURRENT_USER\Software\TestKey\TestVal", "testing", , RegistryWrite)
  72.     ' or
  73.    a = ReadWriteDeleteRegistry("HKCU\Software\TestKey\TestVal", "testing", , RegistryWrite)
  74.     Debug.Print a
  75.     ' delete example
  76.    ' WARNING: make sure you delete the right one. Make some test with a key that you created
  77.    a = ReadWriteDeleteRegistry("HKCU\Software\TestKey\TestVal", , , RegistryDelete)
  78.     Debug.Print a
  79.    
  80. End Sub
  81.  

Add a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.