Wildcards and macros in Microsoft Word

ICC 2011 workshop handout

Prerequisites:

  • Basic computer skills (working with files and directories),
  • Simple text editing
  • Basic usage of Microsoft Word (Find, Find and replace dialogs,...)

Aim of the workshop:

After the completion of this workshop participants will:

  • Understand the principle of wildcards
  • Be able to define simple (possibly even more complex) find patterns
  • Be able to perform find and replace operations using wildcards
  • Be able to record and use simple macros

Workshop plan:

  1. Who is who (getting to know each other),
  2. What is what (description of working environment we will use during the workshop)
  3. What are wildcards?
  4. Macros
  5. Exercises

Details:

1. Who is who (getting to know each other),

2. What is what

Our primary tools in this workshop will be Find and Find and replace dialogs in Microsoft Word. Use ctrl+f to open the Find dialog, and ctrl+h to open the Find and replace dialog. You can, of course, use menu to find particular items.

Office 2003:

  • use menu "Edit"

office 2010:

Use F6 to switch between different parts of the main window. There is a navigation pane containing a search box and a "search" split button which opens a submenu with more items related to Find and replace. Make sure that the navigation pane is enabled (see the tab “View” on the ribbon). Here are a few words about using the ribbon:

  • Press alt to set focus on the ribbon
  • Use the left and right arrows to switch between tabs and find some buttons
  • Press ctrl+F1 to maximize the ribbon. Make sure the ribbon is maximized. (Go to the ribbon and use the left and right arrows to find the button "Minimize the ribbon". Ensure that this button is not pressed, jaws and nvda will announce "pressed", if the button is pressed.)
  • Press space or enter to (de)activate the button (e.g. items "File", "Help", and "Minimize the ribbon" are buttons)
  • Use tab and shift+tab to move between items on a particular tab (these combinations work only on the maximized ribbon. If you prefer the ribbon to be minimized, press space or enter on a particular tab and then use tab/ shift+tab)
  • Items on a tab are organized into groups. Use ctrl+left and right arrows to move between groups on a particular tab
  • Use space or enter to activate particular item on a tab

If you want to use wildcards, you have to enable them. Do the following to enable them:

  • open Find or Find and replace dialog,
  • press "More" button to show advanced search options
  • check the "Use wildcards" checkbox

3. What are wildcards?

Wildcards are special characters which can be used to describe text by a pattern. These patterns are called expressions. Examples:

We are looking for all three-digit numbers (123, 543, 553) and describe them by an expression as follows:

  • find a string which starts by a non-numeric character followed by three consecutive numeric characters and finally again a non-numeric character

Another trivial example is to find 2 or more consecutive spaces and replace them by one space:

  • find a string which starts by a space and the space character repeats 2 or more times and replace this string with one space

Finally let's think about something more complicated. Find 2 consecutive words which both start with a capital letter and so they are probably the first name and surname of a person and try to change the order of these words:

  • find a string which starts by a capital letter, then there are some letter characters followed by a space and another word which starts by a capital letter followed by other letter characters. Remember the two words as separate "boxes" named 1 and 2. Replace this string by a string 2 1 (we used names of our boxes containing found words.)

Note: all examples in this text are enclosed in quotes. The quotes are here only to clarify the boundaries of an example. Don't type them into Find and replace dialogs.

3.1 Basic letters

The simplest expressions can be constructed from standard letters and numbers. You use this kind of expressions in all standard searches or replacements. Some non-letter characters have special meaning and if you want to find them, you have to prefix them by the \ character, e.g. the symbol "*" has a special meaning (is a wildcard) and if you want to find the * character, you have to type "\*" into Find dialog. There are many symbols with special meaning. We are going to describe them below.

3.2 One character

wildcard: ?

description: Question mark can be used to represent one "unknown" character. Some examples follow:

  • "b?te": represents strings like bite, byte, bate, bete. Notice that we use the word "strings" not "words". The word "string" is more suitable because the expression in this example matches also strings like "b te"
  • " ??? ": this expression matches all three-character string with a space before and after the word, e.g. " dog ", " cat ", ...
  • "l?c?": matches four-character strings with l on the first and c on the third position. E.g. "lack" "lect" ...

exercises

  1. write an expression which matches all 4-character strings with the letter "b" on the second and the letter "c" on the fourth position.

3.3. group of characters

wildcard: []

how to type on English keyboard: use two nearest keys to the right of the letter p.

description: this wildcard enables you to reduce the "?" symbol to a set of characters. The "[]" construct can contain a group of characters and/or a range defined by the "-" symbol. You can negate the group by the "!" character. The "!" character has the special meaning only if it is the first character immediately after the left bracket.

See the following examples:

  • "b[iy]te": matches strings bite or byte
  • " [A-Z][a-z][a-z][a-z] ": matches all 6-character strings comprising a space followed by a capital letter, then three lower case letters and another space. This expression describes e.g. 4-letter names somewhere in the middle of a paragraph, (the first and the last word of the paragraph would not be matched because the space character is expected at the beginning and the end of a matched string. Can you think of any other 4-letter words with a capital first letter which would not be matched by our expression?)
  • "b[! 0-9]te": describes all strings beginning with the letter b followed by a symbol which is not a space nor a number and followed by letters "te". (byte, bite, b.te,...)

exercises

  1. write an expression which matches descriptions of chess moves in the form a1:b2, A8:A6 ... . The chessboard has 8 lines and 8 columns. Columns are marked by the letters a to h and rows are numbered 1 to 8. White moves are typed with capital letters and black moves are lower case ones. Use only one expression and suppose that markings are correct (a1:A2 is incorrect but don't pay attention to filtering these incorrect markings)

3.4 word delimiters

wildcard: <>

how to type on English keyboard: shift+, and shift+.

description: symbols "<" and ">" are used to match the beginning and the end of a word. Example:

  • "<dog>": finds the word dog, not letters dog as a part of another (longer) word

Now we can rewrite the example where we have used spaces at the beginning and end of expressions to find words including ones at the beginnings and ends of paragraphs.

  • "<[A-Z][a-z][a-z][a-z]>": now we are able to find names at the beginnings and ends of paragraphs.

3.5 arbitrary string

wildcard: *

how to type on the English keyboard: shift+8

description: this wildcard matches an arbitrary string with an arbitrary size. Matched strings are as short as possible. See examples:

  • "\(*\)": this expression matches all texts in brackets (). Notice that brackets "(" and ")" are preceded by backslashes because these characters have a special meaning (are wildcards) which is to be described later.
  • "[A-Z]*[.\!\?]": this expression matches a string which starts by a capital letter and is terminated with a period, exclamation mark, or question mark. Some symbols in the [] are again wildcards and so backslashed.

exercises

  • Imagine that you want to highlight a very long (e.g. 10 pages) part of a document. You can do it as follows:
    1. scroll to the beginning of the text and type any unusual character (e.g. `)
    2. scroll to the end of the text you want to select and type the unusual character again
    3. open Find dialog, enable wildcards and create a pattern which matches any text between 2 special symbols.

3.6 repeating

wildcard: {}

how to type on the English keyboard: shifted [] keys

Description: this construct gives you the possibility to define the length of a string to be matched. More precisely, you can define how many times the pattern followed by {} construct can be repeated. The number of repeats is defined inside the curly brackets as follows:

  • "{a,b}": the pattern before the curly brackets can be applied minimally a and maximally b times in searching the text.
  • "{a,}": the pattern before the curly brackets is to be applied at least a times.
  • "{,b}": the pattern before the curly brackets can be applied at most b times in the search.
  • "{n}": the pattern before the curly brackets must be applied exactly n times in the search.

Hmm, the definition above might not be very clear. Let's try to describe the principle by a few examples:

  • "<?{5}>": find all 5-letter words. The expression starts with the beginning of the word boundary followed by a question mark which represents one character. After the "?" there is a specification in curly brackets which says that the word must be exactly 5 characters long.
  • "<[0-9]{6,8}>": this expression matches all numbers which are 6 to 8 digits long.

Note that the {} operator is greedy, which means that a matched string is as long as possible. Try to play with it. Create a file with 10 letters a and try to find a pattern "a{1,}".

exercises

  • Delete all consecutive spaces in the document. Use Find and replace to do it. Create a pattern which matches chains of spaces and replace them by one space.
  • write an expression which matches all email addresses. The address will consist of a string which contains letters of English alphabet and symbols "._-" followed by the @ symbol and again a string made of ingredients equal to the string before the @ symbol.

3.7 remembering parts of matched strings

wildcard: ()

description: you can use brackets to encapsulate a part of a pattern. A string matched by the bracketed part of the pattern is saved in a "named box" and you can use the contents of this box in the find or the replace string. You can use 9 different boxes. They are named by numbers 1 to 9 and you can refer to them by "\x" where x is a number 1 to 9. Nested boxing unfortunately does not work.

This is a very powerful part of wildcards. Let's try to think about a few examples:

  • "(<*>) \1": this expression matches a string containing one word 2 times, e.g. "hallo hallo". Try to create a text which will contain one word 2 times and play with this example. You can use Find and replace to delete the second copy of that word (how to do it?). You can, of course, define a less restrictive pattern in () and so match e.g. duplicate sentences, paragraphs, pages,...
  • "<([0-9]{1,})([0-9]{2})dkg>": this expression matches all three- or more-digit numbers followed by the letters dkg. The first digits are stored in the "box" \1 and last two digits are stored in another "box" \2. We can use Find and replace to convert all weights in dkg to weights in kg. Replace string: "\1,\2kg". This example works on three or more digit numbers.

exercises

  • find all 5-letter palindromes in the text. A palindrome is a string, which can be read from left to right and from right to left with the same meaning, e.g. civic, radar, level, rotor, kayak, reviver, racecar, and redder.
  • find all 2 consecutive words starting by a capital letter (e.g. Sinatra Frank) and change the order of these words (Frank Sinatra). We may suppose that 2 consecutive words beginning with capitals are a surname and a first name.

3.8 Other simple wildcards

  • ^t : tabulator
  • ^l : hard line break
  • ^13 : paragraph break. If you want to insert a paragraph break into the Replace field use ^p.
  • ^m : page/section break

4. macros

Macros are recorded or written sequences of word commands. We are going to explore how to record and manage simple macros. More complex macros can be written in Visual Basic which is out of the scope of this workshop.

4.1 required tools

Office 2003:

all necessary tools are in menu Tools/Macro

Office 2010:

All required tools in Office 2010 are on a tab "Developer" which is disabled by default. We can enable this tab as follows:

  • activate the ribbon (press alt)
  • press context menu and activate the item named "Customize the ribbon"
  • choose item "Customize ribbon" in categories (probably done if you activated this dialog as described above)
  • choose item "All tabs" in the list of groups of commands
  • select the tab named "Developer" in "Available commands" list and hit the button Add
  • press Ok

After you enabled the "Developer" tab, you can find all things related to macros in the "Code" group.

4.2 recording a macro

Let's try to record a simple macro which (after pressing a shortcut) swaps the line with the cursor with the next line of text. This macro is not very useful, but demonstrates the principle:

  • type three lines of text (e.g. line1, line2, line3) and move the cursor to the first one

  • go to the menu or tab (depends on your version of MS Office) and press the button "Record macro”

  • name the macro (don't use spaces) e.g. "swap_lines"

  • choose where to store the macro: if you choose (all documents) "normal.dotm", your macro will be saved in a default template and so accessible in all opened documents. You can store it also into the currently opened document which is now a better alternative for us.

  • fill-in some description if you want

  • press the "Keyboard" button and define the hotkey. Set focus on the edit named "Press new shortcut key" and hit e.g. alt+ctrl+down arrow. Save it into the currently opened document.

  • press "Assign" button

  • press "Close" button A yeah. Recording is in progress and now we do operations we want to record. Be aware that all operations will be recorded, so think before each step. We want to record operations necessary to swap 2 lines. So do the following:

  • press down arrow (go one line down)

  • press home (make sure that the cursor is at the beginning of the line)

  • press shift+down arrow to select the line

  • press ctrl+x to cut the line to the clipboard. The cursor is now on the originally third and now the second line

  • press up arrow to go to the first line

  • press home to ensure that the cursor is at the beginning of the line

  • press the ctrl+v to paste the line from the clipboard.

The lines are swapped and we stop recording. Do it again on a Developer tab or Tools/Macro by pressing "Stop recording" button.

Try to play with the newly created macro. Make sure that your macro is working.

You probably see that recording of new macros is really easy. You can create your own macros e.g. to open frequently used documents, find frequently searched patterns and so on.

If you want to manage macros (delete, rename,...), activate the item "Macros" in group Macros on a Developer tab. The dialogue is simple and self-explanatory.

5. exercises

These 2 exercises are a little bit complicated. If you like funny things then continue reading.

5.1 secret in the chaos :)

File secret.txt contains a secret. You might want to know what is inside the chaos. Well, try to find it. You know something about wildcards which is enough. Happy guessing!!! Letters of English alphabet and are important.

5.2 Hanoi towers

Create a document which simulates a slightly modified game of Hanoi towers. The discs of the game are substituted by letters. Your final product should look and work as follows:

  • create a document named icc_hanoi.rtf
  • when a user opens the document there will be one line of text:
    • (icc camp,,)
    • Notice 2 comas at the end of the line. These comas represent empty rods. Brackets at the beginning and the end of the line are here to make things simpler. Delete them only if you really know what you are doing. :)
  • there are hotkeys defined in the document which allow you to move the last letter from a particular rod to another rod.
  • you can think about your hotkeys, here is my suggestion:
    • define hotkeys ctrl+space and the characters ,.jlui one by one (we will use the rows m,. jkl uio)
    • if you want to move a letter from rod 1 to rod 2, press the second character in the first row (,)
    • if you want to move a letter from rod 1 to rod 3, press the third character in the first row (.)
    • letter j is the first in the second row and moves a letter from the second to the first rod
    • letter l is the third in the second row and moves letters from the second to the third rod
    • letter u is the first letter in the third row and moves letters from the third to the first rod
    • and finally i (the second letter in the third row) moves letters from the third to the second rod.
  • the task of the player is probably clear. Use defined hotkeys to move words "icc camp" from rod 1 to rod 2.
  • here is the demo which shows how the line “(icc camp,,)” changes after pressing hotkeys
    • start of the game:(icc camp,,)
    • ctrl+space and ,:(icc cam,p,)
    • ctrl+space and .:(icc ca,p,m)
    • ctrl+space and l:(icc ca,,mp)
    • ctrl+space and ,:(icc c,a,mp)
    • ctrl+space and u:(icc cp,a,m)
    • ctrl+space and i:(icc cp,am,)
    • ctrl+space and ,:(icc c,amp,)
    • ...
    • final (winning) state:(,icc camp,)

notes:

  • define 6 macros which make simple find and replace operations and one macro (e.g. ctrl+space and enter) which can be used to test the final state of the game. If the user activates this seventh macro, the text (,icc camp,) changes to "Congrats!!! You are done!!!"
  • make sure that macros and hotkeys will be saved in the document, not in the default document template
  • use wildcards and make correct expressions
  • if you make things clever enough, you can have more lines in the document and play more games in parallel :))

think about a possibility to add also lines with explanation of the game which is not hard to do, e.g. the following example:

Hello player,
This is a modified version of Hanoi towers. There are three lines which will be modified when you press correct hotkeys.
hotkeys:
...
and now the lines:
(icc camp,,)
(abcdefgh,,)
(12345678,,)