2017-04-16 136 views
1

如果我使用下面的代碼來關閉所有當前打開的Excel實例,我需要用什麼來重新打開剛纔關閉的所有Excel實例?我知道我必須更改下面的內容來保存某個文件路徑,但不知道實際的代碼應該是什麼。重新打開最近關閉的Excel實例

Public Sub CloseAllExcel() 
On Error GoTo handler 

    Dim xl As Excel.Application 
    Dim wb As Excel.Workbook 

    Do While xl Is Nothing 
     Set xl = GetObject(, "Excel.Application") 
     For Each wb In xl.Workbooks 
     wb.Save 
     wb.Close 
     Next 
     xl.Quit 
     Set xl = Nothing 
    Loop 
    Exit Sub 

handler: 
    If Err <> 429 Then 'ActiveX component can't create object 
     MsgBox Err.Description, vbInformation 
    End If 

End Sub 
+0

也許構建它們被關閉之前,可以容納所有的文件的路徑數組,寫入陣列到紙張上和下一次當你打開宏工作簿,您可以打開所有你最近關閉的工作簿通過閱讀數組。 – sktneer

回答

1

這將工作簿的文件路徑存儲到文本文件。如果以False作爲輸入運行此宏,將打開所有最近關閉的文件。 (未測試)

Public Sub CloseAllExcel(Closing As Boolean) 
On Error GoTo handler 

    Dim xl As Excel.Application 
    Dim wb As Excel.Workbook 

    Dim strPath As String 
    strPath = "C:\path.txt" 


    If Close Then 

    Dim fso as Object 
    Set fso = CreateObject("Scripting.FileSystemObject") 

    Dim oFile as Object 
    Set oFile = FSO.CreateTextFile(strPath) 



    Do While xl Is Nothing 
     Set xl = GetObject(, "Excel.Application") 
     For Each wb In xl.Workbooks 

     oFile.WriteLine Application.ActiveWorkbook.FullName 
     wb.Save 
     wb.Close 
     Next 
     oFile.Close 
     Set fso = Nothing 
     Set oFile = Nothing 
     xl.Quit 
     Set xl = Nothing 
    Loop 

    Exit Sub 

    Else 
    Dim FileNum As Integer 
    Dim DataLine As String 

    FileNum = FreeFile() 
    Open strPath For Input As #FileNum 

    While Not EOF(FileNum) 
     Line Input #FileNum, DataLine 
     Workbooks.Open DataLine 
    Wend 
    Exit Sub 
    End If 

handler: 
    If Err <> 429 Then 'ActiveX component can't create object 
     MsgBox Err.Description, vbInformation 
    End If 

End Sub 
+0

這就是我想的之後,我只需要讓它工作。你不能在括號中使用Close來聲明子名(我不是VBA專家 - 我實際上並不知道這些括號是做什麼的)。由於這是一個公共子集,我本可以期望能夠推F8並循環訪問代碼,但我無法這樣做 - 爲什麼?謝謝您的幫助。 – nick1408

+0

@ nick1408這是sub的一個參數。您可以通過另一個子窗口或即時窗口調用它:CloseAllExcell True。或者,您可以刪除括號內的參數,並在代碼的第一行添加Close = True(或False,如果您想打開以前關閉的文件)。 – Masoud

+0

再次感謝@Masoud。當我把代碼放入VBA編輯器的行 public sub CloseAllExcel(Close As Boolean) 如果關閉然後 cole紅色(因爲是不好)。給出的錯誤是編譯錯誤預期:標識符。我對你的建議編輯有一個破解,並會報告回來,但我確實喜歡你的原始代碼選項(CloseAllExcel True/False) – nick1408

1

您可以使用Very-Hidden工作表,您可以在其中保留所有當前打開的文件。

注意:如果你想有一個選項來保存和讀取註冊表。

CloseAllExcel代碼

Option Explicit 

Public Sub CloseAllExcel() 

On Error GoTo handler 

Dim xlApp As Excel.Application 
Dim wb As Excel.Workbook 
Dim i As Long 
Dim Hidws As Worksheet 

On Error Resume Next 
Set Hidws = ThisWorkbook.Worksheets("Admin") 
On Error GoTo 0 

If Hidws Is Nothing Then ' check if there isn't "Admin" sheet exists in the workbook 
    Set Hidws = ThisWorkbook.Sheets.Add(Before:=ThisWorkbook.Worksheets(Worksheets.Count)) 
    Hidws.Name = "Admin" 
    Hidws.Visible = xlSheetVeryHidden ' make the "Admin" sheet very-hidden 
End If 

i = 1 
Do While xlApp Is Nothing 
    Set xlApp = GetObject(, "Excel.Application") 
    For Each wb In xlApp.Workbooks 
     Hidws.Range("A" & i).Value = wb.FullName ' save each workbook full name and path in column "A" in "Admin" very-hidden sheet 
     i = i + 1 
     wb.Close True 
    Next 
    xlApp.Quit 
    Set xlApp = Nothing 
Loop 
Exit Sub 

handler: 
If Err <> 429 Then 'ActiveX component can't create object 
    MsgBox Err.Description, vbInformation 
End If 

End Sub 

RestoreExcelLastSession代碼:在 「管理」 非常隱蔽片讀取文件從 「A」 欄(名稱和路徑)。

Sub RestoreExcelLastSession() 

Dim xlApp As Excel.Application 
Dim wb  As Excel.Workbook 
Dim i  As Long 
Dim Hidws As Worksheet 

On Error Resume Next 
Set Hidws = ThisWorkbook.Worksheets("Admin") 
On Error GoTo 0 

If Hidws Is Nothing Then ' check if "Admin" sheet exists 
    MsgBox "No Files have been restored" 
    Exit Sub 
End If 

i = 1 
Do While Hidws.Range("A" & i).Value <> "" ' loop through cells in Column "A" 
    Set xlApp = CreateObject("Excel.Application") ' open a new Excel instance per file 
    xlApp.Workbooks.Open (Hidws.Range("A" & i).Value) 
    i = i + 1 
    Set xlApp = Nothing 
Loop 

End Sub 
相關問題