How to get the most recently used document to be opened automatically when you open Word

Article contributed by Astrid Zeelenberg, Dave Rado and Will Rickards

1.

Using a command line switch to open the most recently used document

You can open Word and have the most recently used (MRU) file opened automatically by clicking on the Windows Start button, selecting Run, and typing:
winword.exe /mFile1.

The /m switch normally means run a macro called [whatever follows the m], but it can also be used to run almost any WordBasic command, and File1 is the WordBasic command to open the MRU file. The only WordBasic commands which don't appear to work when following the /m switch are those which rely on a document already being open.

Alternatively, you can create a new shortcut to Word; right-click on it and select Properties; and on the Shortcut tab, where it says Target, add /mFile1 to the end of the path, so it looks like this:

"C:\Program Files\Microsoft Office\Office\WINWORD.EXE" /mFile1

Make sure that the path of Winword.exe is still set to the folder where Office is installed.

You can now use the new shortcut when you want to start Word with the most recently used file and your usual shortcut when you don't. 

However, note that using the /m switch prevents any AutoExec macros you may have from running. Also, if the MRU file is inaccessible or has been deleted or renamed, this method won't find the most recently used file that does exist; it will just create a blank document. If you want it to open the most recently used valid file, or if you don't want to disable your AutoExec macros, you'll need to use a  macro.

2.

Using a macro to open the most recently used document whenever Word opens

Paste the following code into a Global template and it will run automatically whenever Word opens:

Sub AutoExec()

Dim
oRecentFile As RecentFile

If RecentFiles.Count >= 1 Then
   
    'First delete any invalid files from the recent files list.
    On Error Resume Next
    'in case one of the drives is an invalid drive
    For Each oRecentFile In RecentFiles
         If Len(Dir(oRecentFile.Path & "\" & oRecentFile.Name)) = 0 Then
            'If the file doesn't  exist, delete it from the list
            RecentFiles(oRecentFile.Index).Delete
        End If
    Next oRecentFile
    Set oRecentFile = Nothing
    On Error GoTo 0

    If RecentFiles.Count >= 1 Then
        RecentFiles(1).Open
    End If

End If

End Sub

The AutoExec macro opens the first valid file that's listed in the MRU (Most Recently Used) list: For more on AutoExec macros see: Writing application event procedures.

Note that if you also want the selection to return to your last editing point, you can add the line:

Application.GoBack

... just after the line:

RecentFiles(1).Open

3.

Using a macro to open the most recently used document whenever Word is opened from its icon, but not when you open Word by launching a file

Paste the following code into a module in a Global template:

Option Explicit

Public Declare Function
GetCommandLine Lib "kernel32" _
 Alias "GetCommandLineA" () As Long

Public Declare Function
lstrcpy Lib "kernel32" _
 Alias "lstrcpyA" (ByVal lpString1 As String, _
 ByVal lpString2 As Long) As Long

Public Declare Function
lstrlen Lib "kernel32" _
 Alias "lstrlenA" (ByVal lpString As Long) As Long


Sub
AutoExec()

  Dim
oRecentFile As RecentFile
  Dim
strCommandLine As String

  If
RecentFiles.Count >= 1 Then

      'First delete any invalid files from the recent files list.
      On Error Resume Next
      'in case one of the drives is an invalid drive
      For Each oRecentFile In RecentFiles
           If Len(Dir(oRecentFile.Path & "\" & oRecentFile.Name)) = 0 Then
              'If the file doesn't  exist, delete it from the list
              RecentFiles(oRecentFile.Index).Delete
          End If
      Next oRecentFile
      Set oRecentFile = Nothing
      On Error GoTo 0

      'get the commandline by calling the CmdLinetoString function, which follows
     
strCommandLine = CmdLinetoString(GetCommandLine()) 

      If Len(strCommandLine) <= Len(Chr(34) & Application.Path & _
        "\" & "winword.exe" & Chr(34) & " ") Then
          'Following only runs if Word was launched from its icon
          If RecentFiles.Count >= 1 Then
              RecentFiles(1).Open
          End If
      End If
  End If
End Sub


Public Function CmdLinetoString(ByVal lngPtr As Long) As String
    Dim strReturn As String
    Dim StringLength As Long
    'get the length of the string (not including the terminating null character)
    StringLength = lstrlen(lngPtr)
    'initialize our string so it has enough characters including the null character
    strReturn = String$(StringLength + 1, 0)
    'copy the string we have a pointer to into our new string
    lstrcpy strReturn, lngPtr
    'now strip off the null character at the end
    strReturn = Left$(strReturn, StringLength)
    'return the string
    CmdLinetoString = strReturn
End Function


Again, if you also want the selection to return to your last editing point, just add the line:

Application.GoBack

... immediately after the line:

RecentFiles(1).Open

4.

The Application.GoBack bug and how to get round it

See the article: GoBack (Shift+F5) doesn't work in some newly-opened documents


Click to view Terms of Use page

Click to view Disclaimer page