Get Program Files path

Tested in Excel 365 (16.8730.2046) 64-bit

 
 
  1. Function GetProgFilesPath() As String
  2.     Dim objReg As Object
  3.     Dim strComputer As String
  4.     Dim strKeyPath As String
  5.     Dim ValueName As String
  6.     Dim strValue
  7.     Const HKEY_LOCAL_MACHINE = &H80000002
  8.    
  9.     strComputer = "."
  10.    
  11.     Set objReg = GetObject("winmgmts:" _
  12.         & "{impersonationLevel=impersonate}!\\" _
  13.         & strComputer & "\root\default:StdRegProv")
  14.    
  15.     strKeyPath = "SOFTWARE\Microsoft\Windows\CurrentVersion"
  16.     ValueName = "ProgramFilesDir"
  17.    
  18.     ' Get the Program Files folder from the Registry
  19.    objReg.GetStringValue HKEY_LOCAL_MACHINE, strKeyPath, ValueName, strValue
  20.    
  21.     ' Set the value of this Function
  22.    GetProgFilesPath = strValue
  23.    
  24.     ' Release Memory
  25.    Set objReg = Nothing
  26. End Function

Also, you can use environ() function. See below a list of arguments (use the index or the text):

Index Text
1 ALLUSERSPROFILE
2 APPDATA
3 CommonProgramFiles
4 CommonProgramFiles(x86)
5 CommonProgramW6432
6 COMPUTERNAME
7 ComSpec
8 DriverData
9 FPS_BROWSER_APP_PROFILE_STRING
10 FPS_BROWSER_USER_PROFILE_STRING
11 HOMEDRIVE
12 HOMEPATH
13 LOCALAPPDATA
14 LOGONSERVER
15 NUMBER_OF_PROCESSORS
16 OneDrive
17 OneDriveConsumer
18 OS
19 Path
20 PATHEXT
21 PROCESSOR_ARCHITECTURE
22 PROCESSOR_IDENTIFIER
23 PROCESSOR_LEVEL
24 PROCESSOR_REVISION
25 ProgramData
26 ProgramFiles
27 ProgramFiles(x86)
28 ProgramW6432
29 PSModulePath
30 PUBLIC
31 SESSIONNAME
32 SystemDrive
33 SystemRoot
34 TEMP
35 TMP
36 USERDOMAIN
37 USERDOMAIN_ROAMINGPROFILE
38 USERNAME
39 USERPROFILE
40 windir
41 __COMPAT_LAYER

Example:

 
 
  1. Environ(ProgramFiles)

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.