Getting File Information From The Registry
This page describes how to access the System Registry to determine attributes of a file
based upon the file's extension..
You can use VBA's built-in functions and the functions provided by the Microsoft Scripting Runtime (typically at
C:\System\System32\scrrun.dll to retrieve various information about a files. However, two important pieces of
information cannot be had using these methods. The first property is the descriptive name of a document. For example, a file
with an extension of .xls has the type Microsoft Office Excel 97-2003 Worksheet. The
second missing property is the file name of the application that is used to open that file. For a file with an extension
of .xls, the file is C:\Program Files\Microsoft Office\Office11\Excel.exe.
Of course, the actual path of the Excel application may be different on your machine.
This page provides two functions for retrieving these properties.
- GetFileDesciptionFromExtension. This function takes as a parameter the extension of a file,
such as .xls. Since the functions works with only the file extension (the leading period is
required), you don't need to specify an existing file. It returns the description of a file based on the extension. For
example, for the extension .xls, the function returns Microsoft Excel 97-2003 Workbook.
- GetExecutableFileFromExtension. This function returns the fully qualified name of the
executable file used to process files with a specified extension. For example, if you pass in the extension .xls,
the function will return C:\Program Files\Microsoft Office\Office11\Excel.exe. Of course, the actual file name will
vary from one installation to the next.
This function takes in as a parameter a file name extension. Note that while the leading periods are required by the API code (e.g.,
.xls rather than xls, the condition of a missing period is corrected by the code.
The function returns as its result the document description. For an extension of .xls, the code returns
the the string Microsoft Office Excel 97-2003 Worksheet. If an error occurs or a registry entry cannot be found, the
result is vbNullString. The code listing is included at the end of this page.
The following components are required for the code to work. Both functions described on this page share the external requirements, so
they will be listed only once.
- Module: modGetSystemErrorMessageText. This is included in the downloadable workbook or availalble
directly from the Format Message page.
- Module: modRegistry, This is included in the downloadable workbook or as a direct download from
modRegistry.zip.
- DLL Files: The Microsoft Scripting RunTime, included in the Setup Zip file>, and
the Microsoft XML Version 6 DLL, also included in the Setup Zip file>. The
Setup Zip file> will automatically copy these files to the appropriate folders and register those
files with Windows, a step normally taken by using the RegSvr32.exe program.
This function takes in as a parameter a file name extension. Note that while the leading periods are required by the API code (e.g.,
.xls rather than xls, the condition of a missing period is corrected by the code.
The function returns as its result the file name of the executable file used to open and process the file. For an extension
of .xls, the code returns the the string C:\Program Files\Microsoft Office\Office\Excel.exe.
If an error occurs or a registry entry cannot be found, the result is vbNullString. The code listing is
included at the end of this page.
The code for GetFileDesciptionFromExtension is shown below.
Function GetFileDesciptionFromExtension(Extension As String) As String
Dim ProgID As Variant
Dim Description As Variant
Dim B As Boolean
Dim Ext As String
If Left(Extension, 1) = "." Then
Ext = Extension
Else
Ext = "." & Extension
End If
B = RegistryKeyExists(HKEY_CLASSES_ROOT, Ext, False)
If B = False Then
Exit Function
End If
ProgID = RegistryGetValue(HKEY_CLASSES_ROOT, Ext, vbNullString)
If IsNull(ProgID) Then
Exit Function
End If
B = RegistryKeyExists(HKEY_CLASSES_ROOT, CStr(ProgID), False)
If B = False Then
Exit Function
End If
Description = RegistryGetValue(HKEY_CLASSES_ROOT, CStr(ProgID), vbNullString)
If IsNull(Description) Then
Exit Function
End If
GetFileDesciptionFromExtension = CStr(Description)
End Function
The code for is shown below.
Function GetExecutableFileFromExtension(Extension As String) As String
Dim ProgID As Variant
Dim Description As Variant
Dim B As Boolean
Dim Ext As String
Dim CLSID As Variant
Dim ShortFileName As Variant
Dim LongFileName As String
Dim L As Long
If Left(Extension, 1) = "." Then
Ext = Extension
Else
Ext = "." & Extension
End If
B = RegistryKeyExists(HKEY_CLASSES_ROOT, Ext, False)
If B = False Then
Exit Function
End If
ProgID = RegistryGetValue(HKEY_CLASSES_ROOT, Ext, vbNullString)
If IsNull(ProgID) Then
Exit Function
End If
B = RegistryKeyExists(HKEY_CLASSES_ROOT, CStr(ProgID), False)
If B = False Then
' If it doesn't exist, get out.
Exit Function
End If
Description = RegistryGetValue(HKEY_CLASSES_ROOT, CStr(ProgID), vbNullString)
If IsNull(Description) Then
Exit Function
End If
B = RegistryKeyExists(HKEY_CLASSES_ROOT, CStr(ProgID) & "\CLSID", False)
If B = False Then
Exit Function
End If
CLSID = RegistryGetValue(HKEY_CLASSES_ROOT, CStr(ProgID) & "\CLSID", vbNullString)
If IsNull(CLSID) Then
End If
ShortFileName = RegistryGetValue(HKEY_CLASSES_ROOT, "CLSID\" & CLSID & "\LocalServer32", vbNullString)
If IsNull(ShortFileName) Then
Exit Function
End If
L = 255
LongFileName = String(L, vbNullChar)
L = GetLongPathName(ShortFileName, LongFileName, L)
LongFileName = Left(LongFileName, L)
GetExecutableFileFromExtension = LongFileName
End Function
This page last updated: 05-July-2008