首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >重新打开最近关闭的Excel实例

重新打开最近关闭的Excel实例
EN

Stack Overflow用户
提问于 2017-04-16 02:27:12
回答 2查看 259关注 0票数 2

如果我使用下面的代码关闭当前打开的所有Excel实例,那么我需要使用什么来重新打开刚刚关闭的Excel的所有实例?我知道我将不得不更改下面的代码以保存某个文件,但只是不确定实际的代码应该是什么。

代码语言:javascript
复制
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
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-04-16 05:24:50

这将存储工作簿到文本文件的文件路径。如果以False作为输入运行此宏,则将打开所有最近关闭的文件。(未测试)

代码语言:javascript
复制
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
票数 1
EN

Stack Overflow用户

发布于 2017-04-16 06:52:36

您可以使用一个Very-Hidden工作表,在该工作表中,您将保持当前打开的所有文件。

备注:如果您需要,可以为注册表保存和读取一个选项。

Sub CloseAllExcel Code

代码语言:javascript
复制
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 代码:从"Admin“非常隐藏的表中的"A”列读取文件(名称和路径)。

代码语言:javascript
复制
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
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43433124

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档