ThreeWave 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..
ShortFadeBar

SectionBreak

File Information

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.

SectionBreak

GetFileDesciptionFromExtension

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.

SectionBreak

External Requirements

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.

SectionBreak

GetExecutableFileFromExtension

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.

SectionBreak

GetFileDesciptionFromExtension

The code for GetFileDesciptionFromExtension is shown below.

Function GetFileDesciptionFromExtension(Extension As String) As String
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' GetFileDesciptionFromExtension
' This function returns the descriptive name of a document type based on the
' extension of the document file. For example, given an extension of 'xls' the
' function returns 'Microsoft Office Excel 97-2003 Worksheet'.
' This module requires the modRegistry file. This file is included in the
' downloadable workbook example, or directly from the cpearson.com web site
' at 'http://www.cpearson.com/Zips/modRegistry.zip'.
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Dim ProgID As Variant
Dim Description As Variant
Dim B As Boolean
Dim Ext As String

' The class entry requires a leading period in the extension.
' An example extension, for this case Excel, is '.xls'.
If Left(Extension, 1) = "." Then
    Ext = Extension
Else
    Ext = "." & Extension
End If

' See if the registry key exists for the specified extension.
' If it doesn't, get out immediately.
B = RegistryKeyExists(HKEY_CLASSES_ROOT, Ext, False)
If B = False Then
    Exit Function
End If
' Key does exist. Get the default value of the key (name = vbNullString)
' The ProdID is the indentifier of the documents with an Ext
' extension. For example, the ProgID associated with '.xls' is
' 'Microsoft Office Excel 97-2003 Worksheet'. We will translate
' the ProgID to a file description later. 
ProgID = RegistryGetValue(HKEY_CLASSES_ROOT, Ext, vbNullString)
If IsNull(ProgID) Then
    ' If the result is Null, the value was not found. Get out. 
    Exit Function
End If

' Otherwise, we attempt to find the registry key equal ProgID. 
B = RegistryKeyExists(HKEY_CLASSES_ROOT, CStr(ProgID), False)
If B = False Then
    ' If it doesn't exist, get out. 
    Exit Function
End If

' Get the default value for the ProgID entry. The default value of the ProgID
' is the name of the document type. For example, the default value for
' ProgID of 'Excel.Sheet.8" is 'Microsoft Office 97-2003 Worksheet'. 
Description = RegistryGetValue(HKEY_CLASSES_ROOT, CStr(ProgID), vbNullString)
' If Description is Null, the value cannot be found so get out. 
If IsNull(Description) Then
    Exit Function
End If
' Otherwise, return the file description string. 
GetFileDesciptionFromExtension = CStr(Description)

End Function

SectionBreak

GetExecutableFileFromExtension

The code for is shown below.

Function GetExecutableFileFromExtension(Extension As String) As String
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' GetExecutableFileFromExtension
' This function return the executable file(exe) that is associated with a file
' extension. The registry mapping from extension to exe file is how Windows
' knows to open an 'xls' file with Excel rather than with PowerPoint, for example.
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
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

' The class entry requires a leading period in the extension.
' An example extension, for this case Excel, is '.xls'. 
If Left(Extension, 1) = "." Then
    Ext = Extension
Else
    Ext = "." & Extension
End If

' See if the registry key exists for the specified extension.
' If it doesn't, get out immediately. 
B = RegistryKeyExists(HKEY_CLASSES_ROOT, Ext, False)
If B = False Then
    Exit Function
End If
' Key does exist. Get the default value of the key (name = vbNullString)
' The ProdID is the indentifier of the documents with an Ext
' extension. For example, the ProgID associated with '.xls' is
' 'Microsoft Office Excel 97-2003 Worksheet'. We will translate
' the ProgID to a file description later. 
ProgID = RegistryGetValue(HKEY_CLASSES_ROOT, Ext, vbNullString)
If IsNull(ProgID) Then
    ' If the result is Null, the value was not found. Get out. 
    Exit Function
End If

' Otherwise, we attempt to find the registry key equal ProgID. 
B = RegistryKeyExists(HKEY_CLASSES_ROOT, CStr(ProgID), False)
If B = False Then
    ' If it doesn't exist, get out.
    Exit Function
End If

' Get the default value for the ProgID entry. The default value of the ProgID
' is the name of the document type. For example, the default value for
' ProgID of 'Excel.Sheet.8" is 'Microsoft Office 97-2003 Worksheet'. 
Description = RegistryGetValue(HKEY_CLASSES_ROOT, CStr(ProgID), vbNullString)
' If Description is Null, the value cannot be found so get out. 
If IsNull(Description) Then
    Exit Function
End If

B = RegistryKeyExists(HKEY_CLASSES_ROOT, CStr(ProgID) & "\CLSID", False)
If B = False Then
    ' can't find CSLID. get out. 
    Exit Function
End If

' the CLSID, e.g., {00020820-0000-0000-C000-000000000046} is the default
' value of the CLSID key. 
CLSID = RegistryGetValue(HKEY_CLASSES_ROOT, CStr(ProgID) & "\CLSID", vbNullString)
If IsNull(CLSID) Then
    ' value not found. get out. 
End If
' Next, find the CLSID key in the registry. This contains a key named "LocalServer32"
' whose default value of the executable associdated with the CLSID. Thus, for
' an extension like '.xls' we can get the file name "C:\ProgramFiles\Microsoft Office\Office\Excel.exe 
ShortFileName = RegistryGetValue(HKEY_CLASSES_ROOT, "CLSID\" & CLSID & "\LocalServer32", vbNullString)
If IsNull(ShortFileName) Then
    ' can't get exe file. get out. 
    Exit Function
End If

' got the exe in short file (8.3) format. Convert to long. 
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

-->