
Tips.Net > WordTips Home > Macros > WordBasic Examples > Determining if a File Exists with WordBasic
Summary: Macros can easily work with files. Here's WordBasic code that lets you check to see if a file exists at all. (This tip works with Microsoft Word 6, and Word 95.)
As you are programming your macros, you may have a need to determine if a particular file exists on disk. For instance, the purpose of your macro may be to open and read from a text file. Before doing so, you will want to check to see if the file exists, in order to avoid an error.
The following WordBasic function can be used to check for the existence of a file. All you need to do is pass it the full filename as a string, and the macro returns either True (if the file exists) or False (if it doesn't).
Function FileThere(FileNm$)
FileThere = (Files$(FileNm$) > "")
End Function
You can use the function similar to the following:
If FileThere("c:\myfile.txt") Then
'
' Do stuff here
'
End If
Tip #818 applies to Microsoft Word versions: 6 95
Save Time! You can have this tip (and several hundred just like it) in the WordTips annual archives. Imagine having over 400 tips available at your fingertips, in each annual volume.
Learn to search for and replace text, formatting, graphics, and special characters the easy way with WordTips: Find and Replace. (more information...)
Ask a Word Question
Make a Comment
Beauty Tips
Car Tips
Cleaning Tips
College Tips
Cooking Tips
Excel2007 Tips
ExcelTips
Family Tips
Gardening Tips
Health Tips
Home Tips
Money Tips
Pet Tips
Word2007 Tips
WordTips