This code does not fulfill what you expect:
If Dir(SigString) <> "" Then Signature = GetBoiler(SigString) Else Signature = "" End If
If you pass an empty string ( "" ) or vbNullString to Dir , it will return the name of the first file in the current directory path (the path returned by CurDir$ ). So, if SigString empty, your If condition will evaluate to True , because Dir will return a non-empty string (the name of the first file in the current directory), and GetBoiler will be called. And if SigString empty, the fso.GetFile call will fail.
You must either change your condition to verify that SigString not empty, or use FileSystemObject.FileExists instead of Dir to check for the presence of a file. Dir difficult to use precisely because it does what you do not expect from it. Personally, I would use Scripting.FileSystemObject over Dir because there is no funny business ( FileExists returns True if the file exists, and, well, False if it does not). What's more, FileExists expresses the intent of your code much more clearly than Dir .
Method 1: Make SigString Not SigString First
If SigString <> "" And Dir(SigString) <> "" Then Signature = GetBoiler(SigString) Else Signature = "" End If
Method 2: use the FileSystemObject.FileExists method
Dim fso As Object Set fso = CreateObject("Scripting.FileSystemObject") If fso.FileExists(SigString) Then Signature = GetBoiler(SigString) Else Signature = "" End If
Mike Spross Oct. 15 '08 at 21:14 2008-10-15 21:14
source share