如何在 Powerpoint 中使用 VBA 宏检查文件是否存在

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39999739/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 11:23:17  来源:igfitidea点击:

How do i check whether a File exists using VBA Macro in Powerpoint

vbapowerpointpowerpoint-vba

提问by Zigouma

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          

回答by Dave

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

如果要检查本地计算机上是否存在文件,则要使用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.

如果您需要任何进一步的解释,请告诉我。

回答by User632716

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

使用 VBA 检查文件是否存在

回答by rohrl77

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

这是我检查某些东西是否存在的版本。包括一个测试子。这应该适用于任何 VBA 环境,包括 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