2017-04-04 201 views
-1

有沒有辦法來檢查這個(例如test.xlsm)文件是否打開?Visual Basic檢查文件是否打開|重複關閉

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。

我希望有更好的解決方案的任何建議。

+0

我不想有一個文件路徑。 當我沒有路徑時,你有解決方案嗎? @ dot.Py – Z3RP

+1

我們無法看到您的IsFileOpen函數,實際上很重要。請張貼代碼。 – vacip

+0

我認爲這是我不善於編碼的問題,我認爲功能缺失,我認爲這是功能。 我從這個網站得到:https://msdn.microsoft.com/de-de/library/aa300915%28v=vs.71%29.aspx @vacip – Z3RP

回答

0

您定義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 
+0

是的,我gto錯誤(在德國):Argumenttyp ByRefunverträglich – Z3RP

+0

你是如何實現它並調用函數的?因爲我只是在這裏測試了這個代碼,它工作! –

0

一個簡單的邏輯可以用如下檢查文件是否打開...

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