问题
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:
Private Sub CommandButton21_Click()
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
End Sub
回答1:
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.
回答2:
This is the best way I've seen:
Sub test()
thesentence = InputBox("Type the filename with full extension", "Raw Data File")
If Dir(thesentence) <> "" Then
MsgBox "File exists."
Else
MsgBox "File doesn't exist."
End If
End Sub
Credit here:
Check if the file exists using VBA
回答3:
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
来源:https://stackoverflow.com/questions/39999739/how-do-i-check-whether-a-file-exists-using-vba-macro-in-powerpoint