How do I determine the full path of the folder that contains 32-bit programs using VBA? It's called "Program Files" on 32-bit Windows systems, but on 64-bit systems it's called "Program Files (x86)".
Environ will do the trick:
debug.print Environ("ProgramFiles")
debug.print Environ("PROGRAMFILES(X86)")
'If you want to check if current PC is x64
debug.print Environ("PROCESSOR_IDENTIFIER")
List of environment variables can be found here.
UPDATE: Based on the conversation I've had with Christian and based on my comments, I looked into this a little more.
I have two machines I tested on:
I ran the following statements in the immediate window:
? Environ("ProgramFiles")
? Environ("PROGRAMFILES(X86)")
? Environ("ProgramW6432")
Results
Machine 1:
C:\Program Files
C:\Program Files (x86)
C:\Program Files
Machine 2:
C:\Program Files
//Blank//
//Blank//
So, based on these limited findings, you may want to see the if ProgramW6432 has a value. If not, assume 32 bit and use ProgramFiles.
IF Environ("ProgramW6432") <> "" THEN
'I'm 64 bit so check both ProgramW6432 and PROGRAMFILES(X86)
ELSE
'I'm 32 bit so check ProgramFiles
END IF
Conversely, you could use PROCESSOR_IDENTIFIER to determine x64 vs. x86 and do the same thing.
I wouldn't say either way is foolproof but should get you on the right track.
I thought I'd summarize the conclusions that can be drawn from the information scattered across all answers here, plus a bit of integration on my part. Credits to all previous answer posters!
The output of Environ
for any given "program file"-related environment variable will vary depending on Windows (32 or 64 bit) as well as Office (32 or 64 bit) as follows:
Windows Office ProgramFiles PROGRAMFILES(X86) ProgramW6432
------- ------ ---------------------- --------------------- ----------------
32-bit 32-bit C:\Program Files [empty string] [empty string]
64-bit 32-bit C:\Program Files (x86) C:\Program Files (x86) C:\Program Files
64-bit 64-bit C:\Program Files C:\Program Files (x86) C:\Program Files
Note that for the Windows 64 bit + Office 32 bit setup, the output of Environ("ProgramFiles")
does not match with the actual value of the ProgramFiles
environment variable in Windows! At the command prompt, echo %ProgramFiles%
returns C:\Program Files
, not C:\Program Files (x86)
.
If you need the path to the 32 bit program files folder (C:\Program Files
for 32-bit Windows, and C:\Program Files (x86)
for 64-bit Windows) then you can use this function:
Function Get32BitProgramFilesPath() As String
If Environ("ProgramW6432") = "" Then
'32 bit Windows
Get32BitProgramFilesPath = Environ("ProgramFiles")
Else
'64 bit Windows
Get32BitProgramFilesPath = Environ("ProgramFiles(x86)")
End If
End Function
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With