HOWTO: Determine the Path for an Office Application
ID: Q240794
|
The information in this article applies to:
-
Microsoft Visual Basic Professional and Enterprise Editions for Windows, versions 5.0, 6.0
-
Microsoft Visual Basic for Applications versions 5.0, 6.0
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:
Application |
ProgID |
Microsoft Access |
Access.Application |
Microsoft Excel |
Excel.Application |
Microsoft Outlook |
Outlook.Application |
Microsoft PowerPoint |
Powerpoint.Application |
Microsoft Word |
Word.Application |
Microsoft FrontPage |
FrontPage.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:
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\<PROGID>\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.
REFERENCES
For additional information, please click the article numbers below
to view the articles in the Microsoft Knowledge Base:
Q234788 HOWTO: Find the Installation Path of an Office 2000 Application
Q145679 HOWTO: Use the Registry API to Save and Retrieve Setting
© Microsoft Corporation 1999, All Rights Reserved. Contributions by Lori Turner, Microsoft Corporation
Additional query words:
Keywords : kbAutomation kbRegistry KbVBA kbVBp kbVBp500 kbVBp600 kbGrpDSO kbDSupport
Version : WINDOWS:5.0,6.0
Platform : WINDOWS
Issue type : kbhowto
|