Once something is selected, the selection can be changed by issuing a command with the following parameters:
- Move type
- Move unit
- Count
- Extend
"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.
Lastly, we change the font to italics.
No comments:
Post a Comment