Hack 13 Build a Better MRU![]() ![]() files at the touch of a button with this hack.When you work in Word, you often need to access files you were using earlier. To help you, Word provides a list of the most recently used files: the MRU, which appears by default at the bottom of the File menu. 2.12.1 How Word's MRU WorksBarring any action on your part, Word automatically adds files to the MRU when you do the following:Open an existing document.Save a file for the first name.Use the Save As command to save a file under a different name.
number of entries on the MRU, select Tools the General tab, and adjust the "Recently used file list" setting, as shown in Figure 2-35. Word can remember up to nine of your most recently used files, or you can choose zero to disable the MRU. Figure 2-35. Changing your MRU settings![]() Options dialog box, reopen it, and specify how many entries you want for your fresh start. (Remember that other lists, such as the My Recent Documents list in Windows XP and Windows 2000, can still betray your indiscretions.)
nearly far enough. If you open 90 documents each day, a list of 9 is a waste of time. Here's how to pump up the MRU to power-user proportions, creating a MegaMRU. 2.12.2 Getting StartedYou will implement this MegaMRU as a user form (see Figure 2-36) that displays the names of the last 25 documents you used in Word. To open a document, select it from the list and click the Open button. Figure 2-36. A custom MRU lets you access far more of your recent documents![]() which lets you store data in a plain-text .ini settings file on your system. For this hack, create the following .ini file: [MRU_Files] 2.12.3 The CodeThe MegaMRU uses an event handler [Hack #69] with the DocumentBeforeClose event to get its information:Open the Visual Basic Editor and make sure you display the Project Explorer and the Properties window.In the Project Explorer, right-click Normal and select Insert Normal.dot. Press F4, type the name for the class (MRUClass), and press Enter.Press F7 to activate the Code window for the class module and insert the following code, which will ignore unsaved documents. It will, however, alert you to save previously saved documents. Public WithEvents MyMRU As Word.ApplicationClick the Close button to close the MRUClass window. 2.12.3.1 Creating the MRU moduleNext, create a code module in Normal.dot with the macro for initializing the class module, the macro for displaying the user form, and the macro for adding items to the MRU. To create a new module, select Normal in the Project Explorer and choose Insert module and change its name in the Properties window to MegaMRU. You should place the rest of the code in this section in the MegaMRU module. 2.12.3.2 Initializing the class moduleThe code for initializing the class module consists of a declaration of MyMRU as a new member of the MRU class and a short macro that assigns the Word.Application object to the MyMRU property of the MyMRU object: Dim MyMRU As New MRUClassEach time you start Word, you must run the Initialize_MyMRU macro to start your event handler. Usually, you put a call to the macro in your AutoExec macro [Hack #60] . If you don't have an AutoExec macro already, you can simply name the Initialize_MyMRU macro above AutoExec instead. 2.12.3.3 Displaying the user formTo display the user form, use its Show method: Sub Open_MyMRU( )Because you'll use this macro to open one of the documents on your MRU, create a menu item, a toolbar button, or a keyboard shortcut [Hack #1] for the macroor even all three. 2.12.3.4 Adding a document's information to the MRUThe macro for adding a document's information to the MRU moves all the existing entries in the MRU list (except the last entry) one place down the list: item 25 drops off the list, item 24 moves to item 25, item 23 moves to item 24, and so forth. (You use a For... Next loop with a negative increment to make this change, because working positively propagates the same item through the list: item 1 becomes 2 becomes 3 becomes 4, and so on.) The new document then enters at the top of the chart. Sub Add_to_MRU( ) 2.12.4 Creating the User FormHere's how to create the user form used to display the MegaMRU, as shown in Figure 2-36:Right-click Normal in the Project Explorer and select Insert frmMRU as the name, and press Enter.Use the down arrow key to move to the Caption property, type Most Recently Used Word Documents as its value, and press Enter.Increase the user form's height to about 350 pixels and its width to about 400 pixels. (Either drag the sizing handle or type the measurements in the Properties window.)Add a label with the caption Most Recently Used Documents, with AutoSize set to True and WordWrap set to False. Position the label at the upper-left corner of the user form.Add a listbox, name it lstMRU, and make it about 250 pixels high and 360 pixels wide. To make sure the user can select only one item in the list at a time, set the MultiSelect property to 0 - fmMultiSelectSingle. Center the listbox horizontally in the user form (Select Format Accelerator to O, set Caption to Open, set Default to True, and set Enabled to False. You may want to reduce the button's height and width a little from the (rather big) default measurements.Create a second command button. Name this one cmdCancel, set its Accelerator to C, set Cancel to True, set Caption to Cancel, set Default to False, and make sure Enabled is True. If you changed the height or width of cmdOpen, make this button the same size.Select and group the buttons (Format group at the bottom of the user form, and center it horizontally. 2.12.5 Adding the Code to the User FormAfter laying out the user form, select the user form and press F7 to display its code sheet in a window. Then create the following four macros. 2.12.5.1 Creating the UserForm_Initialize macroThe UserForm_Initialize macro adds the items in the MRU file to the listbox in the user form. This macro runs when you call the user form. Private Sub UserForm_Initialize( ) 2.12.5.2 Creating the lstMRU_Click macroThe lstMRU_Click macro enables the cmdOpen button on the user form as soon as the user clicks an entry. This macro prevents the user from clicking the Open button with no entry selected. Private Sub lstMRU_Click( ) 2.12.5.3 Creating the cmdCancel_Click macroThe cmdCancel_Click macro hides the user form and then unloads it from memory after the user clicks the Cancel button: Private Sub cmdCancel_Click( ) 2.12.5.4 Creating the cmdOpen_Click macroThe cmdOpen_Click macro hides the user form, opens the document corresponding to the item chosen in the listbox, and then unloads the user form from memory. This macro also contains a short error handler, but it reports an error only if Word can't find the file. Private Sub cmdOpen_Click( )After you make your changes, click the Save Normal button in the Visual Basic Editor to save Normal.dot. 2.12.6 Using the MRUWith all these items in place, you're ready to use the user form. Run the Initialize_MyMRU macro to initialize your event handler, which will start monitoring Word's document closures. Each document you close will be added in turn to your MRU. To open a document on your MRU, use the menu, toolbar, or keyboard customization you created to display the user form. Next, click the document in the listbox and click the Open button. 2.12.7 Hacking the HackYou can modify the MegaMRU in several ways:Increase the number of documents involved. You can track as many documents as you want, but you will likely reach the point of diminishing returns somewhere between 100 and 200 documents. If you add too many entries to the list, rewriting the .ini file can slow down an aging PC, but today's brawny processors sneer at such trivial tasks. To increase the number of files to, for example, 100, change the 25 in the UserForm_Initialize procedure to 100 and the 24 in the Add_to_MRU procedure to 99.To present the documents on the MRU list by date, file size, or another useful attribute, create a separate section for each document within the .ini file: MRUFile01 for the first document, MRUFile02 for the second document, and so on. You can then use the keys to create further subdivisions of data: [MRUFile01]To exclude certain documents, folders, or templates from the MRU, add one line to the MyMRU_DocumentBeforeClose procedure. For example, to exclude documents based on a template named Secret.dot, make the following the first line of the Add_to_MRU macro: If ActiveDocument.AttachedTemplate = "Secret.dot" Then Exit SubGuy Hart-Davis |