#2
约定的童话2023-09-09 16:00
|
如下,判断指定的excel文件是否打开,如果打开,则关闭。但关闭时出错”下标越界“,请大侠帮忙解决一下,谢谢。
Sub test()
Dim f As String
f = "C:\Users\Administrator\Desktop\test.xlsx"
bl = IsFileOpen(f)
If bl Then
MsgBox "打开"
Excel.Workbooks("test.xlsx").Close False
Else
MsgBox "没有打开"
End If
end sub
Function IsFileOpen(filename As String) As Boolean
Dim filenum As Integer, errnum As Integer
On Error Resume Next
filenum = FreeFile()
Open filename For Input Lock Read As #filenum
Close filenum
errnum = Err
On Error GoTo 0
Select Case errnum
Case 0: IsFileOpen = False
Case 70: IsFileOpen = True
Case Else: Error errnum
End Select
End Function