Manipulating the Work menu with macros

Article contributed by John McGimpsey

Screen shot - Work Menu

Word's Work menu is one of Word's best and most underutilized features. Many of us have documents we work on often, or templates that we modify on a regular basis. Storing these files in the Work menu makes them easy to retrieve at a moment's notice. And unlike the Most Recently Used file list in the File Menu, the Work menu is persistent, which is handy if you open nine files in between.

The Work menu suffers, however, from some disadvantages as well. First, it can be difficult to remove items from the menu, if one manages to remember the keyboard shortcut (and on Mac OS X, the default accessibility options may pre-empt the shortcut, making it impossible). Since the shortcut's Remove Menu Item command works for any menu item, if your finger inadvertently slips off the mouse button, you may delete something else entirely.

A second disadvantage is that the menu is stored in a volatile preference file, which means that if you have to trash the preferences, or if you want to move your installation to another machine, you lose your settings.

A third disadvantage is a lack of context-specific loading of the menu. Whenever I'm developing an add-in, I like to have the add-in listed in the Work menu, so that if I have to modify it, I can open it quickly without having to navigate through a bunch of folders. When the add-in isn't loaded, however, I'd prefer not to see it on the menu. Using the regular user interface, though, once you add a file, it stays added until it you manually remove it.

I use a system of macros to make sure that the Work menu stays clean and user friendly. Specifically,

Removing items from the Work Menu

Removing items manually

You can remove items from the Work menu by typing the CMD, OPT and - keys (that's the minus key from the main keyboard). The cursor will turn into a thick minus sign:

Screen shot  - Cursor

Now select the item from the menu. Note: You should be careful selecting menu items, as this works for any menu item in Word.

If the keyboard shortcut does nothing...

When Mac OS X's Universal Access (UA) is enabled, and zooming is turned on (perhaps via the CMD-OPT-8 key combo), the CMD-OPT-<minus> key combination is intercepted by the system to zoom the screen out. If your screen is already at max zoom, this will make the combination appear to do nothing.

If you want to leave UA enabled, type CMD-OPT-8, then CMD-OPT-<minus>. If you want to turn UA off, select the Keyboard Shortcuts tab from the Keyboard and Mouse pane of the System Preferences dialog. Uncheck the Universal Access checkbox.

Using a macro to delete selected items from the Work menu

This macro will allow you to selectively delete items from the Work menu. You can attach it to a toolbar button or a keyboard shortcut, or run it from the Tools/Macro/Macros dialog. A series of prompts will appear, allowing you to delete or skip each item in the Work menu:

Screen shot - Delete dialog

   Public Sub DeleteWorkMenuItems()
      Dim i As Long
      Dim nDelete As Long
      With WorkMenuItems
         For i = .Count To 1 Step -1
            nDelete = MsgBox( _<
               Prompt:="Delete """ & .Item(i).Name & """?", _
               Buttons:=vbYesNoCancel, _
               Title:="Delete Item from menu")
            If nDelete = vbCancel Then Exit For
            If nDelete = vbYes Then .Item(i).Delete
         Next i
      End With
   End Sub
      

Using a macro to delete ALL items from the Work menu

This macro will clear all items from the Work Menu:

    Public Sub ClearWorkMenu() 
        Dim wmItem As WorkMenuItem 
        For each wmItem in WorkMenuItems 
            wmItem.Delete 
        Next wmItem 
    End Sub 
      

Adding a Delete item to the Work menu

You can put an item on the Work menu that will allow you to call the DeleteWorkMenuItems macro, above. Putting the following macros and DeleteWorkMenuItems in a regular code module in an add-in will place a new item on the Work menu to make deletions easier.

Screen shot - Work Menu

    Public Sub AutoExec()
        AddDeleteItemToWorkMenu
    End Sub


    Public Sub AutoClose()
        RemoveDeleteItemFromWorkMenu
    End Sub


    Private Sub AddDeleteItemToWorkMenu()
        Debug.Print "Add Delete Item"
        RemoveDeleteItemFromWorkMenu
        With CommandBars(1).FindControl(ID:=30100).Controls  'Work menu
            With .Add(Type:=msoControlButton, Before:=2, temporary:=True)
                .Caption = "Delete item from menu"
                .Tag = "jemWdWorkMenuDeleteItem"
                .Style = msoButtonCaption
                .OnAction = "DeleteWorkMenuItems"
            End With
        End With
    End Sub
    
    Private Sub RemoveDeleteItemFromWorkMenu()
        On Error Resume Next
        CommandBars.FindControl(Tag:="jemWdWorkMenuDeleteItem").Delete
        On Error GoTo 0
    End Sub
      

 

Return to Top

Storing and retrieving Work menu items in a text file

Word stores your Work menu items in the ~/Library/Preferences/Microsoft/com.microsoft.Word.prefs.plist file, where ~ is your home folder. If this file ever gets corrupted or you have to run Remove Office, you'll lose your Work menu items. One way to preserve them is to store the items in a safe place. I choose to use an add-in to store the items in a text file store in the ~/Library/Application Support/Microsoft Office/Word folder. This folder isn't created by Word or Office (it should be, but that's a different story), so the add-in creates the folder if it doesn't already exist.

Note that, once you create your text file, you can edit the file directly using any text editor, to add or delete Work menu items.

Screen shot - Folder

Store Work menu items in a text file

This macro stores the Work menu items in a text file and deletes them from the menu:

    Public Sub UnloadWorkMenuToTextFile()
        Dim nFileNum As Long
        Dim i As Long
        Dim sPathSep As String
        Dim sTextFilePath As String
        Dim sTextFileFullName As String
        sPathSep = Application.PathSeparator
        sTextFilePath = PrepSupportFolder
        If Len(sTextFilePath) > 0 Then
            sTextFileFullName = sTextFilePath & sPathSep & "jemWdWorkMenu.txt"
            nFileNum = FreeFile
            If Dir(sTextFileFullName) <> "" Then Kill sTextFileFullName
            Open sTextFileFullName For Output As #nFileNum
            For i = WorkMenuItems.Count To 1 Step -1
                With WorkMenuItems(i)
                    Write #nFileNum, .Path & sPathSep & .Name
                    .Delete
                End With
            Next i
            Close #nFileNum
        End If
    End Sub
      

This macro checks to see if the storage folder exists. As written it's designed only for MacWord v.X or 2004, but it could be modified for earlier versions of MacWord or WinWord:

    Public Function PrepSupportFolder() As String
        Dim sPathSep As String
        Dim sPrepSupportPath As String
        sPathSep = Application.PathSeparator
        #If Mac Then
            If Application.Version >= 10 Then
                sPrepSupportPath = Options.DefaultFilePath(wdUserOptionsPath)
                If Len(Dir(sPrepSupportPath, vbDirectory)) = 0 Then _
                    sPrepSupportPath = UCase(Left(sPrepSupportPath, 1)) & _
                    Mid(sPrepSupportPath, 2)
                sPrepSupportPath = Left(sPrepSupportPath, _
                    InStr(sPrepSupportPath, "preferences") - 1) & "Application Support"
                If Len(Dir(sPrepSupportPath, vbDirectory)) = 0 Then _
                    MkDir sPrepSupportPath
                sPrepSupportPath = sPrepSupportPath & sPathSep & "Microsoft Office"
                If Len(Dir(sPrepSupportPath, vbDirectory)) = 0 Then _
                    MkDir sPrepSupportPath
                sPrepSupportPath = sPrepSupportPath & sPathSep & "Word"
                If Len(Dir(sPrepSupportPath, vbDirectory)) = 0 Then _
                    MkDir sPrepSupportPath
            End If
        #End If
        If Len(Dir(sPrepSupportPath, vbDirectory)) > 0 Then _
            PrepSupportFolder = sPrepSupportPath
    End Function

Restore Work menu items from a text file

Of course, you'll want to retrieve the items when the add-in is loaded:

    Public Sub LoadWorkMenuFromTextFile()
        Dim nFileNum As Long
        Dim sInput As String
        Dim sTextFilePath As String
        Dim sTextFileFullName As String
        sTextFilePath = GetSupportFolder
        If Len(sTextFilePath) > 0 Then
            sTextFileFullName = sTextFilePath & _
                Application.PathSeparator & "jemWdWorkMenu.txt"
            nFileNum = FreeFile
            If Len(Dir(sTextFileFullName)) > 0 Then
                Open sTextFileFullName For Input As #nFileNum
                Do While Not EOF(nFileNum)
                    Input #nFileNum, sInput
                    If Dir(sInput) <> "" Then WorkMenuItems.Add sInput
                Loop
                Close #nFileNum
            End If
        End If
    End Sub
    Public Function GetSupportFolder() As String
        Dim sPathSep As String
        Dim sSupportPath As String
        sPathSep = Application.PathSeparator
        #If Mac Then
            If Application.Version >= 10 Then
                sSupportPath = Options.DefaultFilePath(wdUserOptionsPath)
                If Len(Dir(sSupportPath, vbDirectory)) = 0 Then _
                    sSupportPath = UCase(Left(sSupportPath, 1)) & _
                    Mid(sSupportPath, 2)
                sSupportPath = Left(sSupportPath, _
                    InStr(sSupportPath, "preferences") - 1) & "Application Support" _
                    & sPathSep & "Microsoft Office" & sPathSep & "Word"
            End If
        #End If
        If Len(Dir(sSupportPath, vbDirectory)) > 0 Then _
            GetSupportFolder = sSupportPath
    End Function
      

Automating storing and restoring Work menu items

To automate the above macros in an add-in, the following macros are called when the add-in is loaded and unloaded:

    Public Sub AutoExec()
        LoadWorkMenuFromTextFile
    End Sub

    Public Sub AutoClose()
        UnloadWorkMenuToTextFile
    End Sub
      

Automatically add a file to the Work menu

This technique is useful for ensuring that an add-in (or other file) is automatically added to the Work menu. Put this in a regular code module in the add-in:

    Public Sub AutoExec()
        WorkMenuItems.Add ThisDocument
    End Sub
      

or regular document:

    Public Sub AutoOpen()
        WorkMenuItems.Add ThisDocument
    End Sub
      

To then automatically unload the add-in/file from the Work menu:

    Public Sub AutoExit()
        Dim i As Long
        For i = 1 To WorkMenuItems.Count
            With WorkMenuItems(i)
                If ThisDocument.FullName = .Path & _
                   Application.PathSeparator & .Name Then _
                        .Delete
            End With
        Next i
    End Sub

Return to Top