1

I want to check if a file already exist before running my code. If it exists than exit otherwise keep my code running. What I wrote is following code:

If FileFolderExists("C:\Users\Moez\Desktop\Macro_Project\Test1.pptm") Then
    MsgBox "Modification already done!"
Else
    deleteTextBox
    AllBlackAndDate
    LastModifiedDate
    SaveAllPresentations "C:\Users\Moez\Desktop\Macro_Project\Test1.pptm" ' save here
End If
Christopher Oezbek
  • 23,994
  • 6
  • 61
  • 85
Zigouma
  • 45
  • 1
  • 2
  • 10
  • 3
    This has been asked here many times: [1](http://stackoverflow.com/questions/16351249/vba-check-if-file-exists) [2](http://stackoverflow.com/questions/26551757/how-do-i-check-if-a-file-exists-before-i-open-it-via-excel-vba) [3](http://stackoverflow.com/questions/4082539/how-do-i-determine-if-file-exists-using-vba-excel-2007) [4](http://stackoverflow.com/questions/35712855/test-if-file-exists-using-vba-or-excel-without-dir) – vacip Oct 12 '16 at 13:46

3 Answers3

8

If you want to check a file exists on the local machine you want to use a FileSystemObject.

Dim fso : Set fso = CreateObject("Scripting.FileSystemObject")

if fso.FileExists("Your file name and path here") Then
    ' do what you like in the case where the file exists
Else
    ' do whatever you wanted to do where the file doesn't exist
End If

Let me know if you need any further explanation.

Dave
  • 4,328
  • 2
  • 24
  • 33
  • 1
    Nice. I'll add this to my VBA library. – rohrl77 Oct 12 '16 at 13:33
  • 1
    Upvoted. The `FileSystemObject` is very useful for other functions too. It'll be helpful to get acquainted with it. :) – Ryan Wildry Oct 12 '16 at 13:43
  • 1
    Taken to the next level it can be written as `IF CreateObject("Scripting.FileSystemObject").FileExists("C:\Users\Moez\Desktop\Macro_Project\Test1.pptm") THEN` (better to use in a one line function to return TRUE/FALSE). – Darren Bartrup-Cook Oct 12 '16 at 13:45
  • @RyanWildry I just did and you are right... highly useful. Anything else of that kind that I could look at? – rohrl77 Oct 12 '16 at 13:58
  • 1
    Also in the scripting runtime is the Dictionary Data structure. Extremely useful. See: http://stackoverflow.com/documentation/vba/3667/scripting-dictionary-object#t=201610121401569699478 – Ryan Wildry Oct 12 '16 at 14:02
  • Note that early-binding (adding a reference) to the Scripting runtime will give you better runtime performance, and IntelliSense/autocompletion at design time. I don't understand why everybody keeps late-binding that library. – Mathieu Guindon Oct 12 '16 at 14:45
  • I like the one line solution. What happens to the object if it is not "Set" e.g. how to set it to nothing when clearing up? – Jamie Garroch - MVP Oct 13 '16 at 13:49
  • @JamieG If you fail to `Set` it initially, you'll get an `Object Required` error. Once you are finished with it and want to clear it up, you can `Set fso = Nothing` which will release the memory held by the object. – Dave Oct 13 '16 at 15:44
  • I get the second part of that Dave but in the 7th comment above this one, Darren isn't setting a reference to the object so it's that one line use I don't quite understand and how it's possible to release the memory in that case. – Jamie Garroch - MVP Oct 13 '16 at 23:04
  • @rohrl77 what else is in your vba library ? :) trying to constitute myself one, but my projects are all around the place ! – Malcoolm Oct 25 '17 at 14:43
3

This is the best way I've seen is using the Dir function:

file_name = InputBox("Type the filename with full extension", "Raw Data File")

If Dir(file_name) <> "" Then
    MsgBox "File exists."
Else
    MsgBox "File doesn't exist."
End If

Source: Check if the file exists using VBA

Christopher Oezbek
  • 23,994
  • 6
  • 61
  • 85
Preston
  • 7,399
  • 8
  • 54
  • 84
0

Here is my version of checking if something exists. Including a test sub. This should work in any VBA environment, including PowerPoint.

Sub test()
MsgBox (FileFolderExists("C:\Users\Moez\Desktop\Macro_Project\Test1.pptm"))
End Sub

Private Function FileFolderExists(str As String) As Boolean
Dim sCheck As String
sCheck = Dir(str)

If Len(sCheck) > 0 Then
    FileFolderExists = True
Else
    FileFolderExists = False
End If
End Function
rohrl77
  • 3,277
  • 11
  • 47
  • 73