Dir function does nothing

vincentchrisnata

New Member
Joined
Feb 10, 2022
Messages
5
Office Version
  1. 365
  2. 2013
Platform
  1. Windows
Hello,
I have a problem on the dir function, as i run the code, it does nothing. I want to prompt a message asking to overwrite file if the file already exists in the directory.

Thanks before.

VBA Code:
Sub SavePDF_Cost()
    Dim DocPath As String
    Dim DefName As String
    Dim EmpID As String
    Dim DocName As String
    
    DocPath = "X:\Cost"
    DefName = "Cost."
    EmpID = Range("A2").Value
    DocName = DefName & EmpID

    If Dir(DocPath & DocName) <> vbNullString Then
        If MsgBox(DocName & " already exists!" & vbNewLine & "Do you want to overwrite it?", vbExclamation + vbYesNo) = vbYes Then
            ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=DocPath & DocName, Quality:=xlQualityStandard, _
                IncludeDocProperties:=False, IgnorePrintAreas:=False, OpenAfterPublish:=True
            MsgBox "The File has been saved!"
        End If
    End If
End Sub
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
think you need another "\" at the end of your DocPath statement
 
Upvote 0
think you need another "\" at the end of your DocPath statement

I added "\", but still does nothing

VBA Code:
Sub SavePDF_Cost()
    Dim DocPath As String
    Dim DefName As String
    Dim EmpID As String
    Dim DocName As String
    
    DocPath = "X:\Cost\"
    DefName = "Cost."
    EmpID = Range("A2").Value
    DocName = DefName & EmpID

    If Dir(DocPath & DocName) <> vbNullString Then
        If MsgBox(DocName & " already exists!" & vbNewLine & "Do you want to overwrite it?", vbExclamation + vbYesNo) = vbYes Then
            ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=DocPath & DocName, Quality:=xlQualityStandard, _
                IncludeDocProperties:=False, IgnorePrintAreas:=False, OpenAfterPublish:=True
            MsgBox "The File has been saved!"
        End If
    End If
End Sub
 
Upvote 0
Ive tested this twice with the additional "\"and it works fine. The only issue that it can be is where you reference range("A2"). What is the info stored in range("A2")
 
Upvote 0
Ive tested this twice with the additional "\"and it works fine. The only issue that it can be is where you reference range("A2"). What is the info stored in range("A2")
If Dir(DocPath & DocName) <> vbNullString Then

When i change "<>" to "=", the code can run. Maybe is it because it can't compare them?
 
Upvote 0
If Dir(DocPath & DocName) <> vbNullString Then

When i change "<>" to "=", the code can run. Maybe is it because it can't compare them?
You haven't answered what is in your cell a2.
In your first post the code finds existing files perfectly so the only reason changing <> to = would work if the file didn't exist in the first place
 
Upvote 0
M
You haven't answered what is in your cell a2.
In your first post the code finds existing files perfectly so the only reason changing <> to = would work if the file didn't exist in the first place

Cell A2 is correct, the employee ID
I tried to debug.print If Dir(DocPath & DocName) <> vbNullString . It always returns FALSE either the file exists or not
 
Upvote 0
Do you have a file extension on the end of docname?
 
Upvote 0
Yes actually I just found the solution as you mentioned
I added the file extensions ".pdf"

DocName = DefName & EmpID & ".pdf"

Thanks gordsky and RoryA for the responses :)
Glad you solved it, thats why i asked in post#4 what was in A2. If it wasnt a file ext it was never going to find the file
 
Upvote 0

Forum statistics

Threads
1,215,064
Messages
6,122,936
Members
449,094
Latest member
teemeren

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top