【问题标题】:to check if csv file is open from vba检查 csv 文件是否从 vba 打开
【发布时间】:2015-07-14 08:15:46
【问题描述】:

我想导出表以访问 csv 文件。如果我要导出表的 csv 文件已关闭,它工作正常。但是,如果文件已打开,则不会出现错误,也不会导出表。 有什么方法可以检查 csv 文件是否已经打开并在可能的情况下关闭它??

【问题讨论】:

    标签: csv file ms-access vba export


    【解决方案1】:

    在这里找到了解决方案。 用于检查文件或文档是否打开的 VBA 函数https://support.microsoft.com/en-us/kb/209189

    【讨论】:

      【解决方案2】:
      Sub MacroName()
         Dim Path As String
         Path = "C:\test.doc"
         If Not FileLocked(Path) Then
            Documents.Open strFileName
         End If
      End Sub
      
      Function FileLocked(Path As String) As Boolean
         On Error Resume Next
            Open strFileName For Binary Access Read Write Lock Read Write As #1
         Close #1
            If Err.Number <> 0 Then
               MsgBox "Error #" & Str(Err.Number) & " - " & Err.Description
            FileLocked = True
            Err.Clear
         End If
      End Function
      

      【讨论】:

        【解决方案3】:

        this microsoft support page 中所述,您可以检查文件是否为只读:

        Sub Example1()
        
            ' Test to see if the Read-only attribute was assigned to the file.
        
            If GetAttr("c:\example.csv") And vbReadOnly Then
                MsgBox "File is Read-only"
            Else
                MsgBox "File is not read-only"
            End If
        
        End Sub
        

        如果你打开了那个文件,你可以随时关闭你打开的任何文件。

        intFile = FreeFile()
        Open "c:\example.csv" For Output As #intFile
        Write #intFile
        Close #intFile
        

        如果您没有在Close 语句中指定文件名,您将关闭您打开的所有文件。

        如果文件被其他应用打开,不知道有没有办法关闭。

        【讨论】:

        • 这不会让我知道 csv 文件是否打开。
        猜你喜欢
        • 1970-01-01
        • 2019-03-30
        • 2022-11-22
        • 2019-04-29
        • 2015-08-02
        • 1970-01-01
        • 1970-01-01
        • 2013-06-23
        • 1970-01-01
        相关资源
        最近更新 更多