WD: Macro to Determine If a File Exists on DiskLast reviewed: February 2, 1998Article ID: Q88866 |
The information in this article applies to:
SUMMARYThe 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 ApplicationsMicrosoft 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 SubWord 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 SubNOTE: 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 SubWARNING: 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
© 1998 Microsoft Corporation. All rights reserved. Terms of Use. |