- Recovering More of Your Work with a Shorter AutoRecover Interval
- Automatically Saving Your Work Frequently
- Closing a Document Without Saving
- Closing All Your Open Documents
- Making Backups as You Work
- Showing More Items on the Recent Documents List
- Opening the Most Recently Used Document at Startup
- Clearing the Recent Documents List
- Creating and Opening Document Workspaces
- Automatically Prompting for Document Properties
- Creating a Trusted Location for Documents
- Viewing Total Editing Time Updated in Real-Time
- Calculating Billable Time Charges
- Locking Document Formatting
- Preventing Untracked Changes
- Setting Up a Document for Structured Editing
- Inspecting a Document for Personal Information
- Viewing Two Documents Side by Side
- Updating All Fields Automatically
Creating and Opening Document Workspaces
When I work in Word (which I do pretty much all day long), I usually work with distinct groups of documents:
- If I'm working on a book, I open the current chapter, the outline, a notes document, a document to record screen shots, and so on.
- If I'm working on an article, I open the article, the proposal, my research notes, and so on.
- If I'm working on a blog post, I open the post and perhaps a few supporting documents.
It's not unusual for me to work on two, three, or even four such projects during the day. I like the variety, but it's a major pain to close all the documents for one project and then open all the documents I need for the next project. Perhaps that's why the Word macros I'm going to show you in this chapter are my favorites. Their purpose, as you'll see, is to create workspaces for Word projects that you can easily and quickly open.
A workspace is just a collection of related documents. Unfortunately, Word doesn't come with this functionality, but you can use VBA to create your own workspaces, as shown in Listing 3.8.
Listing 3.8. Procedures That Create and Open a Workspace of Files
' CreateWorkspace() ' Saves the path and filename data of all the ' open files to the Windows Registry. Before ' running this procedure, make sure only the ' files you want in the workspace are open. ' Sub CreateWorkspace(strWorkspaceName As String) Dim total As Integer Dim doc As Document Dim i As Integer ' ' Delete the old workspace Registry settings ' First, get the total number of files ' total = GetSetting("Word", strWorkspaceName, "TotalFiles", 0) For i = 1 To total ' ' Delete each Registry setting ' DeleteSetting "Word", strWorkspaceName, "Document" & i Next 'i ' ' Create the new workspace ' i = 0 For Each doc In Documents ' ' Make sure it's not a new, unsaved file ' If doc.Path <> "" then ' ' Use i to create unique Registry setting names ' i = i + 1 ' ' Save the FullName (path and filename) to the Registry ' SaveSetting "Word", strWorkspaceName, "Document" & i, doc.FullName End If Next 'doc ' ' Save the total number of files to the Registry ' SaveSetting "Word", strWorkspaceName, "TotalFiles", i Application.StatusBar = i & " documents saved to workspace." End Sub ' ' OpenWorkspace() ' Accesses the Registry's workspace settings ' and then opens each workspace file. ' Sub OpenWorkspace(strWorkspaceName As String) Dim total As Integer Dim i As Integer Dim filePath As String Dim doc As Document Dim fileAlreadyOpen As Boolean ' ' See if we should first close all the open documents ' If MsgBox("Close all the open documents first?", vbYesNo) = vbYes Then CloseAllOpenDocuments End If ' ' Get the total number of files from the Registry ' total = GetSetting("Word", strWorkspaceName, "TotalFiles", 0) For i = 1 To total ' ' Get the path and filename ' filePath = GetSetting("Word", strWorkspaceName, "Document" & i) ' ' Make sure the file isn't already open ' fileAlreadyOpen = False For Each doc In Documents If filePath = doc.FullName Then fileAlreadyOpen = True Exit For End If Next 'doc ' ' Open it ' If Not fileAlreadyOpen Then Documents.Open filePath End If Next 'i End Sub
Listing 3.8 shows two procedures that create the workspace functionality for Word:
- CreateWorkspace—This procedure uses the Windows Registry to store a list of open documents. Before running this procedure, make sure that only those files you want to include in the workspace are currently open.
- OpenWorkspace—This procedure first asks if you want to close all the open documents. (If you click Yes, the procedure runs the CloseAllOpenDocuments macro from Listing 3.4.) The procedure then accesses the Registry and runs through the list of saved files. For each setting, the procedure checks to see if the file is already open. If it's not, the procedure runs the Documents.Open method to open the file.
Notice that both procedures take strWorkspaceName as an argument. This is a string value that specifies the name of the workspace you want to create or open. This enables you to create as many different workspaces as you need. Here are two simple procedures that demonstrate how you'd use the macros in Listing 3.8 to create and open a workspace:
Sub CreateWorkspaceTest() CreateWorkspace "My Project" End Sub Sub OpenWorkspaceTest() OpenWorkspace "My Project" End Sub