2017-04-04 19 views
-1

この(example test.xlsm)ファイルが開いているかどうかを確認する方法はありますか?Visual Basicは、ファイルが開いているかどうかをチェックします。 duplicate closed

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"を定義しましたが、v_datenwebIf IsFileOpen(v_datenweb) = True Thenとしました。


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

はい、私はエラー(ドイツ語):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 
関連する問題