Reopening recently closed instances of Excel

不羁岁月 提交于 2020-01-15 07:49:27

问题


If I use the below code to close all instances of Excel that are currently open what would I need to use to reopen all the instances of Excel that were just closed? I know I'll have to change the below to save a filepath somewhere but just not sure what the actual code should be.

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

回答1:


This stores file paths of the workbooks to a text file. If you run this macro with False as the input, this will open all of the recently closed files. (Not tested)

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



回答2:


You could use a Very-Hidden worksheet, where you'll keep all of the Files currently open.

Note: If you want there is an option to Save and Read for the Registry.

Sub CloseAllExcel Code:

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 Code: reads the files (names and Path) from Column "A" in "Admin" very-hidden sheet.

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


来源:https://stackoverflow.com/questions/43433124/reopening-recently-closed-instances-of-excel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!