Read Registry Keys (and possibly write)

M

Mike Iacovou

Hi all.

I've had a quick search & "google" for vba code to read (and maybe write) to
registry keys. I know there is a GetSetting / SaveSetting function for
specific keys, but I'm hoping to be able to read other registry key
information (mainly application paths). I suppose this can be achieved by a
COM call - but would appreciate any tutorials / guides / info.
For the sake of argument, reading the "path" of "excel.exe" in current apps
for any demo code.
TIA
 
J

Jim Cone

Here is a start...
http://support.microsoft.com/kb/145679/en-us#top
"How To Use the Registry API to Save and Retrieve Setting"

Jim Cone
San Francisco, USA
http://www.realezsites.com/bus/primitivesoftware



"Mike Iacovou" <[email protected]>
wrote in message
Hi all.
I've had a quick search & "google" for vba code to read (and maybe write) to
registry keys. I know there is a GetSetting / SaveSetting function for
specific keys, but I'm hoping to be able to read other registry key
information (mainly application paths). I suppose this can be achieved by a
COM call - but would appreciate any tutorials / guides / info.
For the sake of argument, reading the "path" of "excel.exe" in current apps
for any demo code.
TIA
 
S

Steve Yandl

Here are two approaches other than using Windows API functions.

For the examples, these would return the default value under
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App
Paths\excel.exe

First,

Dim wsh
strKeyPath = "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\" _
& "CurrentVersion\App Paths\excel.exe\"
Set wsh = CreateObject("WScript.Shell")
MsgBox wsh.RegRead(strKeyPath)

of if you're on WinMe/XP/2k or beyond (WMI is installed on the PC), you
could use

Const HKEY_LOCAL_MACHINE = &H80000002
strComputer = "."
Set oReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & _
strComputer & "\root\default:StdRegProv")
strKeyPath = "SOFTWARE\Microsoft\Windows\CurrentVersion\App
Paths\excel.exe"
strValueName = ""
oReg.GetStringValue HKEY_LOCAL_MACHINE,strKeyPath,strValueName,strValue
MsgBox strValue


Using WMI gives you more flexibility when reading multiple subkeys or values
but the "WScript.Shell" option would be available on more machines. You can
also write new values or amend existing ones.

Steve
 
M

Mike Iacovou

@Jim & Steve - many thanks.
@Ron - thanks as ever. The WSH method is simple - but I am not sure if this
is present on Win95/NT systems - and the vbapp is running on these :(
I opted to modify the API call example you referenced - and it works nicely
( i assumed that this will be compatible across the whole windows family)
thanks again to all.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top