重新打开最近closures的Excel实例

如果我使用下面的代码来closures所有当前打开的Excel实例,我需要用什么来重新打开刚closures的所有Excel实例? 我知道我将不得不改变下面的地方保存一个文件path,但只是不知道实际的代码应该是什么。

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 

这将工作簿的文件path存储到文本文件。 如果用False作为input运行这个macros,这将打开所有最近closures的文件。 (未testing)

 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 

你可以使用一个Very-Hidden工作表,在那里你将保留所有的文件当前打开。

注意 :如果你想有一个选项来保存和读取registry。

Sub 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 

Sub RestoreExcelLastSession代码 :从“Admin”隐藏工作表中的列“A”中读取文件(名称和path)。

 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