PDA

View Full Version : Best method to detect 32/64-bit Program Files Folder?



partlysunny
08-23-2012, 01:06 PM
Hi all, new to the forum.

We need one of our macros to find a file and read the first line. The file will always be located in either:

"c:\Program Files\Microsoft Office\Templates\"

OR

"c:\Program Files(x86)\Microsoft Office\Templates\"

Right now our code only works for the first scenario, and reads:

Dim configfile As String
' Get the templates path
a$ = "c:\Program Files\Microsoft Office\Templates\"
configfile = a$ & "configfile.txt"

Been playing with detecting whether it's 64-bit using the Environ("ProgramFiles") and Environ("ProgramFiles(86)") variables, but the deeper we get the more unnecessarily complicated it becomes.

Is there an elegant/simple way for the macro to always look for BOTH paths and pass forward the one that's correct? (Btw, we know we shouldn't be hard coding any paths, long story...)

Thanks so much for any insights.

Kenneth Hobs
08-23-2012, 01:37 PM
Welcome to the forum!

If always in either, use Dir().

partlysunny
08-23-2012, 02:36 PM
Great idea Kenneth, thank you. Upon locating the desired file, any thoughts on the best way to assign the valid location to a variable (a$)?

Dim configfile As String
' Get the templates path
If Dir$("C:\Program Files\Microsoft Office\Templates\MyApp\") <> "" Then
a$ = [the above path]
configfile = a$ & configfile.txt
Else
If Dir$("C:\Program Files(x86)\Microsoft Office\Templates\MyApp\") <> "" Then
a$ = [B][the above path]
configfile = a$ & configfile.txt
End If
End If

Kenneth Hobs
08-23-2012, 07:00 PM
Sub Test_MyPath()
Dim s As String
s = MyPath & "configFile.txt"
MsgBox s & vblf & Dir(s) <> ""
End Sub

Function MyPath() As String
' Get the templates path
If Dir("C:\Program Files\Microsoft Office\Templates\MyApp\", vbDirectory) <> "" Then _
MyPath = "C:\Program Files\Microsoft Office\Templates\MyApp\"
If Dir("C:\Program Files(x86)\Microsoft Office\Templates\MyApp\", vbDirectory) <> "" Then _
MyPath = "C:\Program Files(x86)\Microsoft Office\Templates\MyApp\"
End Function

macropod
08-23-2012, 08:38 PM
Cross-posted at: http://social.msdn.microsoft.com/Forums/en-US/worddev/thread/de06df71-4c3f-4590-8b3c-060831b4f15e
For cross-posting etiquette, please read: http://www.excelguru.ca/content.php?184

snb
08-24-2012, 03:32 AM
Did you ever use ?

Sub snb()
j = 1
For Each sf In CreateObject("wscript.shell").specialfolders
Cells(j, 1) = sf
j = j + 1
Next
End Sub

partlysunny
08-24-2012, 10:45 AM
Kenneth, thank you again, your code structure seems much cleaner than what we were trying and yours clearly defines the variable. Thank you!