Caution

Be careful you don't specify an Index value that's greater than the number of items in the recent documents list.To check the maximum possible value for Index,use RecentFiies.count.

Each RecentFile object comes with a Name property that returns the file's name, as well as a Path property that returns the file's path. Here's some code that uses these two properties to tell Word to open the most recently used file:

With RecentFiles(1)

Documents.Open .Path & "\" & .Name End With

That's useful, but it would be handy to have Word open the most recently used file each time you start the program. If you want Word to run some code each time it's started, follow these steps:

1. Open the Normal project in the Visual Basic Editor's Project Explorer.

2. Create a new module and rename it as AutoExec.

3. In this module, create a Sub procedure named Main.

4. Enter your code in that procedure.

Listing 7.1 shows a sample Main procedure that opens the most recently used file at startup.

0 0

Post a comment