【问题标题】:Visual Basic check if the file is open | duplicate closed [duplicate]Visual Basic 检查文件是否打开 |重复关闭[重复]
【发布时间】:2017-08-29 13:21:11
【问题描述】:

有没有办法检查这个(例如 test.xlsm)文件是否打开?

Dim v_datenwb As String
v_datenwb = "test.xlsm"
Dim err As String
err = " ist nicht geöffnet!"
Dim op As Integer
op = 0

Do While op <> 1
If IsFileOpen(v_datenweb) = True Then
Workbooks(v_datenwb).Activate
op = 1
Else
If vbCancel = MsgBox(v_datenwb & err, vbRetryCancel, "Error") Then
    Exit Sub
End If
err = " ist nicht geöffnet! Sind sie sich sicher dass das Dokument offen ist ?"
End If
Loop

Public Function IsFileOpen( _
ByVal FileName As String, _
Optional ByVal ViewKind As String ="{00000000-0000-0000-0000-000000000000}" _
) As Boolean

我尝试了类似的东西,但没有奏效。 每次程序检查文件是否打开时,结果都是 False。

如果有任何更好的解决方案建议,我将不胜感激。

【问题讨论】:

  • 我不想有文件路径。当我没有路径时,您有解决方案吗? @dot.Py
  • 我们看不到您的 IsFileOpen 函数,这是真正重要的函数。请同时发布该代码。
  • 我认为这是我不擅长编码的问题,我认为缺少该功能我认为这是该功能。我从这个网站得到它:msdn.microsoft.com/de-de/library/aa300915%28v=vs.71%29.aspx@vacip

标签: excel vba


【解决方案1】:

可以使用如下简单的逻辑来检查文件是否打开...

Function IsFileOpen(fileName As String) As Boolean
Dim wb As Workbook
On Error Resume Next
Set wb = Workbooks(fileName)
On Error GoTo 0
If Not wb Is Nothing Then IsFileOpen = True
End Function

Sub Test()
Dim fName As String
fName = "test.xlsm"
If IsFileOpen(fName) Then
    MsgBox "File is open.", vbExclamation
Else
    MsgBox "File is not open.", vbExclamation
End If
End Sub

【讨论】:

    【解决方案2】:

    您定义了v_datenwb = "test.xlsm",但您在If IsFileOpen(v_datenweb) = True Then 调用了v_datenweb


    你试过this solution吗?

    您只需将c:\Book2.xls 更改为您想要的工作簿。

    此代码有一个名为IsFileOpen 的函数和一个名为TestFileOpened 的宏调用该函数。 子TestFileOpened()

        ' Test to see if the file is open.
        If IsFileOpen("c:\Book2.xls") Then
            ' Display a message stating the file in use.
            MsgBox "File already in use!"
            '
            ' Add code here to handle case where file is open by another
            ' user.
            '
        Else
            ' Display a message stating the file is not in use.
            MsgBox "File not in use!"
            ' Open the file in Excel.
            Workbooks.Open "c:\Book2.xls"
            '
            ' Add code here to handle case where file is NOT open by another
            ' user.
            '
        End If
    
    End Sub
    
    ' This function checks to see if a file is open or not. If the file is
    ' already open, it returns True. If the file is not open, it returns
    ' False. Otherwise, a run-time error occurs because there is
    ' some other problem accessing the file.
    
    Function IsFileOpen(filename As String)
        Dim filenum As Integer, errnum As Integer
    
        On Error Resume Next   ' Turn error checking off.
        filenum = FreeFile()   ' Get a free file number.
        ' Attempt to open the file and lock it.
        Open filename For Input Lock Read As #filenum
        Close filenum          ' Close the file.
        errnum = Err           ' Save the error number that occurred.
        On Error GoTo 0        ' Turn error checking back on.
    
        ' Check to see which error occurred.
        Select Case errnum
    
            ' No error occurred.
            ' File is NOT already open by another user.
            Case 0
             IsFileOpen = False
    
            ' Error number for "Permission Denied."
            ' File is already opened by another user.
            Case 70
                IsFileOpen = True
    
            ' Another error occurred.
            Case Else
                Error errnum
        End Select
    
    End Function
    

    【讨论】:

    • 是的,我遇到了错误(德语):Argumenttyp ByRef unverträglich
    • 你是如何实现它并调用函数的?因为我刚刚在这里测试了这段代码,它工作了!
    猜你喜欢
    • 2023-04-10
    • 2015-08-31
    • 2019-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-07
    • 1970-01-01
    相关资源
    最近更新 更多