object model) lets you pop up a dialog for selecting a file or a folder, with several dialog type
options. This dialog lets users easily select a file or folder, whose name can then be used in code for
further operations.
There is no longer any reason to use a Callbackfunction or the CommonDialogcontrol for
working with files and folders, so this chapter covers using the FileSystemObjectand the
FileDialogobject for working with files and folders, and components of the ADO object model
and legacy VB statements for working with text files.
Working with Windows Explorer Folders ........................................................................
When you save documents (of any sort) to your computer’s hard drive, you need to specify the
folder (otherwise everything will end up either in your root Documents folder or the current
folder, making it very hard to find specific documents). If you work with Word, Excel, or Outlook
templates, you also need to specify a templates folder, so your code will look in the right place for
your templates. You can get the default user templates folder from the Word File Options dialog,
but again, you probably don’t want to keep all your templates in the root Templates folder.
See the Word Export.accdb sample database’s main menu for command buttons with
code for selecting the Documents and Templates paths.
To work with Windows folders, you have two options: the Office FileDialogobject, or the
FileSystemObject. These two methods are discussed in the following sections.
The sample database for this chapter is Files and Folders.accdb.
The Office FileDialog Object....................................................................................
To allow the maximum amount of user choice, combined with convenience, I like to put one or
two folder selection command buttons on a database’s main menu, for selecting folders that will be
used throughout the database. In the sample database for this chapter, Files and Folders.accdb, for
example, the main menu has a section with two sets of controls for selecting a folder; one has a
command button that pops up a Folder Picker dialog for selecting the Input Documents folder
(used for storing documents to be loaded into Attachment fields or textboxes on forms), and the
other opens an Output Documents Folder Picker for selecting the folder where files saved from
attachments are to be stored. After a folder is selected, its name is displayed in the textbox under
the command button. Figure 9.1 shows a main menu with these options.
NOTENOTE
CROSS-REFCROSS-REF
Part II Writing VBA Code to Exchange Data between Office Components