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.

Note 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-18 shows the result of calling the GetExecutable function, with an argument of the filename for an MP3 audio file. The function returns the full path of the application that's associated with the file.

C^Oocuments and Seltingsljw'iDesktop1'Ragtime ¿nnie.Tnpî

jj^l The e>jeculable fi C ^Program Files

IWinampVWinamp ute OK

Figure 11-18: Determining the path and name of the application associated with a particular file. CD- This example is available on the companion CD-ROM. The filename is % file

ROM association.xlsm.

0 0

Post a comment