3 Smart Ways to Quickly Reopen Last Closed Word Document via VBA

In this article, we want to show you 3 quick ways to quickly reopen some last closed Word document via VBA.

When surfing on the Internet via a browser, if you accidentally close a web page, you can easily get it back through a feature called “Reopen closed tab”. So is there such a feature which we can use in Word?

Unfortunately, there is no built-in tool for this. But as always, we can make use of some macros to achieve desired result.Reopen Recently Closed Word Documents

Method 1: Reopen the Last Closed Document

  1. First of all, press “Alt+ F11” to open VBA editor.
  2. Then click on “Normal’ and then click “Insert” tab.
  3. Next choose “Module” on the drop-down menu of “Insert”.Click "Normal"->Click "Insert"->Click "Module"
  4. And open new module by double clicking on it.
  5. Now paste and save the following codes on new module:
Function GetTempFolder() As String
  GetTempFolder = IIf(Environ$("tmp") <> "", Environ$("tmp"), Environ$("temp"))
End Function

Sub AutoClose()
  Const ForReading = 1, ForWriting = 2, ForAppending = 8

  Dim objFileSystem
  Dim objFile
  Dim strLogFileName As String
  Dim strText As String, strLine As String, strAddText As String

  strLogFileName = GetTempFolder() & "\" & "OpenLog.txt"

  Set objFileSystem = CreateObject("Scripting.FileSystemObject")
  Set objFile = objFileSystem.OpenTextFile(strLogFileName, ForAppending, True, TristateFalse)

  objFile.WriteLine (ActiveDocument.FullName)
  objFile.Close
End Sub

Sub ReopenLastClosedDoc()
  Const ForReading = 1, ForWriting = 2, ForAppending = 8

  Dim objFileSystem
  Dim objFile
  Dim strLogFileName As String
  Dim strText As String, strAddText As String
  Dim strLine As String
  Dim objLines As New Collection
  Dim nCount As Integer

  strLogFileName = GetTempFolder() & "\" & "OpenLog.txt"

  Set objFileSystem = CreateObject("Scripting.FileSystemObject")
  Set objFile = objFileSystem.OpenTextFile(strLogFileName, ForReading, False, TristateFalse)

  Do While objFile.AtEndOfStream <> True
    strLine = objFile.ReadLine
    objLines.Add (strLine)
  Loop

  nCount = objLines.Count
  Documents.Open objLines.Item(nCount)

  Set objLines = Nothing
End Sub
  1. Next go to assign a button for the macro and add it to “Quick Access Toolbar”. For detailed steps, you can refer to this article: How to Remove the Formatting of Pasted Texts with Macro and VBA in Your Word
  2. Lastly, click the button you just add to“Quick Access Toolbar” to run the macro. And you shall have the last closed document reopened.Click the Button to Run Macro

Note:

Above codes create a text file named as “OpenLog” in “Temp” folder on your computer. Then every time you close a document, its full name will be written into the text file.

Method 2: Reopen Last Five Closed Documents

  1. Install and run macro following steps in method 1.
  2. Remember to replace macro “ReopenLastClosedDoc” with this one:
Sub ReopenLast5ClosedDocs()
  Const ForReading = 1, ForWriting = 2, ForAppending = 8

  Dim objFileSystem
  Dim objFile
  Dim strLogFileName As String
  Dim strText As String, strAddText As String
  Dim strLine As String
  Dim objLines As New Collection
  Dim nIndex As Integer, nCount As Integer

  strLogFileName = GetTempFolder() & "\" & "OpenLog.txt"
 
  Set objFileSystem = CreateObject("Scripting.FileSystemObject")

  Set objFile = objFileSystem.OpenTextFile(strLogFileName, ForReading, False, TristateFalse)
 
  Do While objFile.AtEndOfStream <> True
    strLine = objFile.ReadLine
    objLines.Add (strLine)
  Loop
 
  nCount = objLines.Count
 
  For nIndex = nCount To (nCount - 4) Step -1
    Documents.Open objLines.Item(nIndex)
  Next nIndex

  Set objLines = Nothing
End Sub

With this macro, you can reopen the last five documents just closed in a second.

Method 3: Reopen Last Several Closed Documents

Concrete need varies from person to person. Thus, we will offer you a macro to reopen a specific number of closed documents.

What you need is to replace “ReopenLastClosedDoc” with this macro:

Sub ReopenMultipleRecentlyClosedDocs()
  Const ForReading = 1, ForWriting = 2, ForAppending = 8

  Dim objFileSystem
  Dim objFile
  Dim strLogFileName As String
  Dim strText As String, strAddText As String, strNumOfDocsToBeRreopened As String
  Dim strLine As String
  Dim objLines As New Collection
  Dim nIndex As Integer, nCount As Integer

  strLogFileName = GetTempFolder() & "\" & "OpenLog.txt"
 
  Set objFileSystem = CreateObject("Scripting.FileSystemObject")

  Set objFile = objFileSystem.OpenTextFile(strLogFileName, ForReading, False, TristateFalse)
 
  Do While objFile.AtEndOfStream <> True
    strLine = objFile.ReadLine
    objLines.Add (strLine)
  Loop
 
  nCount = objLines.Count
 
  strNumOfDocsToBeRreopened = InputBox("Enter the number of documents need to reopen: ")
 
  For nIndex = nCount To (nCount - strNumOfDocsToBeRreopened + 1) Step -1
    Documents.Open objLines.Item(nIndex)
  Next nIndex

  Set objLines = Nothing
End Sub

Running the macro will trigger an input box. Just enter the number of documents you need to reopen and click “OK”.Enter a Number ->Click "OK"

Tip to Salvage Damaged Documents

As we know, documents and data are recoverable. It really gives us a lot comfort while a data disaster breaking out. So the most recommend and best way to do a doc recovery is to obtain a professional tool.

Author Introduction:

Vera Chen is a data recovery expert in DataNumen, Inc., which is the world leader in data recovery technologies, including fix xls and pdf repair software products. For more information visit www.datanumen.com