Tested in Excel 365 (16.8730.2046) 64-bit
- Function GetProgFilesPath() As String
- Dim objReg As Object
- Dim strComputer As String
- Dim strKeyPath As String
- Dim ValueName As String
- Dim strValue
- Const HKEY_LOCAL_MACHINE = &H80000002
- strComputer = "."
- Set objReg = GetObject("winmgmts:" _
- & "{impersonationLevel=impersonate}!\\" _
- & strComputer & "\root\default:StdRegProv")
- strKeyPath = "SOFTWARE\Microsoft\Windows\CurrentVersion"
- ValueName = "ProgramFilesDir"
- ' Get the Program Files folder from the Registry
- objReg.GetStringValue HKEY_LOCAL_MACHINE, strKeyPath, ValueName, strValue
- ' Set the value of this Function
- GetProgFilesPath = strValue
- ' Release Memory
- Set objReg = Nothing
- 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:
- Environ(ProgramFiles)