2011-09-19 29 views
2

所有活動.xls文件我已經試過與此類似:如何關閉在VB6

Set kitap = CreateObject("Excel.Application") 
If IsXlsOpen() = True Then 
    kitap.Application.Quit 
End If 

..但沒有工作了,所以我neeed找到如何關閉所有Excel文件開始我的計劃之前,在VB6

編輯:全部代碼在這裏:

Dim i As Integer 
Dim kitap As Object 

Dim strcnn As String 
Dim cnn As New ADODB.Connection 
Dim Cmd As New ADODB.Command 
Dim rs As New ADODB.Recordset 

Private Sub Form_Load() 

    strcnn = "myconn" 
    cnn.Open strcnn 
    Cmd.ActiveConnection = cnn 

End Sub 

Public Function dotdate(ByRef elem) As String 
    Dim day, month, year As String 

    year = Right(elem, 4) 
    month = Mid(elem, Len(elem) - 5, 2) 
    day = Mid(elem, 1, Len(elem) - 6) 

    If Len(day) = 1 Then 
     day = "0" & day 
    End If 

    dotdate = day & "." & month & "." & year 

End Function 

Public Function IsXlsOpen(wbName) As String 
    Dim xl As Excel.Application 

    IsXlsOpen = False 
    On Error Resume Next 
     Set xl = GetObject(, "Excel.Application") 

     If Err.Number <> 0 Then Exit Function  
      xl.Workbooks(wbName).Activate  
     If Err.Number = 0 Then IsXlsOpen= True  
End Function 

Private Sub Command1_Click() 

    Dim i As Integer 
    Dim cek As String 

    Set kitap = CreateObject("Excel.Application") 

    If IsXlsOpen("my.xls") = True Then 
    kitap.Application.Quit 
    End If 

    kitap.Workbooks.Add 

    cek = "Select * From blabla" 
    rs.Open cek, cnn 

    If rs.EOF = True Then 
     Situation.Caption = "Situation : EOF" 
    Else 
     kitap.Cells(i + 1, 1).Value = "ID" 
     kitap.Cells(i + 1, 2).Value = "Caption" 
     kitap.Cells(i + 1, 3).Value = "Date" 
     i = i + 1 
     Do While Not rs.EOF 
      kitap.Cells(i + 1, 1).Value = rs.Fields("id") 
      kitap.Cells(i + 1, 2).Value = rs.Fields("capt") 
      kitap.Cells(i + 1, 3).Value = dotdate(rs.Fields("date")) 
      rs.MoveNext 
      i = i + 1    
     Loop    
     rs.Close     
    End If 

    kitap.ActiveWorkbook.SaveAs (App.Path & "\my.xls") 
    kitap.Application.Quit 
    Situation.Caption = "Situation : Excel Formatted Report Ready." 

    Exit Sub 

err: 
    rs.Close 
    Situation.Caption = "Critical Error! : Connection error detected. Please reset action." 
End Sub 
+0

有這裏似乎有些混亂。您正在創建一個Excel實例,並且如果另一個Excel實例甚至是同一個實例存在,那麼您正在關閉您創建的實例。最好先檢查一個實例是否存在。另外,您是否需要關閉所有工作簿,或只關閉「my.xls」?如果是這樣,爲什麼不使用不同的GetObject格式:'Set xl = GetObject(「C:\ Docs \ MyXL.xls」)''''' 'xl.Close' – Fionnuala

+0

另外,你的意思是VBA,它是Excel原生的,而不是VB6? – Fionnuala

+0

每次我重新創建一個工作簿,並給出相同的myXL.xls名稱,同時保存,所以這不是我的情況我想關閉所有的工作簿,如果posbile保存和關閉行動我需要做什麼,之後,我將重新創建一個Excel工作簿和填寫它比生病保存它。我想用vb6編碼實際上。不適用於excel。 –

回答

2

雖然我更多的是VBScript和VBA的傢伙,多一點信息將有助於:

  • 即是什麼IsXlsOpen
  • 什麼是您的完整kitmap代碼,即您是否打開和關閉工作簿?
  • 你有沒有其他的xl實例打開(在你的代碼之前或期間)?

this link通常可以解決VBA的問題,在固定全局引用

請注意,這是很好的做法,關閉/退出工作簿/實例,並將它們設置爲Nothing,即圖莎爾的代碼

xlWB.Close False 
xlApp.Quit 
Set xlWB = Nothing 
Set xlApp = Nothing 
+0

ty幫助它確實給了一個很好的做法。 –

2

要保存並關閉所有工作簿,read more

Option Explicit 

Sub CloseAndSaveOpenWorkbooks() 
    Dim Wkb As Workbook 

    With Application 
     .ScreenUpdating = False 

     '  Loop through the workbooks collection 
     For Each Wkb In Workbooks 

      With Wkb 

       '    if the book is read-only 
       '    don't save but close 
       If Not Wkb.ReadOnly Then 

        .Save 

       End If 

       '    We save this workbook, but we don't close it 
       '    because we will quit Excel at the end, 
       '    Closing here leaves the app running, but no books 
       If .Name <> ThisWorkbook.Name Then 

        .Close 

       End If 

      End With 

     Next Wkb 


     .ScreenUpdating = True 
     .Quit 'Quit Excel 
    End With 
End Sub