This code doesn't do 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
is 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
is empty, the call to fso.GetFile
will fail.
You should either change your condition to check that SigString
isn't empty, or use the FileSystemObject.FileExists
method instead of Dir
for checking if the file exists. Dir
is tricky to use precisely because it does things you might not expect it to do. Personally, I would use Scripting.FileSystemObject
over Dir
because there's no funny business (FileExists
returns True
if the file exists, and, well, False
if it doesn't). What's more, FileExists
expresses the intent of your code much clearly than Dir
.
Method 1: Check that SigString
is non-empty 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