Microsoft KB Archive/240794

= How to Determine the Path for an Office Application =

Article ID: 240794

Article Last Modified on 7/15/2004

-

APPLIES TO


 * Microsoft Visual Basic 6.0 Professional Edition
 * Microsoft Visual Basic 5.0 Professional Edition
 * Microsoft Visual Basic 6.0 Enterprise Edition
 * Microsoft Visual Basic 5.0 Enterprise Edition
 * Microsoft Visual Basic for Applications 6.0
 * Microsoft Visual Basic for Applications 5.0
 * Microsoft Office Access 2003
 * Microsoft Access 2002 Standard Edition
 * Microsoft Access 2000 Standard Edition
 * Microsoft Office Excel 2003
 * Microsoft Excel 2002 Standard Edition
 * Microsoft Excel 2000 Standard Edition
 * Microsoft Office FrontPage 2003
 * Microsoft FrontPage 2002 Standard Edition
 * Microsoft FrontPage 2000 Standard Edition
 * Microsoft Office PowerPoint 2003
 * Microsoft PowerPoint 2002 Standard Edition
 * Microsoft PowerPoint 2000 Standard Edition
 * Microsoft Office Word 2003
 * Microsoft Word 2002 Standard Edition
 * Microsoft Word 2000 Standard Edition

-



This article was previously published under Q240794



SUMMARY
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:

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:

HKEY_LOCAL_MACHINE\Software\Classes\ PROGID \CLSID

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

HKEY_LOCAL_MACHINE\Software\Classes\CLSID\ {xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxx} \LocalServer32

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:

HKEY_LOCAL_MACHINE\Software\Classes\Excel.Application\CLSID

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:

HKEY_LOCAL_MACHINE\Software\Classes\CLSID\{00020841-0000-0000-C000-000000000046}\LocalServer32

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

Sample Code
 Start a new Standard EXE project in Visual Basic. Form1 is created by default. Add a TextBox and a CommandButton to Form1.  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: 'HKEY_LOCAL_MACHINE\Software\Classes\\CLSID 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   'HKEY_LOCAL_MACHINE\Software\Classes\CLSID\ '    {xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxx}\LocalServer32

RetVal = RegOpenKeyEx(HKEY_LOCAL_MACHINE, _       "Software\Classes\CLSID\" & sCLSID & "\LocalServer32", 0&, _      KEY_ALL_ACCESS, hKey) 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  Press the F5 key to run the program. In the TextBox, type Excel.Application and click the CommandButton. The path to Excel.exe appears in a MessageBox.

<div class="references_section">