I am trying to check if a .pdf exists before attaching it to an e-mail.
The location and name of the .pdf will be different each time so I can't hard code it.
Code below has been simplified for illustration purpose.
THIS WORKS
Code:
If [COLOR=blue][B]Dir(C:\Some\File\Path\my.pdf)[/B] [/COLOR]<> "" Then
MsgBox "FILE FOUND"
Else
MsgBox "## error - File does not exist ##"
End If
Code:
[B][COLOR=red]myfile = "C:\Some\File\Path\my.pdf"[/COLOR][/B]
If [B][COLOR=blue]Dir(myfile)[/COLOR][/B] <> "" Then
MsgBox "FILE FOUND"
Else
MsgBox "## error - File does not exist ##"
End If
THIS DOESN'T WORK
Code:
Path = ActiveWorkbook.Path
Name = ActiveCell.Value
[B][COLOR=red]myfile = Path & Name[/COLOR][/B]
If[COLOR=blue] [B]Dir(myfile)[/B][/COLOR] <> "" Then
MsgBox "FILE FOUND"
Else
MsgBox "## error - File does not exist ##"
End If
Stepping through the code shows that Dir(myfile) has the same value as the one that does work.
Dim myfile As String has no effect.
Given that Dir(myfile) has the exact same value in the one that doesn't work and the one that does work.
I don't know why it isn't working.
Vulcan.
HOW I HAVE TESTED IT
Code:
Path = ActiveWorkbook.Path
Name = ActiveCell.Value
[B][COLOR=red]myfile = Path & Name[/COLOR][/B]
[B][COLOR=red]myfile = "C:\Some\File\Path\my.pdf"[/COLOR][/B]
If[COLOR=blue] [B]Dir(myfile)[/B][/COLOR] <> "" Then
MsgBox "FILE FOUND" _
& Chr(10) _
& [B][COLOR=red]myfile [/COLOR][/B] _
& Chr(10) _
& [B][COLOR=red]Path & Name[/COLOR][/B]
Else
MsgBox "## error - File does not exist ##"
End If
FILE FOUND
"C:\Some\File\Path\my.pdf"
"C:\Some\File\Path\my.pdf"