Word Hacks [Electronic resources] نسخه متنی

اینجــــا یک کتابخانه دیجیتالی است

با بیش از 100000 منبع الکترونیکی رایگان به زبان فارسی ، عربی و انگلیسی

Word Hacks [Electronic resources] - نسخه متنی

Andrew Savikas

| نمايش فراداده ، افزودن یک نقد و بررسی
افزودن به کتابخانه شخصی
ارسال به دوستان
جستجو در متن کتاب
بیشتر
تنظیمات قلم

فونت

اندازه قلم

+ - پیش فرض

حالت نمایش

روز نیمروز شب
جستجو در لغت نامه
بیشتر
لیست موضوعات
توضیحات
افزودن یادداشت جدید







Hack 13 Build a Better MRU

Summon more than your nine most recently used
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 Works


Barring 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.



When you open a file from a macro, you can use the
AddToRecentFiles property to prevent Word from
adding it to the MRU:

Documents.Open FileName:="Foo.doc",_
AddToRecentFiles:=False

Generally, the MRU works well for light users of Word. To change the
number of entries on the MRU, select ToolsOptions, click
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

If you want to wipe the MRU clean, clear the checkbox, close the
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.)


You'll confuse the MRU if you delete, move, or
rename any of the files it currently lists. Word will suggest you
check the file's permissions, make sure you have
enough free memory and disk space to open the document, or open the
file with the Text Recovery converter. But it will not remove the
file from the MRU, as you probably expect (and want).

The MRU is handy, but for a power user it doesn't go
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 Started


You 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

The key to this hack is the PrivateProfileString command [Hack #67],
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]
MRU01=C:\Dox\Doc 1.doc
MRU02=C:\Dox\Doc 2.doc


2.12.3 The Code


The 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
InsertClass to create a new class in
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.Application
Private Sub MyMRU_DocumentBeforeClose(ByVal Doc As Document, _
Cancel As Boolean)
With ActiveDocument
If .Path <> " Then
Add_to_MRU
Else
If .Saved = False Then
Select Case MsgBox("Do you want to save the changes " & _
"to " & .Name & "?", _
vbYesNoCancel + vbExclamation, _
"Microsoft Office Word")
Case vbYes
Dialogs(wdDialogFileSaveAs).Show
If .Saved = True Then Add_to_MRU
Case vbNo
.Close SaveChanges:=wdDoNotSaveChanges
Case vbCancel
End
End Select
End If
End If
End With
End Sub

Click the Close button to close the MRUClass
window.

2.12.3.1 Creating the MRU module


Next, 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 InsertModule. Select the
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 module


The 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 MRUClass
Sub Initialize_MyMRU( )
Set MyMRU.MyMRU = Word.Application
End Sub

Each 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 form


To display the user form, use its Show method:

Sub Open_MyMRU( )
frmMRU.Show
End Sub

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 MRU


The 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( )
Dim i As Integer
For i = 24 To 1 Step -1
System.PrivateProfileString(FileName:="c:\windows\mru.ini", _
Section:="MRU_Files", Key:="MRU" & Format(i + 1, "00")) = _
System.PrivateProfileString(FileName:="c:\windows\mru.ini", _
Section:="MRU_Files", Key:="MRU" & Format(i, "00"))
Next i
System.PrivateProfileString(FileName:="c:\windows\mru.ini", _
Section:="MRU_Files", Key:="MRU01") = ActiveDocument.FullName
End Sub


2.12.4 Creating the User Form


Here'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
InsertUserForm.

Press F4 to activate the Properties window, type
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
FormatCenter In FormHorizontally).

Create a command button named cmdOpen, set
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 (FormatGroup), position the
group at the bottom of the user form, and center it horizontally.


2.12.5 Adding the Code to the User Form


After 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 macro


The 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( )
Dim i As Integer
For i = 1 To 25
lstMRU.AddItem System.PrivateProfileString( ,_
FileName:="d:\windows\mru.ini", _
Section:="MRU_Files", Key:="MRU" & Format(i, "00"))
Next i
End Sub

2.12.5.2 Creating the lstMRU_Click macro


The 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( )
cmdOpen.Enabled = True
End Sub

2.12.5.3 Creating the cmdCancel_Click macro


The cmdCancel_Click macro hides the user form and
then unloads it from memory after the user clicks the Cancel button:

Private Sub cmdCancel_Click( )
frmMRU.Hide
Unload frmMRU
End Sub

2.12.5.4 Creating the cmdOpen_Click macro


The 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( )
On Error GoTo Trap
frmMRU.Hide
Documents.Open lstMRU.Value
Unload frmMRU
End
Trap:
If Err.Number = 5174 Then MsgBox "Word cannot find the file " _
& lstMRU.Value & "." _
& vbCr & vbCr &_
"The file may have been renamed, moved, or deleted.", vbOKOnly + vbCritical,
"MRU - File Not Found"
End Sub

After you make your changes, click the Save Normal button in the
Visual Basic Editor to save Normal.dot.


2.12.6 Using the MRU


With 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 Hack


You 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]
Name=c:\dox\Example 1.doc
Size=144048
Creator=Adam Schmidt
[MRUFile02]
Name=Z:\Public\Memo 1443.doc
Size=256074
Creator=Stelios Jones

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 Sub

Guy Hart-Davis


/ 162