10

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)".

3
  • 3
    @rene: it depends on what he means. On a 32 bit Win XP, there is just a "Program Files" folder. "Program Files (x86)" is normally only available under 64 bit windows.
    – Doc Brown
    Jan 2, 2012 at 14:40
  • @docbrown Yes, that's what I meant thanks!
    – James
    Jan 2, 2012 at 15:30
  • Note that I'm fairly sure this depends on whether the application that does the request is 32 or 64 bit.
    – Nyerguds
    Nov 28, 2017 at 9:03

5 Answers 5

16

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:

  • Machine 1: Win 7 Ultimate, 64 Bit, Office 2010 64 Bit
  • Machine 2: Win 7 Ultimate, 32 Bit, Office 2007 32 Bit

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.

3
  • Great solution; quick question what happens if we run this on a 32-bit machine, does Environ("PROGRAMFILES(X86)") just return blank? Sorry I' don't have one to hand.
    – James
    Jan 2, 2012 at 15:30
  • I don't have one handy either, but environ("HELLO_WORLD") returns an empty string so I would imagine you'd get the same result if that that variable is used only on x64 machines. Virtual PC (and other OS emulators) are free. If you've got an x86 OS lying around, blow the dust off, install in virtual environment and see what happens. :)
    – ray
    Jan 2, 2012 at 17:33
  • 1
    Also, I just saw Christian's answer and I should note that on my machine, I get different folders with var "ProgramFiles" vs. "PROGRAMFILES(X86)". You will probably be best off using his recommendation for getting the x64 folder; and, just speculating here, if both "PROGRAMFILES(X86)" and "ProgramW6432" return blank values, then "ProgramFiles" would then be applicable.
    – ray
    Jan 2, 2012 at 17:36
10

One-stop shop

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
6

ray023's answer is basically correct, but one addition:

At least on my machine (Win 7 Home Premium 64 bit, Access 2000 installed), both
Environ("ProgramFiles") and
Environ("PROGRAMFILES(X86)")

...return the same folder, C:\Program Files (x86).

To get the "non-x86-folder" (C:\Program Files) on my 64 bit Windows, I need to use Environ("ProgramW6432").

Here's another link about the Environ function, including code how to list all environment variables (that's how I found ProgramW6432).


EDIT:

As I already said in a comment, I just tested it on my other machine as the results seem to depend not only on the operating system, but on the installed MS Office version as well:

This machine runs on Win XP SP3 32-bit, and Access 2000 is installed:

Environ("ProgramFiles") returns C:\Programme.
(that's "Program Files" in German - I'm in Germany and my Windows is in German)

Environ("PROGRAMFILES(X86)") and Environ("ProgramW6432") return an empty string.

--> So the safest way to determine the "x86 folder" (no matter if on Win XP or Win 7) seems to be Environ("ProgramFiles").

4
  • I can't confirm this; I get the expected result? I'm using the 64-bit version of office, can I confirm that you are, as I suspect you're not? Thank for helping though.
    – James
    Jan 2, 2012 at 16:37
  • No, I'm using a 32-bit version (Access 2000, to be specific - that's the only part of MS Office that's installed on this machine). Jan 2, 2012 at 16:48
  • @ChristianSpecht That's interesting b/c I have 64bit word and I get different folders using ProgramFiles vs. PROGRAMFILES(X86). I wonder if that plays a role when using this command; not to the point where I want to take time to test it, but might be something for James. :P
    – ray
    Jan 2, 2012 at 17:44
  • Strange. So it seems to depend on the operating system version and the installed MS Office version. I have another machine with Win XP 32-bit and Access 2000, I'll test on that one and report the results. Jan 2, 2012 at 18:46
5

Here is an article showing you how to read this information from the registry:

http://accesstips.wordpress.com/2010/01/08/get-a-computers-program-files-folder-path-with-access-vba-and-wmi/

In the comments of that article is also a hint how to retrieve the information from the environment variable "ProgramFiles". But beware, if you have different partitions, there may be more than one "Program Files" folder, for example "C:\Program Files" and "D:\Program Files".

1

Environ("ProgramFiles") is enough statement for both 32 bit and 64 bit system ... in 64 bit you will get Result C:\Program Files (x86)

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.