# Excel VBA Dir() command help

**URL:** <https://forum.kirupa.com/t/excel-vba-dir-command-help/323772>\
**Category:** programming\
**Created:** [July 17, 2011, 12:25pm UTC](https://forum.kirupa.com/t/excel-vba-dir-command-help/323772 "2011-07-17T12:25:15Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![The\_Vulcan](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/the_vulcan/32/145_2.png) [@The\_Vulcan](https://forum.kirupa.com/u/The_Vulcan)\
**Post date:** [July 17, 2011, 12:25pm UTC](https://forum.kirupa.com/t/excel-vba-dir-command-help/323772/1 "2011-07-17T12:25:15Z")

</div>

:hang:

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.

**THIS WORKS**

```auto
If [COLOR=blue]**Dir(C:\Some\File\Path\my.pdf)** [/COLOR]<> "" Then
 
MsgBox "FILE FOUND"

Else

MsgBox "## error - File does not exist ##" 

End If

```

**THIS WORKS**

```auto

**[COLOR=red]myfile = "C:\Some\File\Path\my.pdf"[/COLOR]**

If **[COLOR=blue]Dir(myfile)[/COLOR]** <> "" Then
 
MsgBox "FILE FOUND"

Else

MsgBox "## error - File does not exist ##" 

End If

```

**But as I can’t hard code it, I need to pass the Path and file Name to it.**

**THIS DOESN’T WORK**

```auto
Path = ActiveWorkbook.Path
Name = ActiveCell.Value

**[COLOR=red]myfile = Path & Name[/COLOR]**

If[COLOR=blue] **Dir(myfile)**[/COLOR] <> "" Then
  
MsgBox "FILE FOUND"
 
Else
 
MsgBox "## error - File does not exist ##" 
 
 End If

```

In the version above that doesn’t work.  
Stepping through the code shows that **[COLOR=blue]Dir(myfile)[/COLOR]** has the same value as the one that does work.

Dim myfile As String has no effect.

Given that[COLOR=blue] **Dir(myfile)**[/COLOR] 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.
