Public Function UpdateExcel()
Dim xlApp As New Excel.Application, xlBook As Excel.Workbook
    Dim xlsheet As Excel.Worksheet
    Dim strFileName As String        xlsheet.Columns("A:A").Select
        Selection.ColumnWidth = 15
        xlsheet.Columns("B:B").Select
        Selection.ColumnWidth = 13.25
        xlsheet.Columns("C:C").Select
        Selection.ColumnWidth = 13.25
        
        xlsheet.Range("C1445:C1447").Select
        With Selection.Borders
            .LineStyle = xlContinuous
            .Weight = xlThin
            .ColorIndex = xlAutomatic
        End With
        
        xlsheet.Range("D1444:G1448").Select
        With Selection.Borders
            .LineStyle = xlContinuous
            .Weight = xlThin
            .ColorIndex = xlAutomatic
        End With
        
        xlsheet.Range("A3:A1442").Select
        With Selection
            .HorizontalAlignment = xlRight
        End With
        xlsheet.Range("A1").Select            xlBook.Save
            xlBook.Close False
            xlApp.Quit
            Set xlApp = NothingEnd Function
这个函数多次调用时,第一次是成功的,可是从第二次调用开始就报object或者with共同体变量没有设定,不知是什么原因,请各位帮忙解决。

解决方案 »

  1.   

    xlApp.Quit
                Set xlApp = Nothing
    excel用上面语句退出有时会不成功,就是还有一个进程存在,不知会不会有影响。
      

  2.   

    这个问题好像是bug,在word里同样的方法就可以关,但excel有时会关不掉,你也用getobject函数看有没有excel进程,关不掉就只能用结束进程的api了,不过官方不推荐用这种方法。
      

  3.   

    ExlBook.Close
        ExlApp.Quit
        Set ExlSheet = Nothing
        Set ExlBook = Nothing
        Set ExlApp = Nothing