private sub form_load()
Command1.caption="打开"
end sub
Private Sub Command1_Click()
on error resume next
Set Excel=CreateObject("Excel.Application")
if InStr(Command1.caption,"打开")>0 then '打开xls文件
Set A=Excel.Documents.open("D:\1.xls")
Excel.Visible=True
Command1.caption="关闭"
else
'如果文档对象不为空,那么说明打开了文档,关闭它,并销毁文档对象
If Not(A Is Nothing) Then A.Close:Set A=Nothing
'如果Excel application对象不为空,那么说明创建了Excel对象,
'退出它,并销毁对象
If Not(Excel Is Nothing) Then Excel.Quit:Set Excel=Nothing
Command1.caption="打开"
end if
End Sub