Update:
While the below code was good for learning, there is a VBA Built in Function for working w/ Registry, but I suppose it's only useful for storing/saving settings in Registry related to your VBA project, not setting/retrieving settings from "other programs"/"locations in Registry".
See GetSetting
and SaveSetting
and DeleteSetting
https://learn.microsoft.com/en-us/office/vba/language/reference/user-interface-help/getsetting-function
https://learn.microsoft.com/en-us/office/vba/language/reference/user-interface-help/deletesetting-statement
https://learn.microsoft.com/en-us/office/vba/language/reference/user-interface-help/savesetting-statement
I built a function to accept/utilize all three as shown below, but it's not needed. I opened up RegEdit and used F5
to Refresh and watch as I stepped through code.
Option Explicit
Public Sub Test_RegKeyFunc()
Dim appname As String, section As String, key As String, default, KeyVal, GetSettingBool As Boolean, SaveSettingBool As Boolean, DelSettingBool As Boolean
appname = "MyApp"
section = "MySettings"
key = "AutoDoThisBool"
KeyVal = "TRUE"
Call RegKeyFunc(appname, section, key, , KeyVal) ' Call Func without setting Save = True Returns ""
Debug.Print RegKeyFunc(appname, section, key, , KeyVal)
If RegKeyFunc(appname, section, key, , KeyVal) = "" Then
Stop
End If
Call RegKeyFunc(appname, section, key, , KeyVal, , True) ' Call Func and Save Setting
Debug.Print RegKeyFunc(appname, section, key, , KeyVal)
If RegKeyFunc(appname, section, key, , KeyVal) = "TRUE" Then
Stop
End If
Call RegKeyFunc(appname, section, key, , KeyVal, , , True) ' Call Func and Del Key/Setting
Debug.Print RegKeyFunc(appname, section, key, , KeyVal)
If RegKeyFunc(appname, section, key, , KeyVal) = "" Then
Stop
End If
Stop
Call RegKeyFunc(appname, section, key, , KeyVal, , , , True) ' Call Func and Del SubFolder/Section
Debug.Print RegKeyFunc(appname, section, key, , KeyVal)
If RegKeyFunc(appname, section, key, , KeyVal) = "" Then
Stop
End If
Stop
Call RegKeyFunc(appname, section, key, , KeyVal, , , , , True) ' Call Func and Del Folder
Debug.Print RegKeyFunc(appname, section, key, , KeyVal)
If RegKeyFunc(appname, section, key, , KeyVal) = "" Then
Stop
End If
Stop
End Sub
Public Function RegKeyFunc(appname As String, section As String, Optional key As String, Optional default, Optional KeyVal, Optional GetSettingBool As Boolean, Optional SaveSettingBool As Boolean, Optional DelSettingBool As Boolean, Optional DelSectionBool As Boolean, Optional DelAppBool As Boolean)
'HKCU\SOFTWARE\VB and VBA Program Settings
If SaveSettingBool = True Then
SaveSetting appname, section, key, KeyVal
End If
If DelSettingBool = True Then
DeleteSetting appname, section, key
End If
If DelSectionBool = True Then
DeleteSetting appname, section
End If
If DelAppBool = True Then
DeleteSetting appname
End If '
RegKeyFunc = GetSetting(appname, section, key, default)
End Function
End Update
Heres my generic VBA code for working w/ Windows Registry.
Public Function ReadRegKeyVal(RegKeyStr As String) As Integer
ReadRegKeyVal = CreateObject("WScript.Shell").RegRead(RegKeyStr)
End Function
Public Function RegKeyExists(RegKeyStr As String) As Boolean
On Error GoTo ErrorHandler
CreateObject("WScript.Shell").RegRead (RegKeyStr)
RegKeyExists = True
Exit Function
ErrorHandler:
RegKeyExists = False
End Function
Public Sub SaveRegKey(RegKeyStr As String, RegKeyDesiredStateInt As Integer, Optional RegKeyType As String = "REG_DWORD")
CreateObject("WScript.Shell").RegWrite RegKeyStr, RegKeyDesiredStateInt, RegKeyType
Debug.Print "Generated --> " & RegKeyStr & "," & RegKeyDesiredStateInt & "," & RegKeyType
End Sub
An Example Call Sub:
Public Const DWordRegKeyEnabled As Integer = 1
Public Const DWordRegKeyDisabled As Integer = 0
Public RegKeyStr As String, RegKeyLocStr As String, RegKeyNameStr As String
Public RegKeyDesiredStateInt As Integer, RegKeyCurrentStateInt As Integer
Public RegKeyFoundBool As Boolean
Public Sub SetMinMaxEnabledInExcelStatusBar()
RegKeyDesiredStateInt = DWordRegKeyEnabled
RegKeyLocStr = "HKEY_CURRENT_USER\SOFTWARE\Microsoft\Office\" & Application.Version & "\Excel\StatusBar\"
RegKeyNameStr = "MaxValue"
RegKeyStr = RegKeyLocStr & RegKeyNameStr
Debug.Print "RegKeyStr = " & RegKeyStr
Call SetRegKey(RegKeyStr, RegKeyDesiredStateInt)
End Sub
Public Sub SetRegKey(RegKeyStr As String, RegKeyDesiredStateInt As Integer)
RegKeyFoundBool = RegKeyExists(RegKeyStr)
Debug.Print "RegKeyFoundBool = " & RegKeyFoundBool
If RegKeyFoundBool = False Then
Debug.Print "RegKeyFoundBool = False"
Call SaveRegKey(RegKeyStr, RegKeyDesiredStateInt)
Else
Debug.Print "RegKeyFoundBool = True"
RegKeyCurrentStateInt = ReadRegKeyVal(RegKeyStr)
Debug.Print "RegKeyCurrentStateInt = " & RegKeyCurrentStateInt
If RegKeyCurrentStateInt <> RegKeyDesiredStateInt Then
Debug.Print "RegKeyCurrentStateInt <> RegKeyDesiredStateInt"
Call SaveRegKey(RegKeyStr, RegKeyDesiredStateInt)
Else
Debug.Print "RegKeyCurrentStateInt = RegKeyDesiredStateInt"
End If
End If
End Sub
.RegWrite "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\USBSTOR\Start", 4, "REG_DWORD"
. But this will only work if the user which runs the VBA has rights to change the registry atHKEY_LOCAL_MACHINE
. – Appalachian