Filtering Files using the comdlg32.dll
What is Comdlg32.Dll
The Win32® Common Dialog API functions are located in COMDLG32.DLL, which must be in the
Windows system directory for the dialog interface to function.
The Common Dialog functions provide a standard set of Windows dialog boxes for operations such as
opening, saving, and printing files, or selecting colors and fonts. The class creates objects that, with the
exception of an object instantiation line, are code-compatible with Visual Basic's Common Dialog control.
= COMDLG32.OCX.
Programs using the Visual Basic Common Dialog control require that COMDLG32.OCX be available and
correctly registered. (Note this is the control and NOT the Dll, which all Windows systems have)
If the Common Dialog class is included as a class module in a Visual Basic project, no other files are
required. If the class is compiled as an in-process server, the resulting DLL will need to be available at run
The Win32 Common Dialog object is easier to use than the Visual Basic Common Dialog control because
it doesn't need to be a part of a form, so there is less over head ie you don't need to have the users
ensure that they have this control. The Common Dialog object (DLL) provides the same methods and
properties as the Common Dialog control (OCX) and it can be used to replace the Common Dialog
control in existing projects with very little modification to code.
Here is a list of the API functions available in the Dll. Note the double Entry points for ANSI or UNICODE
are designated by the last letter A (ANSI) or W(Wide character = UNICODE)
These functions were extracted via the "UsesWhat" utility available HERE
This utility scans import tables of Win32 executables for references to libraries and/or APIs.
- Ssync_ANSI_UNICODE_Struct_For_WOW
For VBA users you will note from above the GetOpenFileName & GetSaveFileName,
do these look familiar ?
Yes it is basically the same. Here is the help file info;
Syntax
expression.GetOpenFilename(FileFilter, FilterIndex, Title, ButtonText, MultiSelect)
expression Required. An expression that returns an Application object.
FileFilter Optional Variant. A string specifying file filtering criteria.
This string consists of pairs of file filter strings followed by the MS-DOS wildcard file filter specification,
with each part and each pair separated by commas. Each separate pair is listed in the Files of type
drop-down list box.
For example, the following string specifies two file filters—text and addin:
"Text Files (*.txt),*.txt,Add-In Files (*.xla),*.xla".
To use multiple MS-DOS wildcard expressions for a single file filter type, separate the wildcard expressions with semicolons; for example, "Visual Basic Files (*.bas; *.txt),*.bas;*.txt".
If omitted, this argument defaults to "All Files (*.*),*.*".
FilterIndex Optional Variant. Specifies the index numbers of the default file filtering criteria, from 1 to the
number of filters specified in FileFilter. If this argument is omitted or greater
than the number of filters present, the first file filter is used.
Title Optional Variant. Specifies the title of the dialog box. If this argument is omitted,
the title is "Open."
ButtonText Optional Variant. Macintosh only.
MultiSelect Optional Variant. True to allow multiple file names to be selected.
False to allow only one file name to be selected. The default value is False
Remarks
This method returns the selected file name or the name entered by the user. The returned name may
include a path specification. If MultiSelect is True, the return value is an array of the selected file names
(even if only one filename is selected). Returns False if the user cancels the dialog box.
This method may change the current drive or folder.
Now that we have this out of the way lets look @ why I brought this up.
Using VBA GetOpenFileName, you may wish to filter for certain files eg *.txt = ALL Txt Files, but what
about All Txt files starting with B. You would naturally assume that you can do this via wild card subst
eg B*.txt, or what about ALL Txt files with the letters BE in the file name ??.
Then we would put in *BE*.Txt. Try this and see what happens ?
That's right, it won't work, you are limited with VBA FileOpen function filtering.
FILTERING FILES
Here is a work around for this - using the comdlg32.dll and the API function GetOpenFileNameA.
Private Declare Function GetOpenFileName Lib "comdlg32.dll" _
Alias "GetOpenFileNameA" ( _
pOpenfilename As OPENFILENAME) As Long
Getting started
Before we use this API function we need to define a Type structure for the pOpenfilename.
As this function is similar to the OCX control, then it's properties should be the same, and they are
except for a few minor details. Here are the properties;