学习型 ASP/PHP/ASP.NET 主机 30元/年全能 ASP/PHP/ASP.NET 主机,支持月付专业 MSSQL 数据库空间,支持月付专业 MySQL 数据库空间,支持月付
轻松建立自己的群组,招兵买马   
发新话题
打印

急求VB编程进行数据采集保存在EXCEL2003中的问题

急求VB编程进行数据采集保存在EXCEL2003中的问题

请问如何在一个程序中多次调用同一个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) >= 1 Then
[u] exApp.Workbooks.Open CommonDialog1.FileName[/u]   出错处   
  Range(Cells(k, 6), Cells(k, 6)) = k
   Range(Cells(k, 7), Cells(k, 7)) = k
   Range(Cells(k, 8), Cells(k, 8)) = k
   Range(Cells(k, 9), Cells(k, 9)) = k
    exApp.ActiveWorkbook.Save
    exApp.ActiveWorkbook.Close
      End If
   exApp.Quit
   Set exApp = Nothing
End Sub
但是一进入Private Sub Timer1_Timer()这个里面运行到 exApp.Workbooks.Open CommonDialog1.FileName时就显示
实时错误'91',对象变量或With快变量未设置。

[ 本帖最后由 crzzx 于 2008-7-8 20:57 编辑 ]

[ 本帖最后由 crzzx 于 2008-7-8 20:58 编辑 ]

TOP

Set exApp = Nothing

第一次timer的时候exApp都成Nothing了,下一次哪里来的Workbooks?
请不要在论坛上提课本、教程上讲烂掉的问题。请不要没打好程序设计基础就好高骛远。
请不要将纯HTML、JS、SQL的问题发表到ASP版面。祝大家工作愉快。

TOP

谢谢,明白了!

TOP

发新话题