|
Visual Basic (VB6 and VBA) |
|
Copyright 1999-2001 Christopher Greaves. All rights reserved. Home Page and email to [email protected] |
| If in doubt, record a macro and inspect the entrails! |
Visual Basic Library
This page was last updated on Tuesday, December 18, 2001
Public Function blnFileExists(strFile As String) As Boolean ' Procedure : blnFileExists ' Description: This code determines if a file exists. ' Copyright: Chris Greaves Inc. ' Inputs: A possibly empty string representing a possible legal file path and name. ' Returns: A boolean, TRUE if the file is available to the caller. ' Assumes: Nothing ' Side Effects: None. ' Tested: By the calls shown below. blnFileExists = False ' default result is failure Dim strFullName As String ' Build an absolute file name in this variable strFullName = strFile ' If there is no path separator, then there is no path; make one. If InStr(1, strFile, Application.PathSeparator) > 0 Then Else strFullName = Options.DefaultFilePath(wdDocumentsPath) & Application.PathSeparator & strFile End If ' The Filelen will generate an error if the named file does not exist. On Error GoTo Failed blnFileExists = (FileLen(strFile) = FileLen(strFile)) Failed: 'Sub TESTblnFileExists() ' MsgBox blnFileExists("c:\autoexec.bat") ' TRUE Generally you will find this. ' MsgBox blnFileExists("autoexec.bat") ' FALSE Only if your default directory is C:\ ' MsgBox blnFileExists("c:\ausoexec.bat") ' FALSE Mis-spelled the name ' MsgBox blnFileExists("") ' FALSE Empty string ' MsgBox blnFileExists("*&^%$#@!") ' FALSE Illegal file name constructed here. ' MsgBox blnFileExists(32) ' FALSE Used a file handle? ooops! 'End Sub End Function
| We all knew nothing when we started … |
|
Home Page and Contact Information Send email to [email protected]. This page was last updated Monday, November 19, 2001 |