WD: Macro to Determine If a File Exists on Disk

Last reviewed: February 2, 1998
Article ID: Q88866
The information in this article applies to:
  • Microsoft Word for Windows, versions 1.0, 1.1, 1.1a, 2.0, 2.0a, 2.0a-CD, 2.0b, 2.0c, 6.0, 6.0a, 6.0c
  • Microsoft Word for Windows 95, versions 7.0, 7.0a
  • Microsoft Word 97 for Windows
  • Word for the Macintosh, versions 6.0, 6.0.1, 6.0.1a
  • Microsoft Word 98 Macintosh Edition

SUMMARY

The following macro examples determine if a specified file exists in the current directory.

These macros can help you avoid receiving error messages such as "File not found" and "Document does not exist" when you open or delete specific files using Visual Basic for Applications or WordBasic.

MORE INFORMATION

Visual Basic for Applications

Microsoft provides examples of Visual Basic for Applications procedures for illustration only, without warranty either expressed or implied, including, but not limited to the implied warranties of merchantability and/or fitness for a particular purpose. The Visual Basic procedures in this article are provided 'as is' and Microsoft does not guarantee that they can be used in all situations. While Microsoft support engineers can help explain the functionality of a particular macro, they will not modify these examples to provide added functionality, nor will they help you construct macros to meet your specific needs. If you have limited programming experience, you may want to consult one of the Microsoft Solution Providers. Solution Providers offer a wide range of fee-based services, including creating custom macros. For more information about Microsoft Solution Providers, call Microsoft Customer Information Service at (800) 426-9400.

This example uses the Dir function to check if a certain file exists.

Dir Function Syntax:

   Dir[(pathname[,attributes])]

Word 97 for Windows:

   Sub DoesFileExist()
      Dim MyFile, MyPath, MyName
      ' Returns "WIN.INI" if it exists.
      MyFile = Dir("C:\WINDOWS\WIN.INI")
      If MyFile <> "" Then MsgBox "File Exists."
   End Sub

Word 98 Macintosh Edition:

   Sub DoesFileExist()
      Dim MyFile, MyPath, MyName
      ' Returns "My Document" if it exists.
      MyFile = Dir("HD:MyDocs:My Document")
      If MyFile <> "" Then MsgBox "File Exists."
   End Sub

NOTE: To avoid getting an error, you must specify pathname the first time you call the Dir function.

Dir returns the first file name that matches pathname. When no more file names match, Dir returns a zero-length string (""). Once a zero-length string is returned, you must specify pathname in subsequent calls or an error occurs.

For more information about the Dir Function, while in the Visual Basic for Applications Editor, click the Office Assistant, type "Dir" (without the quotation marks), click Search, and then click to view "Dir Function."

NOTE: If the Assistant is hidden, click the Office Assistant button on the Standard toolbar. If Microsoft Visual Basic Help is not installed on your computer, please see the following article in the Microsoft Knowledge Base:

   ARTICLE-ID: Q120802
   TITLE     : Office: How to Add/Remove a Single Office
               Program or Component

WordBasic

   Macro 1:

   Sub MAIN
      a$ = Files$("TEST.TXT")
      If a$ <> "" Then MsgBox "File Exists", a$
   End Sub

   Note: The Files$ command will return a null string ("") if the
   specified file is not found.

   Macro 2:

   Sub MAIN
      ChDir "C:\WinWord"                    'Change current directory
      a$ = Files$("newmacro.doc")
      If a$ = "NEWMACRO.DOC" Then MsgBox "File Exists", a$
      ' The above filename within the If statement must be uppercase
   End Sub

   Note: Word 6 for the Macintosh users, be sure to include a valid
   Macintosh path.

   For example:

      "Mac HD:Microsoft Word 6:Test Document"

The following macro tests whether the "Test1.doc" file exists in the current directory before using the WordBasic Kill command to delete the file named "Test1.doc".

   Sub MAIN
      a$ = Files$("TEST1.DOC")
      If a$ <> "" Then Kill a$
   End Sub

WARNING: ANY USE BY YOU OF THE CODE PROVIDED IN THIS ARTICLE IS AT YOUR OWN RISK. Microsoft provides this macro code "as is" without warranty of any kind, either express or implied, including but not limited to the implied warranties of merchantability and/or fitness for a particular purpose.

REFERENCES

"Using WordBasic," by WexTech Systems and Microsoft, page 207


Additional query words: word7 winword word95 word6 winword2 files
macword word8 word97
Keywords : kbfield macword macword5 winword kbmacro kbprint
Version : WINDOWS: 1.0, 1.1, 1.1a, 2.0, 2.0a, 2.0a-CD, 2.0b, 2.0c, 6.0, 6.0a, 6.0c, 7.0, 7.0a, 97; MACINTOSH:3.0,3.01,3.02, 4.0, 5.0, 5.1;
Platform : MACINTOSH WINDOWS
Issue type : kbhowto


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: February 2, 1998
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.