Word: TOC Switches

The Princess Bride (1987)
You only think I guessed wrong! That’s what’s so funny! I switched glasses when your back was turned! Ha ha! You fool! You fell victim to one of the classic blunders – The most famous of which is “never get involved in a land war in Asia” – but only slightly less well-known is this: “Never go against a Sicilian when death is on the line”! Ha ha ha ha ha ha ha! Ha ha ha ha ha ha ha! Ha ha ha… 

Switches are what you use when building a TOC in Word to make it do what you want it to do. For example, { TOC \c “tables” } will make a TOC based on a sequence field identifier.

Here are some basic field command shortcuts:
  • ALT+F9 show/hide all field codes
  • Shift+F9 show/hide current field code
  • Ctrl+F9 insert an empty field
  • Ctrl+a, then F9 updates all fields in the document
The following lists all the available switches (taken from the Microsoft site, see links at the bottom).

\c “SEQIdentifier”

Lists figures, tables, charts, or other items that are numbered by a SEQ (Sequence) field. Word uses SEQ fields to number items captioned with the Caption command (Insert menu, Reference submenu). SEQIdentifier, which corresponds to the caption label, must match the identifier in the SEQ field. For example, { TOC \c “tables” } lists all numbered tables.
\h Hyperlinks Inserts TOC entries as hyperlinks (hyperlink: Colored and underlined text or a graphic that you click to go to a file, a location in a file, a Web page on the World Wide Web, or a Web page on an intranet. Hyperlinks can also go to newsgroups and to Gopher, Telnet, and FTP sites).

\a Identifier Lists items captioned with the Caption command (Insert menu, Reference submenu) but omits caption labels and numbers. The identifier corresponds to the caption label. For example, although a caption on page 12 is “Figure 8: Mercury”, the field { TOC \a figures } displays entries as “Mercury…………12”.Use the \c switch to build a table of captions with labels and numbers.
\b BookmarkName Collects entries only from the portion of the document marked by the specified bookmark (bookmark: A location or selection of text in a file that you name for reference purposes. Bookmarks identify a location within your file that you can later refer or link to.).
\f EntryIdentifier Builds a table from TC fields. If EntryIdentifier is specified, the table is built only from TC fields with the same identifier (typically a letter). For example, { TOC \f t } builds a table of contents from TC fields such as { TC “Entry Text” \f t }.
\l Levels Builds a table of contents from TC fields that assign entries to one of the specified levels. For example, { TOC \l 1-4 } builds a table of contents from TC fields that assign entries to levels 1-4. TC fields that assign entries to lower levels are skipped.
\n Levels Omits page numbers from the table of contents. Page numbers are omitted from all levels unless a range of entry levels is specified. For example, { TOC \n 3-4 } omits page numbers from levels 3 and 4. Delete this switch to include page numbers.
\o “Headings” Builds a table of contents from paragraphs formatted with built-in heading styles (heading style: Formatting applied to a heading. Microsoft Word has nine different built-in styles: Heading 1 through Heading 9.). For example, { TOC \o “1-3” } lists only headings formatted with the styles Heading 1 through Heading 3. If no heading range is specified, all heading levels used in the document are listed. Enclose the range numbers in quotation marks.
\p “Separators” Specifies the characters that separate an entry and its page number. For example, the field { TOC \p “—” }, with an em dash, displays a result such as “Selecting Text—53.” The default is a tab with leader dots. You can use up to five characters, which must be enclosed in quotation marks.
\s Identifier Includes a number such as a chapter number before the page number. The chapter or other item must be numbered with a SEQ field. Identifier must match the identifier in the SEQ field. For example, if you insert { SEQ chapter } before each chapter heading, { TOC \o “1-3” \s chapter } displays page numbers as 2-14, where “2” is the chapter number.
\d “Separator” When used with the \s switch, specifies the number of characters that separate the sequence numbers and page numbers. Enclose the characters in quotation marks. Word uses a hyphen (-) if no \d switch is specified. In the table of contents generated by { TOC \o “1-3” \s chapter \d “:” }, a colon (:) separates chapter numbers and page numbers — for example, “2:14.”
\t “Style,Level, Style,Level,…” Builds a table of contents from paragraphs formatted with styles other than the built-in heading styles. For example, { TOC \t “chaptertitle,1, chapterhead,2” } builds a table of contents from paragraphs formatted with the styles “chaptertitle” and “chapterhead.” The number after each style name indicates the table of contents entry level that corresponds to that style. You can use both the \o switch and the \t switch to build a table of contents from built-in heading styles and other styles.
\u Builds a table of contents by using the applied paragraph outline level (outline level: Paragraph formatting you can use to assign a hierarchical level (Level 1 through Level 9) to paragraphs in your document. For example, after you assign outline levels, you can work with the document in outline view or in the Document Map.).
\w Preserves tab entries within table entries.
\x Preserves newline characters within table entries.
\z Hides tab leader and page numbers in Web layout view (Web Layout view: A view of a document as it will appear in a Web browser. For example, the document appears as one long page (without page breaks) and text and tables wrap to fit in the window.)
Links to Mircosoft info on fields can be found here.
[Source Microsoft]

Update: The link to the Microsoft page above is dead. They now say the following:
“For an explanation of all the switches for TOC fields, click Quick Parts in the Text group on the Insert tab, and then click Field. Select the Index and Tables category and the TOC field name. Then click Field Codes and then Options. The Field Options dialog box lists all the switches with a description of each.”

Too Much Style

Police Officer: You in the red Corvette! Pull over immediately.
Xander Cage: Yeah, yeah. These monkeys are following me because I just took this car. Obviously the car doesn’t belong to me, it’s not my style
If you are overwhelmed by the number of styles in Word 2007, you can configure Word to show only the styles that are currently being used in the document:
  1. Alt + Ctrl + Shift + s  (opens the style box)
  2. Click on Options on the bottom right
  3. Under Select styles to show choose In use

Now, in the panel on the right you only see what is currently being used in the document.  The ribbon at the top still gives you access to all the styles.  If you apply a style that is not currently being used, then it gets added to the panel on right.

You can also use Ctrl + Shift + s to pop up a window where you can type in a style name. This window has auto complete, so just start typing and the closest match to the style name will fill in.

Power to the Point

Have you ever sat through an awful presentation? I think that we all have. Too many slides. Too little time.
Good morning class.  Today we have to move a little fast.  We’ve got 1 hour and 60 slides, so no questions until the end.”
Or, there’s the presenter who crams his whole presentation into the slides.

“Um, excuse me… are you just going to read each slide to me?  It seems like your not adding anything, so can I just have a copy of the slides and I’ll go back to work?”
Here is a nice rule of thumb for Powerpoint presentations (from this blog):

It’s quite simple: a PowerPoint presentation should have ten slides, last no more than twenty minutes, and contain no font smaller than thirty points.

Word: Show Me My Property

Word documents have many user-accessible properties, such as Author, Title, etc.  One can find them by clicking on the Word Button -> Prepare -> Properties.  These properties can also be accessed by a macro using the BuiltInDocumentProperties method.
This post will show you which properties are available and how to access them using macros.

To access individual properties, use this:
Sub showProperty()
    Set myProp = ActiveDocument.BuiltInDocumentProperties(wdPropertyAuthor)
    ‘MsgBox (“Value of this property ->” + myProp + “<-")
End Sub
(I like to enclose my displayed values with some character, like an angle bracket, to make it easy to see any preceding or trailing spaces)
In place of wdPropertyAuthor, you can use any of the following properties:
  • wdPropertyAppName
  • wdPropertyAuthor
  • wdPropertyBytes
  • wdPropertyCategory
  • wdPropertyCharacters
  • wdPropertyCharsWSpaces
  • wdPropertyComments
  • wdPropertyCompany
  • wdPropertyFormat
  • wdPropertyHiddenSlides
  • wdPropertyHyperlinkBase
  • wdPropertyKeywords
  • wdPropertyLastAuthor
  • wdPropertyLines
  • wdPropertyManager
  • wdPropertyMMClips
  • wdPropertyNotes
  • wdPropertyPages
  • wdPropertyParas
  • wdPropertyRevision
  • wdPropertySecurity
  • wdPropertySlides
  • wdPropertySubject
  • wdPropertyTemplate
  • wdPropertyTimeCreated
  • wdPropertyTimeLastPrinted
  • wdPropertyTimeLastSaved
  • wdPropertyTitle
  • wdPropertyVBATotalEdit
  • wdPropertyWords
To list all properties in the document, run this macro.  It will put them at the end of the document.
Sub ListProperties()
    Dim rngDoc As Range
    Dim proDoc As DocumentProperty

    Set rngDoc = ActiveDocument.Content

    rngDoc.Collapse Direction:=wdCollapseEnd

    For Each proDoc In ActiveDocument.BuiltInDocumentProperties
        With rngDoc
            .InsertParagraphAfter
            .InsertAfter proDoc.Name & “= “
            On Error Resume Next
            .InsertAfter proDoc.Value
        End With
    Next
End Sub

Back From the Future

Lou: You gonna order something, kid?
Marty McFly: Ah, yeah… Give me – Give me a Tab.
Lou: Tab? I can’t give you a tab unless you order something.
Marty McFly: All right, give me a Pepsi Free.
Lou: You want a Pepsi, PAL, you’re gonna pay for it.  
Here is a simple tip that can save a lot of time. If you click on a link in a PDF file, how to you go back to where you started?
The answer: Alt + Left arrow
Example: If you are on page 2  and you click a link that takes you to page 155, Alt + left arrow takes you back to page 2.
If you want to go back to the future, uh, I mean back to the page that you went to before, then use Alt + Right arrow.

Preventing Emails with Blank Subject Lines in Outlook

Here is a nice fix to prevent Outlook from sending emails without anything in the subject:
Save yourself the annoyance by following the next steps.
  1. Open the Visual Basic Editor (in Outlook, press ALT+F11).
  2. On the left-hand side of the screen, expand the ‘Project1 (go into ‘Microsoft Office Outlook Objects’ -> ‘ThisOutlookSession’).
  3. Double click on ‘ThisOutlookSession’
  4. Paste the following lines of code in the right-hand pane of the screen:
Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
    If Item.Subject = “” Then
    Cancel = MsgBox(“This message does not have a subject.” & vbNewLine & “Do you wish to continue sending anyway?”, vbYesNo + vbExclamation, “No Subject”) = vbNo
    End If
End Sub

Save the project (‘File’ -> ‘Save’) and return to Outlook. From now on, the code will be called every time you press the send-button after you composed an email. Outlook will pop up a warning when you try sending an email with an empty subject line.

Macros: Moving the Selection

I taught myself how to use macros (through lots of web searching and testing lots of bad code in Word), and therefore, there are some basics that were left out along the way.  Today’s post is about one of those missing pieces: moving the selection.
Once something is selected, the selection can be changed by issuing a command with the following parameters:

  1. Move type
  2. Move unit
  3. Count
  4. Extend 

 “Move type” is one of MoveLeft/Right/Up/Down.  This gives us a direction to use as a reference for the rest of the commands.  The move will start with the end of the selection.
“Move unit” specifies the units for the count parameter.
“Count” specifies how many times we repeat the move.
“Extend” can either be wdExtend or wdMove.  Using wdMove removes the selection (not the text, just the selection highlighting) and places the cursor at the end of the selection.


Now let’s put it all together. I’ll use the example below to demonstrate some of the possibilities. I have some text where there is a special word (Phlogtastic) that I want to change the first five characters to italics (so that it will be Phlogtastic).

Here is the sample macro:

Sub makeFirstPartofWordItalic()
‘ fix the headings that don’t have italics

For Each myWord In ActiveDocument.Words
    If myWord = “Phlogtastic ” Then
        myWord.Select
   
        ‘Option 1: Using MoveLeft
        Selection.MoveLeft Unit:=wdCharacter, Count:=7, Extend:=wdExtend
       
        ‘Option 2: Using MoveRight
        Selection.MoveRight Unit:=wdCharacter, Count:=-7, Extend:=wdExtend
       
        ‘Option 3: Using Combinations
        Selection.MoveLeft Unit:=wdWord, Count:=1
        Selection.MoveRight Unit:=wdCharacter, Count:=5, Extend:=wdExtend

        Selection.Font.Italic = True
    End If
Next myWord

End Sub

The macro searches the text until it finds that word that we want.  Next, I give three  different ways to selection the desired part of the text.  All three options use “Phlogtastic ” as the initial selection (including the trailing space).
Option 1: We start at the end of the selection.  Next, move the end of the selection to the left by 6 characters, resulting in “Phlogtastic “.
Option 2: We start at the end of the selection.  Next, move the end of the selection to the right by negative 6 characters, resulting in “Phlogtastic”.  Note that a negative move to the right is a positive move to the left.

Option 3: Note that in the previous cases we used the wdExtend parameter which changed the what is selected.  In this case, we are going to create a whole new selection.  First, the move done without the wdExtend parameter so the cursor is placed at the end of the selection and then moved.  The move unit is in words instead of characters, so moving to the left by one word give us |Phlogtastic” with the cursor at the beginning of the word (before the ‘P’).  Next, we expand the selection by moving five characters to the right, resulting in Phlogtastic”.  

Lastly, we change the font to italics.

Happy Moving 🙂

Super Copyrights

Today I changed a term in one of my documents to use a fancy, new term which has a copyright.  For some reason (which I’ll ignore for now) when I replaced the term, the copyright symbol was not put into superscript. So, macros to the rescue:

Sub copyrightToSuperscript()

‘ copyrightToSuperscript Macro

‘ Change all copyright symbols to superscript

    Selection.Find.ClearFormatting
    Selection.Find.Replacement.ClearFormatting
    With Selection.Find.Replacement.Font
        .Superscript = True
        .Subscript = False
    End With
    With Selection.Find
        .Text = “©”
        .Replacement.Text = “©”
        .Forward = True
        .Wrap = wdFindContinue
        .Format = True
        .MatchCase = False
        .MatchWholeWord = False
        .MatchKashida = False
        .MatchDiacritics = False
        .MatchAlefHamza = False
        .MatchControl = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With
    Selection.Find.Execute Replace:=wdReplaceAll

End Sub

I originally thought that I would need to use the ASCII code for the symbol, but the macro works just fine as written above.