Function IsExcelFileOpen(filePath As String) As Boolean
Dim excel As Variant
Dim wb As Variant
Dim isOpen As Boolean
isOpen = False
On Error Resume Next
Set excel = GetObject(, "Excel.Application") ' 既存のExcelインスタンスを取得
If Not excel Is Nothing Then
Forall book In excel.Workbooks
If book.FullName = filePath Then
isOpen = True
Exit Forall
End If
End Forall
End If
On Error GoTo 0
IsExcelFileOpen = isOpen
End Function
Function IsFileLocked(filePath As String) As Boolean
Dim fileNum As Integer
Dim isLocked As Boolean
fileNum = Freefile()
On Error Resume Next
Open filePath For Binary Access Read Write Lock Read Write As #fileNum
If Err <> 0 Then
isLocked = True
Else
isLocked = False
Close #fileNum
End If
On Error GoTo 0
IsFileLocked = isLocked
End Function