【问题标题】:checking if a file exists in vb [duplicate]检查vb中是否存在文件[重复]
【发布时间】:2019-08-05 01:48:23
【问题描述】:

我正在尝试运行宏来检查文件是否存在。我收到Sub or function not defined 的编译错误。有人可以帮忙吗

If FileExists(filepath) Then

Else
  Call Master
  Call PrinttoPDF
End If

【问题讨论】:

    标签: excel vba


    【解决方案1】:

    尝试以下子。

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

    【讨论】:

    • 嗨。实际上,变量 FilePath 是文件的名称及其完整路径。即使文件存在于路径位置,建议的更改仍在运行 else 部分。你有什么进一步的改变建议吗?
    • 我已经测试了代码并且运行良好。请仔细检查您的文件路径。你能把完整的代码放在这里看看吗。
    【解决方案2】:

    我不是 VBA 专家,但它看起来像 FileExistsMasterPrinttoPDF 不作为 Sub 或 Function 存在。也许换个Case,最后一个应该是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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-11-06
      • 1970-01-01
      • 1970-01-01
      • 2017-01-02
      • 1970-01-01
      • 1970-01-01
      • 2012-01-25
      相关资源
      最近更新 更多