打開了一個execel空文件,
xlApp.Workbooks.Open FILE_MO(FILE_MO="C:\Ehime\app\VB\印字フォーマット\履歴保存.xls")
然後對這個文件做了一些寫入的操作,當我不想寫入的時候,我要關閉這個execel文件,並且不保存!!
可是進程中始終存在EXECEL.exe的進程,誰知道怎麽才能殺掉啊?各位大蝦幫幫忙,怎麽能關閉他並且不保存!!!!!!!!!!!!!

解决方案 »

  1.   

    xlApp.Quit
    Set xlApp = Nothing
      

  2.   

    还有其他的WorkBooks等对象都要释放掉!
      

  3.   

    在退出的时候清空所有的excel对象
      

  4.   

    這是我的代碼,各位看看還有什麽需要釋放掉的!謝謝大家!!
    Private Sub Command2_Click(Index As Integer)
        Dim j            As Integer
        Dim strFName     As String
        Dim objXLS       As Object
        Dim STRDAT(4999) As Alrm
        Dim MODAT(4999)  As Alrm, MO
        Dim i            As Integer
        Dim DataCount   As Integer  'データ数格納
        Dim iRet        As Integer
        Dim cuWork      As Currency
        Dim TR_Save     As Integer
      
        On Error GoTo DiskErr
      
        Select Case Index
               
               Case 0
                    '待ちメッセージの表示
                    If Existfloppy = False Then
                       Form0005.Label1.Caption = ERR_ALARMDATA_MO_001
                       Form0005.Label1.ForeColor = KRED
                       Form0005.Show 1
                       Exit Sub
                     End If                
                      
                   strFName = LLMODRIVER_SAVETREND & "履歴" & Format(Date, "yyyy") & "_" & Format(Date, "mm") & "_" & Format(Date, "dd") & ".xls"'               Set xlApp = CreateObject("Excel.Sheet.8")               'sheet名設定
    '               xlApp.Worksheets(1).name = "全履歴データ"                '/** Excelをセット **/
                    Set xlApp = Nothing
                    Set xlApp = New Excel.Application
                    If FileExists(FILE_MO) = True Then
                    Else
                        '履歴保存が存在しません
                        Form0005.Label1.Caption = FILE_MO_NO_EXIST
                        Form0005.Label1.ForeColor = KRED
                        Form0005.Show 1
                        Exit Sub
                
                    End If
                    
                    Call DispWaitMsg(True)
                    
                    '/** 印字フォーマットを選択しEXCELで起動 **/
                    xlApp.Workbooks.Open FILE_MO
                    
                    'データ設定
                    For i = 0 To DCount - 1
                         DoEvents
                         If gCancel = True Then
                            Exit For
                         End If
                         xlApp.Cells(i + 5, 1).Value = "'" & RTrim(DSPDAT(4999 - DCount + 1 + i).Data_Date)
                         xlApp.Cells(i + 5, 2).Value = "'" & RTrim(DSPDAT(4999 - DCount + 1 + i).Data_Time)
                         Select Case DSPDAT(4999 - DCount + 1 + i).Data_Gamen
                           
                            Case 1
                                xlApp.Cells(i + 5, 3).Value = "機器状態変化"
                            Case 2
                                xlApp.Cells(i + 5, 3).Value = "故障警報"
                            Case 3
                                xlApp.Cells(i + 5, 3).Value = "制御操作"
                            Case 4
                                xlApp.Cells(i + 5, 3).Value = "上下限YITUO"
                            Case 5
                                xlApp.Cells(i + 5, 3).Value = "設定値変更"
                            Case 6
                                xlApp.Cells(i + 5, 3).Value = "システム状態変化"
                         End Select
                         xlApp.Cells(i + 5, 4).Value = "'" & RTrim(DSPDAT(4999 - DCount + 1 + i).Data_Basyo)
                         xlApp.Cells(i + 5, 5).Value = "'" & RTrim(DSPDAT(4999 - DCount + 1 + i).Data_Mishou)
                         xlApp.Cells(i + 5, 6).Value = "'" & RTrim(DSPDAT(4999 - DCount + 1 + i).Data_HHLL)
                         xlApp.Cells(i + 5, 7).Value = "'" & RTrim(DSPDAT(4999 - DCount + 1 + i).Data_Teigi)
                         xlApp.Cells(i + 5, 8).Value = "'" & RTrim(DSPDAT(4999 - DCount + 1 + i).Data_Dousa)
                    Next i                If gCancel = True Then   '在這個if語句里釋放execel
                        xlApp.Quit
                        Set xlApp = Nothing
                                   
                        
                       
                    Else
                        ' EXCEL文档保存
                         xlApp.Workbooks(1).Saved = True
                         xlApp.Workbooks(1).SaveAs strFName
                         xlApp.Application.Quit
                         Set xlApp = Nothing
                    End If
                   
                   
                   
                Case 1
        End Select
        '終了
        Unload Me
       
        Exit Sub
    DiskErr:
        If Err.Number = 1004 Then
            Form0005.Label1.Caption = ERR_ALARMDATA_MO_002
            Form0005.Label1.ForeColor = KRED
            Form0005.Label2.Caption = ERR_ALARMDATA_MO_003
            Form0005.Label2.ForeColor = KRED
            Form0005.Show 1
            Unload Form0007
        Else
            Form0005.Label1.Caption = ERR_ALARMDATA_MO_004
            Form0005.Label1.ForeColor = KRED
            Form0005.Show 1
            Unload Form0007
        End IfEnd Sub
      

  5.   

    If gCancel = True Then   '在這個if語句里釋放execel
            xlApp.Quit
            Set xlApp = Nothin              ==============================================
        以上部分在QUIT之前最好将当前的Workbook置为已存储状态(实际并不保存)这样可以避免EXECEL进程退出失败。改为这样或许就可以了:
     If gCancel = True Then   '在這個if語句里釋放execel
        xlApp.ActiveWorkbook.saved=True
            xlApp.Quit
            Set xlApp = Nothin