![]() |
|
|||||||
|
|
|
Thread Tools | Display Modes |
|
|
|
#1
|
||||
|
||||
|
You can either ignore the error and then test for
strPrevChar is Nothing or test if Selection.Range.Start > 0 Rather than testing every character that way, it is more efficient to ensure your range won't throw that error before the loop begins. Code:
Sub wtf()
Dim rng As Range
Set rng = Selection.Range
If rng.Start = 0 Then rng.MoveStart Unit:=wdCharacter, Count:=1
If rng.End = ActiveDocument.Range.End Then rng.MoveEnd Unit:=wdCharacter, Count:=-1
For Each strChar In rng.Characters
Set strPrevChar = strChar.Previous(wdCharacter, 1)
Set strNextChar = strChar.Next(wdCharacter, 1)
If strPrevChar <> "!" Then
' Perform the operation
End If
Next strChar
End Sub
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
|
#2
|
|||
|
|||
|
Thanks--
Follow-up question: why "declare" the rng variable rather than just always use Selection.Range? (1) It seems like extra code and (2) it actually seems to break the script. For example, if I do this, Code:
With Selection.Range.Find ' Get rid of any stricken-through text
.ClearFormatting 'Is this needed?
.Replacement.ClearFormatting 'Is this needed?
.Text = "*"
.MatchWildcards = True
.Font.StrikeThrough = True
.Replacement.Text = ""
.Execute Replace:=wdReplaceAll
End With
With Selection.Range.Find ' Get rid of any underlining
.Font.Underline = wdUnderlineSingle
.Replacement.Font.Underline = wdUnderlineNone
.MatchWildcards = False
.Text = ""
.Replacement.Text = ""
.Execute Replace:=wdReplaceAll
End With
Code:
Dim rng As Range
Set rng = Selection.Range
With rng.Find ' Get rid of any stricken-through text
.ClearFormatting 'Is this needed?
.Replacement.ClearFormatting 'Is this needed?
.Text = "*"
.MatchWildcards = True
.Font.StrikeThrough = True
.Replacement.Text = ""
.Execute Replace:=wdReplaceAll
End With
With rng.Find ' Get rid of any underlining
.Font.Underline = wdUnderlineSingle
.Replacement.Font.Underline = wdUnderlineNone
.MatchWildcards = False
.Text = ""
.Replacement.Text = ""
.Execute Replace:=wdReplaceAll
End With
|
|
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
Macro to list all character styles in a document
|
ljd108 | Word VBA | 11 | 08-28-2024 01:20 AM |
Replace a random character with the same character
|
RickLegrand | Word | 7 | 07-23-2015 06:35 PM |
How can select from a specific character to another character
|
mohsen.amiri | Word | 2 | 02-19-2015 11:38 PM |
Regular (roman) character style doesn't change text to roman
|
kcbenson | Word | 2 | 10-16-2014 01:31 PM |
Finding or searching ^ character in word document
|
shahin3121 | Word | 2 | 03-05-2012 06:16 PM |