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 k = 1 To 5
    Range(Cells(k, 1), Cells(k, 1)) = "aa"
    Range(Cells(k, 7), Cells(k, 7)) = "aa"
    Range(Cells(k, 8), Cells(k, 8)) = "aa"
    Range(Cells(k, 9), Cells(k, 9)) = "aa"
     Next
    exApp.ActiveWorkbook.Save
    exApp.ActiveWorkbook.Close
      End If
   exApp.Quit
   Set exApp = Nothing
   Timer1.Enabled = True
   Timer1.Interval = 3000
End SubPrivate Sub Command2_Click()
End
End SubPrivate Sub Form_Load()
Set exApp = New Excel.Application
End SubPrivate Sub Timer1_Timer()
    k = k + 1
    CommonDialog1.ShowOpen
If Len(CommonDialog1.FileName) >= 1 Then
    exApp.Workbooks.Open CommonDialog1.FileName
   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()这个里面时,再次打开CommonDialog1.FileName就出现实时错误'91',对象变量或 With 块变量没有设置(错误 91),小弟不才还请各位高手指点!

解决方案 »

  1.   

    代码我帮你改了一下.
    改为这样:Dim exApp  As Excel.Application
    Dim i, k As Integer
    Private Sub Command1_Click()
        Set exApp = Nothing                      '我增加的
        Set exApp = New Excel.Application        '我增加的
        CommonDialog1.ShowOpen
        If Len(CommonDialog1.FileName) >= 1 Then
        If Not IIf(Dir(FileName) <> "", True, False) Then Exit Sub  '我增加的
        exApp.Workbooks.Open CommonDialog1.FileName
        For k = 1 To 5
        Range(Cells(k, 1), Cells(k, 1)) = "aa"
        Range(Cells(k, 7), Cells(k, 7)) = "aa"
        Range(Cells(k, 8), Cells(k, 8)) = "aa"
        Range(Cells(k, 9), Cells(k, 9)) = "aa"
        Next
        exApp.ActiveWorkbook.Save
        exApp.ActiveWorkbook.Close
          End If
      exApp.Quit
      Set exApp = Nothing
      Timer1.Enabled = True
      Timer1.Interval = 3000
    End SubPrivate Sub Command2_Click()
    End
    End SubPrivate Sub Form_Load()
    Set exApp = New Excel.Application
    End SubPrivate Sub Timer1_Timer()
        Set exApp = Nothing                         '我增加的
        Set exApp = New Excel.Application            '我增加的
        k = k + 1
        CommonDialog1.ShowOpen
    If Len(CommonDialog1.FileName) >= 1 Then
      If Not IIf(Dir(FileName) <> "", True, False) Then Exit Sub  '我增加的
      exApp.Workbooks.Open CommonDialog1.FileName
      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
      

  2.   

    谢谢你的帮忙1
    我刚才运行了一下,出了点问题
    If Not IIf(Dir(FileName) <> "", True, False) Then Exit Sub  '我增加的    说此处类型不匹配
    能帮我再看下吗?
      

  3.   

    在过程前面增加on error resume next或on error goto 语句另外,最好在所有range和cells函数前面加上定语,如exapp.activeworkbook.sheet1.range(...)
      

  4.   

    将If Not IIf(Dir(FileName) <> "", True, False) Then Exit Sub
    改为 If Not IIf(Dir(CommonDialog1.FileName) <> "", True, False) Then Exit Sub