以下是判断指定的工作簿是否打开的代码 On Error Resume Next Set wbk = Workbooks("e:\asd.xls") If Err.Number <> 0 Then MsgBox "没有打开" Else MsgBox "打开" '在此处加关闭("e:\asd.xls")的代码 End If
Err.Clear
Private Sub Command1_Click()
On Error Resume Next '让错误继续下一步
'引用 已打开的 Excel 对象
Set excel1 = GetObject(, "Excel.Application")'Excel 对象
If Err.Number <> 0 Then'没打开 Err.Number = 0 Set excel1 = GetObject(, "ET.Application") 'wps 表格 V8 DLL If Err.Number <> 0 Then Err.Number = 0 Set excel1 = GetObject(, "Ket.Application")'wps表格 V9 DLL If Err.Number <> 0 Then '最终的错误 MsgBox "没有运行Excel 或 WPS 表格(Et)。运行后重新刷新。", vbCritical, "错误" Exit Sub End If End If
End If
Call rework
End Sub
Private Sub Command2_Click()
excel1.workbooks(List1.ListIndex + 1).Close'关闭工作簿
Call rework
End Sub
Private Sub rework()
Dim i As Long
List1.Clear '导入所有的工作簿 With excel1 For i = 1 To .workbooks.Count List1.AddItem .workbooks(i).Name Next i End With
End Sub