Use this macro to read a registry key from the registry and change it, if desired. In the examples below, to change the default FileAs format for contacts or toggle the Mark My Comments option off and on.
To use it, you'll need to get the key you want to change from the registry. If you are writing to the registry, the value type of Optional i_Type As String = "REG_DWORD") may need to be updated to the correct type of value.
Read, Edit, Save Registry values
Use these functions with macros (examples below) to change registry values. These functions set or change DWORD values. If you need to change string or binary values, you'll need to change the type in the RegKeySave function.
'reads the value for the registry key i_RegKey 'if the key cannot be found, the return value is "" Function RegKeyRead(i_RegKey As String) As String Dim myWS As Object On Error Resume Next 'access Windows scripting Set myWS = CreateObject("WScript.Shell") 'read key from registry RegKeyRead = myWS.RegRead(i_RegKey) End Function 'sets the registry key i_RegKey to the 'value i_Value with type i_Type 'if i_Type is omitted, the value will be saved as string 'if i_RegKey wasn't found, a new registry key will be created ' change REG_DWORD to the correct key type Sub RegKeySave(i_RegKey As String, _ i_Value As String, _ Optional i_Type As String = "REG_DWORD") Dim myWS As Object 'access Windows scripting Set myWS = CreateObject("WScript.Shell") 'write registry key myWS.RegWrite i_RegKey, i_Value, i_Type End Sub 'returns True if the registry key i_RegKey was found 'and False if not Function RegKeyExists(i_RegKey As String) As Boolean Dim myWS As Object On Error GoTo ErrorHandler 'access Windows scripting Set myWS = CreateObject("WScript.Shell") 'try to read the registry key myWS.RegRead i_RegKey 'key was found RegKeyExists = True Exit Function ErrorHandler: 'key was not found RegKeyExists = False End Function
Choose from multiple values
Use this sample to display a value when multiple options exist, and provides an input box for you to enter the DWORD value, with confirmation that you want to make the change. See the notes in the RegKeySave function if you need to change string or binary values.
This sample macro changes the default File as format of contacts.
You'll need the functions above (RegKeyRead, RegKeySave, RegKeyExists) to use this code, as well as the correct registry key for your version of Outlook.
Sub CheckFileAsFormat() Dim myRegKey As String Dim myValue As String Dim myFileAs As String Dim myAnswer As Integer 'get registry key to work with myRegKey = "HKEY_CURRENT_USER\Software\Microsoft\Office\15.0\Outlook\Contact\FileAsOrder" If myRegKey = "" Then Exit Sub 'check if key exists If RegKeyExists(myRegKey) = True Then 'key exists, read it myValue = RegKeyRead(myRegKey) If myValue = 14870 Then myFileAs = "Company" If myValue = 32791 Then myFileAs = "Last, First" If myValue = 32792 Then myFileAs = "Company (Last, First)" If myValue = 32793 Then myFileAs = "Last, First (Company)" If myValue = 32823 Then myFileAs = "First Last" 'display result and ask if it should be changed myAnswer = MsgBox("The registry value for the key """ & _ myRegKey & """is """ & myFileAs & vbCrLf & _ "Do you want to change it?", vbYesNo) Else 'key doesn't exist, ask if it should be created myAnswer = MsgBox("The registry key """ & myRegKey & _ """ could not be found." & vbCr & vbCr & _ "Do you want to create it?", vbYesNo) End If If myAnswer = vbYes Then 'ask for new registry key value myValue = InputBox("Please enter new value: " & vbCrLf & _ "14870 = Company" & vbCrLf & _ "32791 = Last, First" & vbCrLf & _ "32792 = Company (Last, First)" & vbCrLf & _ "32793 = Last, First (Company)" & vbCrLf & _ "32823 = First Last", myRegKey, myValue) If myValue <> "" Then RegKeySave myRegKey, myValue MsgBox "Registry key saved." End If Else End If End Sub
Enable or Disable Registry Values
This is a simple example and switches the DWORD value between 1 and 0 (to set the option on/enabled or off/disabled). You can use it with any value that is either enabled or disabled.
You'll need the functions above (RegKeyRead, RegKeySave, RegKeyExists) to use this code, as well as the correct registry key for your version of Outlook.
This code sample toggles the Mark My Comments With option on or off in Outlook 2013.
Not all options that are toggled on and off can be changed by code, some may require Outlook to be restarted if you don't use the Options dialog to make the change.
Sub ChangeCommentWith() Dim myRegKey As String Dim myValue As String Dim myFileAs As String Dim myAnswer As Integer 'get registry key to work with myRegKey = "HKEY_CURRENT_USER\Software\Microsoft\Office\15.0\Common\MailSettings\MarkComments" If myRegKey = "" Then Exit Sub 'check if key exists If RegKeyExists(myRegKey) = True Then 'key exists, read it myValue = RegKeyRead(myRegKey) If myValue = 1 Then myValue = "0" Else myValue = 1 Else myValue = 1 End If If myValue <> "" Then RegKeySave myRegKey, myValue MsgBox "Registry key set to " & myValue End If End Sub End Sub
More Information
The above macros are an edited version of the macros at Read and Write Windows Registry with VBA.
Hi,
This does not work with Windows 7 and Office 2010 to read / write registry key in HKLM !
Any solution to read / write HKLM registry from MS Access ?
Cheers,
L@u