top of page
John

Working with the Registry using VBA (the 'wider' Registry: the HKCU, HKCR and HKLM hives)

Updated: Oct 2

Always be careful when working with the Registry ... if you are not sure what you are doing then don't do it! ... and always make a backup first ... see How to back up and restore the registry in Windows.


This post concerns working with the Registry outside of the 'local' "HKEY_CURRENT_USER\Software\VB and VBA Program Settings" key for which built-in VBA functions/statements such as GetSetting and SaveSetting can be used. If you want to read about working with the 'local' key, see see my Working with the Registry using VBA (the 'VB and VBA Program Settings' key) post instead.


Specifically, we are talking about working with keys and values in the following Registry 'hives':


  • HKEY_CURRENT_USER (aka HKCU) ... for which, typically, read and write operations will be allowed

  • HKEY_CLASSES_ROOT (aka HKCR) ... for which, typically, only read operations will be allowed

  • HKEY_LOCAL_MACHINE (aka HKLM) ... for which, typically, only read operations will be allowed


... I say "typically" as access to the Registry depends on a number of factors including the permissions set for the individual hives and keys and the permissions you have (i.e. as a user on your device). Discussion of those permissions is outside the scope of this post (but, as usual, there's plenty of information available on the internet).


It might also be useful to understand that HKCR is a 'virtual' hive being the keys in 'HKLM\Software\Classes' and 'HKCU\Software\Classes' merged together.


To read and learn more about the Registry, you may want to start with the Microsoft article Windows registry information for advanced users (not that advanced, I think!).


Acknowledgements


I want to acknowledge here that I am building on the excellent work of others:

  • The RegOp class developed by Romke Soldaat ... I couldn't have created certain elements of my UtilsRegistry Module without this ... however, the code here is very old and won't function in 64-bit Office ... also it just works very differently to how I think ... I'm quite sure this is a shortfall of me, not Romke Soldaat ... but I find the code, as written here, challenging to use

  • Mike Wolfe's 64-Bit RegOp Refresh post ... while I think I likely could have converted all of the 32-bit Windows API calls to 64-bit eventually, Mike did this long before I thought of doing it ... thanks Mike!


Viewing the Registry keys and values using RegEdit


At times it's useful to be able to view the contents of the Registry directly, without having to use code. To do this, use the RegEdit tool (see following image). Hit the Windows button then type "regedit" (without the speech marks).

RegEdit

In the above image (you can click it to expand it):

  • In the left-hand pane you can see the hives such as HKEY_LOCAL_MACHINE (hives are actually just top-level keys and often they just get called 'keys') and you can see the keys within the hives (such as "SOFTWARE", and its sub-key "7-Zip"), and

  • In the right-hand pane you can see the values associated with the selected key, i.e. the "7-Zip" key has 3 values: "(Default)", "Path" and "Path64" ... and each value has a Name, Type and Data


In the above image, each value has a "REG_SZ" type which means it is a fixed-length string. The code here will allow you to work with the following value types:


  • REG_SZ - fixed-length string

  • REG_EXPAND_SZ - a variable-length string ... often used when strings contain environment variables (enclosed in % signs) and, as such, are often file paths

  • REG_MULTI_SZ - a 'multiple string' (array)

  • REG_DWORD - a 4-byte integer ... for VBA, that is a Long

  • REG_QWORD - an 8-byte integer ... for 64-bit VBA, that is a LongLong


Why use the Windows API?


There are other ways of working with the Registry. The alternatives I'm aware of were:


  • The StdRegProv class of the Windows Management Instrumentation (WMI)

  • The RegDelete(), RegRead() and RegWrite() methods of the WScript.Shell object


... but both of these suffer from a fairly fatal flaw: they CAN (but not ALWAYS) trigger the Malicious Macros Detected warning (see here and here) which is followed by an immediately force-close of the host application. Somewhat less than ideal. However, using the Windows API does not (as at the time of writing ... and not in my testing in using this many, many times) ever trigger the Malicious Macros Detected warning.


The code


So here is the code. I always load this into a standard Module named "UtilsRegistry".


You can then use this to: *


  • Create and delete keys ... and check if they exist

  • Read and write values ... Longs, pointers (that is, LongPtr for 64-bit; Long for 32-bit), strings, expandable strings and arrays (1-dimensional, containing strings, numerics or Booleans) ... and check if they exist

  • Get the type associated with a specific value (if it is not already known e.g. before reading a value written by other code)

  • Get all sub-key names from a key

  • Get all value names from a key


* but note the comments above about which hives can be read-from and which can be written-to.


All of the procedures are commented for your understanding so I won't add additional explanation here.



Or you can download a Module containing all of the above code:



0 comments

Recent Posts

See All

Comentários


bottom of page