Determining file associations

In Windows, many file types are associated with a particular application. This association makes it possible to double-click the file to load it into its associated application.

The following function, named GetExecutable, uses a Windows API call to get the full path to the application associated with a particular file. For example, your system has many files with a .txt extension — one named Readme.txt is probably in your Windows directory right now. You can use the GetExecutable function to determine the full path of the application that opens when the file is double-clicked.

Windows API declarations must appear at the top of your VBA module.

Private Declare Function FindExecutableA Lib "shell32.dll" _ (ByVal lpFile As String, ByVal lpDirectory As String, _ ByVal lpResult As String) As Long

Function GetExecutable(strFile As String) As String Dim strPath As String Dim intLen As Integer strPath = Space(255)

intLen = FindExecutableA(strFile, "\", strPath) GetExecutable = Trim(strPath) End Function

Figure 11-13 shows the result of calling the GetExecutable function, with an argument of the filename for a Word file. The function returns the full path of the application that's associated with the file.

Figure 11-13: Determining the path of the application associated with a particular file.

Figure 11-13: Determining the path of the application associated with a particular file.

Was this article helpful?

0 0

Post a comment