急求VB与Excel问题

来源:百度知道 编辑:UC知道 时间:2024/06/24 14:12:44
请问如何在一个程序中多次调用同一个Excel文件,比如用Timer1控件每隔一定时间把数据保存进入Excel文件里面.下面是我编的一个程序;
Dim exApp As Excel.Application
Dim i, k As Integer
Private Sub Command1_Click()
CommonDialog1.ShowOpen
If Len(CommonDialog1.FileName) >= 1 Then
exApp.Workbooks.Open CommonDialog1.FileName
For i = 1 To 5
Range(Cells(i, 1), Cells(i, 1)) = "aa"
Range(Cells(i, 2), Cells(i, 2)) = "aa"
Range(Cells(i, 3), Cells(i, 3)) = "aa"
Range(Cells(i, 4), Cells(i, 4)) = "aa"
Next
exApp.ActiveWorkbook.Save
exApp.ActiveWorkbook.Close
End If
exApp.Quit
Set exApp = Nothing
Timer1.Enabled = True
Timer1.Interval = 3000
End Sub

Private Sub Form_Load()
Set exApp = New Excel.Application
End Sub

Private Sub Timer1_Timer()
k = k + 1
CommonDialog1.ShowOpen
If Len(CommonDialog1.FileName) &g

exApp.Quit
Set exApp = Nothing
这两句有问题
当timer事件发生时,你已关闭了对象,
第二次时间到了,这个对象就已经不存了.
所以
exApp.Quit
Set exApp = Nothing
要放在窗体关闭的代码里.