【问题标题】:How do i check whether a File exists using VBA Macro in Powerpoint如何在 Powerpoint 中使用 VBA 宏检查文件是否存在
【发布时间】:2017-02-21 07:44:37
【问题描述】:

我想在运行我的代码之前检查一个文件是否已经存在。如果它存在而不是退出,否则保持我的代码运行。我写的是以下代码:

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          

【问题讨论】:

  • 下次试试google。这已经被问了一百次了——这只是今年。 :) 1 2 3 4
  • CommandButton21... 考虑命名的事情。从现在起六个月后,CommandButton42 将更容易分辨 TimestampAndSaveButton(或其他)。

标签: vba powerpoint


【解决方案1】:

如果你想检查本地机器上是否存在文件,你想使用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

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

【讨论】:

  • 不错。我会将它添加到我的 VBA 库中。
  • 赞成。 FileSystemObject 对于其他功能也非常有用。熟悉它会很有帮助。 :)
  • 更进一步,可以写成IF CreateObject("Scripting.FileSystemObject").FileExists("C:\Users\Moez\Desktop\Macro_Project\Test1.pptm") THEN(最好用在单行函数中返回TRUE/FALSE)。
  • @RyanWildry 我刚刚做了,你是对的......非常有用。还有什么我可以看的那种吗?
  • 脚本运行时中还有字典数据结构。非常有用。见:stackoverflow.com/documentation/vba/3667/…
【解决方案2】:

这是我见过的最好的方式:

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

这里的信用:

Check if the file exists using VBA

【讨论】:

  • 和我的方法一样:-)
  • 这实际上是在 Powerpoint 中,而不是在 Excel 中。我试过了,还是不行!
  • @Zigouma 立即尝试
  • 如果您编写必须在 PC 和 Mac 上工作的 VBA,这是唯一的方法,但 FSO 方法对于仅 PC 的开发人员来说更强大。
【解决方案3】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-07-19
    • 2013-04-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-10
    • 1970-01-01
    相关资源
    最近更新 更多