XL: Macro to Open the Most Recently Used File

ID: Q109113


The information in this article applies to:
  • Microsoft Excel for Windows, versions 5.0, 5.0c
  • Microsoft Excel for Windows NT, version 5.0
  • Microsoft Excel for Windows 95, version 7.0


SUMMARY

In Microsoft Excel, you can access a list of the four most recently used files by selecting the File menu. The following macro opens the first file and displays the name of the third file in the list.


MORE INFORMATION

Microsoft provides examples of Visual Basic procedures for illustration only, without warranty either expressed or implied, including but not limited to the implied warranties of merchantability and/or fitness for a particular purpose. This Visual Basic procedure is provided 'as is' and Microsoft does not guarantee that it can be used in all situations. Microsoft does not support modifications of this procedure to suit customer requirements for a particular purpose. Note that a line that is preceded by an apostrophe introduces a comment in the code--comments are provided to explain what the code is doing at a particular point in the procedure.

The following macro will open the most recently used file. Before you run this macro you must make sure that the most recently used file list is turned on. To verify that this option is selected, choose Options from the Tools menu and then select the General tab. Make sure that the Recently Used File List box is checked.


Option Explicit

Sub MostRecentlyUsedFileList()

'Dimension the array to hold the file list
Dim files(4)
Dim arraypos, begin As Integer
Dim item As String

'Initialize a pointer for the array
arraypos = 0

'Find the position of the File List menuitem. This will be used as a
'starting location in the list of menuitems on the File menu.
begin = MenuBars("Worksheet").Menus("file").MenuItems("file list").Index

'Begin loop that continues until it encounters the divider line that
'follows the end of the file list
Do Until MenuBars("Worksheet").Menus("file").MenuItems(begin).Caption = "-"

    'Place the name of the menuitem into the variable called Item.
    'This name will include the ampersand, the number of the file in
    'the list, a space, then the file name itself.
    item = MenuBars("Worksheet").Menus("file").MenuItems(begin).Caption

    'Remove the first three characters of Item and place name in array
    files(arraypos) = Mid(item, 4, Len(item) - 3)

    'Increment counter for menuitem and pointer in array
    begin = begin + 1
    arraypos = arraypos + 1
Loop

   'opens the first file on the most recently used list
   Workbooks.Open filename:=files(0)

   'displays the name of the third file on the most recently used list
   MsgBox files(2)

End Sub 

Additional query words:

Keywords : kbprg
Version : WINDOWS:5.0,5.0c,7.0; winnt:5.0
Platform : WINDOWS winnt
Issue type :


Last Reviewed: October 21, 1999
© 2000 Microsoft Corporation. All rights reserved. Terms of Use.