Monday, April 12, 2010

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 :)

No comments:

Post a Comment