How to Determine the Path for an Office Application


This article includes sample code that illustrates how you can programmatically determine the installation path for a Microsoft Office application given the ProgID for that application.

More Information

Automation servers have a unique ProgID that you typically use to automate that server. The following list provides the ProgIDs for Office applications:

Microsoft AccessAccess.Application
Microsoft ExcelExcel.Application
Microsoft OutlookOutlook.Application
Microsoft PowerPointPowerpoint.Application
Microsoft WordWord.Application
Microsoft FrontPageFrontPage.Application

Note that the table above provides version-independent ProgIDs. Applications have version-dependent ProgIDs as well that you can use with the sample code provided in this article. For instance, Microsoft Excel has a version-independent ProgID "Excel.Application" and version-dependent ProgIDs such as "Excel.Application.8" and "Excel.Application.9."

Given a ProgID for an out-of-process server, you can obtain its location by examining the registry. An out-of-process server has a key in the registry at:


that provides its unique CLSID (or Class ID). That CLSID then has a registry key at:


where the path to the server is specified. To illustrate, if you want to determine the path for Microsoft Excel using the ProgID "Excel.Application," you would examine this key in the registry:


and, depending on what version of Excel is installed, you would find that the CLSID for Excel.Application is "{00020841-0000-0000-C000-000000000046}." Next, using this CLSID, you would examine the following registry key to find the path for EXCEL.EXE:


All of this can be done programmatically using the registry API functions in advapi32.dll.

Sample Code

  1. Start a new Standard EXE project in Visual Basic. Form1 is created by default.
  2. Add a TextBox and a CommandButton to Form1.
  3. Add the following code to the module for Form1:
    Private Declare Function RegOpenKeyEx Lib "advapi32.dll" Alias _
    "RegOpenKeyExA" (ByVal hKey As Long, ByVal lpSubKey As String, _
    ByVal ulOptions As Long, ByVal samDesired As Long, phkResult As Long) _
    As Long

    Private Declare Function RegQueryValueEx Lib "advapi32.dll" Alias _
    "RegQueryValueExA" (ByVal hKey As Long, ByVal lpValueName As String, _
    ByVal lpReserved As Long, lpType As Long, _
    ByVal lpData As String, lpcbData As Long) As Long
    'Note that if you declare the lpData parameter as String,
    'you must pass it ByVal.

    Private Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long

    Const REG_SZ As Long = 1
    Const KEY_ALL_ACCESS = &H3F
    Const HKEY_LOCAL_MACHINE = &H80000002

    Private Sub Command1_Click()
    Dim hKey As Long
    Dim RetVal As Long
    Dim sProgId As String
    Dim sCLSID As String
    Dim sPath As String

    sProgId = Text1.Text

    'First, get the clsid from the progid
    'from the registry key:
    RetVal = RegOpenKeyEx(HKEY_LOCAL_MACHINE, "Software\Classes\" & _
    sProgId & "\CLSID", 0&, KEY_ALL_ACCESS, hKey)
    If RetVal = 0 Then
    Dim n As Long
    RetVal = RegQueryValueEx(hKey, "", 0&, REG_SZ, "", n)
    sCLSID = Space(n)
    RetVal = RegQueryValueEx(hKey, "", 0&, REG_SZ, sCLSID, n)
    sCLSID = Left(sCLSID, n - 1) 'drop null-terminator
    RegCloseKey hKey
    End If

    'Now that we have the CLSID, locate the server path at
    ' {xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxx}\LocalServer32

    RetVal = RegOpenKeyEx(HKEY_LOCAL_MACHINE, _
    "Software\Classes\CLSID\" & sCLSID & "\LocalServer32", 0&, _
    If RetVal = 0 Then
    RetVal = RegQueryValueEx(hKey, "", 0&, REG_SZ, "", n)
    sPath = Space(n)

    RetVal = RegQueryValueEx(hKey, "", 0&, REG_SZ, sPath, n)
    sPath = Left(sPath, n - 1)
    MsgBox sPath
    RegCloseKey hKey
    End If

    End Sub
  4. Press the F5 key to run the program.
  5. In the TextBox, type Excel.Application and click the CommandButton. The path to Excel.exe appears in a MessageBox.


For additional information, please click the article numbers below to view the articles in the Microsoft Knowledge Base:
234788 How To Find the Installation Path of an Office 2000 Application
145679 How To Use the Registry API to Save and Retrieve Setting

ID članka: 240794 - posljednja izmjena: 23. ožu 2009. - verzija: 1

Povratne informacije