Get a Computer’s Program Files Folder Path with Access VBA and WMI



I came across this blog post,
Hey, Scripting Guy! How can I retrieve the path to the Program Files folder on a computer? — CC, and I began thinking how useful knowing the Program Files Path could be. I thought about how it would not just be helpful but even essential information in certain development situations because the Program Files Directory can be at different locations on other computers. Knowing the Program Files Directory Path is essential for an Access developer who produces software that needs to be installed or saved in the Program Files Directory. If an installation program is used the Program Files Path could be accessed by a database which could then copy or save another database and other files to the Program Files Directory.


But first we need to have the code to get the Program Files Directory from information in the Registry.


It is usually fairly easy to modify VBScript to produce a VBA procedure. Many of the procedures in this series of posts on using WMI in VBA were developed this way. So I want to thank the Scripting Guys for the post mentioned above and the many other scripts I have morphed into VBA.


The following Function can retrieve the computer’s Program Files Path for us:

Function GetProgFilesPath() As String

    Dim objReg As Object
    Dim strComputer As String
    Dim strKeyPath As String
    Dim ValueName As String
    Dim strValue

    strComputer = "."
     
    Set objReg = GetObject("winmgmts:" _
        & "{impersonationLevel=impersonate}!\\" _
        & strComputer & "\root\default:StdRegProv")

    ' Get the Program Files folder from the Registry      
    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


The next procedure shows how simple it is to use our Function and copy a file to the Program File Directory.

Sub CopyFile()
 
    Dim strFilePath As String
    Dim strProgFilesPath As String
 
    ' The path and name of the database to copy
    strFilePath = "C:\MainDB.mdb"
 
    ' Get the Program Files Directory Path
    strProgFilesPath = GetProgFilesPath
 
    ' Copy the Database to the Program Files Directory
    FileCopy strFilePath, strProgFilesPath & "\MainDB.mdb"
 
    ' We are finished with this DB so close it
    Application.Quit
 
End Sub


The file that is being copied cannot be open or it will raise an error so we must use two databases to make this work. The first database to open will be the one that runs the code in this post. It can run the two procedures and then Application.Quit to close that database.


My good friend Bill Mosca, founder of my favorite Access site MS_Access_Professionals a Yahoo group, made an excellent comment, showing us an easier way to get the Program Files folder path. Unfortunately, the Comments link is a little hard to find so I have added his comments here so everyone can easily see them:


In case the user does not have rights to the registry you might have to resort to a scripting method. This will also work.

Public Function GetProgramFilesFolder() As String
    Const PROGRAM_FILES = &H26&
    Dim objShell As Object
    Dim objFolder As Object
    Dim objFolderItem As Object

    Set objShell = CreateObject("Shell.Application")
    Set objFolder = objShell.Namespace(PROGRAM_FILES)
    Set objFolderItem = objFolder.Self

    Debug.Print objFolderItem.path

End Function


Thanks for sharing that with us Bill.


Access and VBA are a powerful combination that make it easy to develop great applications. We will learn more about using the power of VBA and WMI in our next post.


You can download the free sample database, WMISample.zip, which contains the code used in this article plus other code samples. The code used in this article is located in the PAWWMIRegistry Module.

Other free downloads include our new Microsoft Access and SQL Azure and demonstration application which uses Microsoft Access and SQL Azure to leverage the power of cloud computing.

More Free Downloads:
Us or UK/AU Pop-up Calendar
Report Date Dialog Form in US or UK/AU Version.
Free Church Management Software with Contributions management.
Code Samples


Get the Access and Outlook Appointment Manager to manage all of your Outlook Calendar Appointments and Access dated information.


Happy computing,

Patrick (Pat) Wood
Gaining Access

About these ads

9 Comments

  1. Posted January 8, 2010 at 3:55 PM | Permalink |

    Hi Pat

    In case the user does not have rights to the registry you might have to resort to a scripting method. This will also work.

    Public Function GetProgramFilesFolder() As String
    Const PROGRAM_FILES = &H26&
    Dim objShell As Object
    Dim objFolder As Object
    Dim objFolderItem As Object

    Set objShell = CreateObject(“Shell.Application”)
    Set objFolder = objShell.Namespace(PROGRAM_FILES)
    Set objFolderItem = objFolder.Self

    Debug.Print objFolderItem.Path

    End Function

    • Posted January 8, 2010 at 5:33 PM | Permalink |

      Thank you for your excellent comments, Bill. They are so good I took the liberty of adding them to the article and giving you credit.

      • Posted January 9, 2010 at 3:42 PM | Permalink

        Thanks, Pat. That’s very nice of you. Keep those great articles coming!

      • Posted January 9, 2010 at 5:31 PM | Permalink

        Thanks for your help and encouragement Bill.

  2. Posted January 13, 2010 at 3:40 PM | Permalink |

    You can also get the Program Files path from the environment:

    Function GetProgramFilesFolder() As String
    GetProgramFilesFolder = Environ(“ProgramFiles”)
    End Function

    • Posted January 13, 2010 at 5:28 PM | Permalink |

      Thanks JP that is excellent code. And since you brought it up I think the subject of using the Environ Function deserves and article of its own.

  3. Posted March 19, 2010 at 9:50 AM | Permalink |

    ‘my solution

    Function GetProgFilesPath(ByVal sExecutable As String) As String
    On Error GoTo Err_Exit
    Dim obj As Object
    Select Case LCase(sExecutable)
    Case “winword.exe”, “word.exe”
    Set obj = CreateObject(“Word.Application”)
    Case “excel.exe”
    Set obj = CreateObject(“Excel.Application”)
    Case “outlook.exe”
    Set obj = CreateObject(“Outlook.Application”)
    Case Else
    MsgBox “Not Managed” & sExecutable, vbInformation
    Exit Function
    End Select
    GetProgFilesPath = obj.path
    Set obj = Nothing

    Exit_Exit:
    Exit Function
    Err_Exit:
    MsgBox “IW_Generali:GetProgFilesPath=” & Err.Description
    Resume Exit_Exit
    End Function

    • Posted March 21, 2010 at 10:25 PM | Permalink |

      I think what Patrick was posting about was locating the official “Program Files” folder on any given computer, i.e. C:\Program Files\, not the path for a program’s files.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: