检查vb中是否存在文件[重复]

问题描述 投票:-2回答:2

这个问题在这里已有答案:

我试图运行一个宏来检查文件是否存在。我得到编译Sub or function not defined的错误。请有人帮忙吗

If FileExists(filepath) Then

Else
  Call Master
  Call PrinttoPDF
End If
excel vba
2个回答
2
投票

尝试以下子。

Sub CheckFilePath()
    If Dir(FilePath, vbNormal) <> "" Then
        Call Master
        Call PrinttoPDF
    Else
        MsgBox "File does not exists."
    End If
End Sub

0
投票

我不是VBA大师,但它看起来像FileExistsMasterPrinttoPDF不存在作为Sub或Function。也许改变案例,最后一个应该是PrintToPdf

我希望错误告诉你错误发生在哪一行。

我在this page上找到了一个可以解决的工作示例:

Sub Test_File_Exist_With_Dir()
    Application.ScreenUpdating = False
    Dim FilePath As String
    FilePath = ""
    On Error Resume Next
    FilePath = Dir("C:\Users\DT168\Desktop\Test folder\Book2.xlsx")
    On Error GoTo 0
    If FilePath = "" Then
        MsgBox "File doesn't exist", vbInformation, "Kutools for Excel"
    Else
        MsgBox "File exist", vbInformation, "Kutools for Excel"
    End If
    Application.ScreenUpdating = False
End Sub
© www.soinside.com 2019 - 2024. All rights reserved.