How to find/replace "1st" with 1st with the "st" subscripted?

S

sly007

Hi,

I want to modify my existing macro and so I can search all occurences of
1st, 2nd, 3rd and replace them with 1st, 2nd and 3rd but only with the st, nd
and rd subscripted. How would I do that?

Thank you very much for your help.
 
G

Graham Mayor

We'll have to guess about the rest of the macro, but for the matter in hand

Dim oRng As Range
Selection.HomeKey wdStory
Selection.Find.ClearFormatting
With Selection.Find
Do While .Execute(findText:="[0-9][dhnrst]{2}", _
MatchWildcards:=True, _
Wrap:=wdFindStop, Forward:=True)
Set oRng = Selection.Range
oRng.Start = oRng.Start + 1
'oRng.Font.Superscript = True
oRng.Font.Subscript = True
Loop
End With

will work, though I suspect you meant 'superscript' which is the usual
format for ordinals.

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
G

Greg Maxey

I would be hard to say how you would modify your existing macro without
first seeing that macro. If the issue at hand is only processing text in
the main text story of your document you might consider using AutoForma.
You don't need macro for that, but if you wanted to call a macro in your
current macro that would do the deed, you could use this.

Sub FormatOrdinals()
Dim bVal As Boolean
bVal = Application.Options.AutoFormatReplaceOrdinals
Application.Options.AutoFormatReplaceOrdinals = True
ActiveDocument.Range.AutoFormat
Application.Options.AutoFormatReplaceOrdinals = bVal
End Sub
 
G

Graham Mayor

On reflection

With Options
.AutoFormatApplyHeadings = False
.AutoFormatApplyLists = False
.AutoFormatApplyBulletedLists = False
.AutoFormatApplyOtherParas = False
.AutoFormatReplaceQuotes = False
.AutoFormatReplaceSymbols = False
.AutoFormatReplaceOrdinals = True
.AutoFormatReplaceFractions = False
.AutoFormatReplacePlainTextEmphasis = False
.AutoFormatReplaceHyperlinks = False
.AutoFormatPreserveStyles = False
.AutoFormatPlainTextWordMail = False
End With
Selection.Range.AutoFormat

should work also.


--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>



Graham said:
We'll have to guess about the rest of the macro, but for the matter
in hand
Dim oRng As Range
Selection.HomeKey wdStory
Selection.Find.ClearFormatting
With Selection.Find
Do While .Execute(findText:="[0-9][dhnrst]{2}", _
MatchWildcards:=True, _
Wrap:=wdFindStop, Forward:=True)
Set oRng = Selection.Range
oRng.Start = oRng.Start + 1
'oRng.Font.Superscript = True
oRng.Font.Subscript = True
Loop
End With

will work, though I suspect you meant 'superscript' which is the usual
format for ordinals.

Hi,

I want to modify my existing macro and so I can search all occurences
of 1st, 2nd, 3rd and replace them with 1st, 2nd and 3rd but only with
the st, nd and rd subscripted. How would I do that?

Thank you very much for your help.
 
G

Greg Maxey

Graham,

I like your search string and I incorporated it into some code to change all
or selective change numbers. I am also posting some code for converting
plain numbers to ordinals:

Sub FormatOrdinalWithReview()
Dim oRng As Word.Range
Dim i As Long
Dim bNoReview As Boolean
bNoReview = True
If MsgBox("Do you want to review items before making changes?", vbQuestion +
vbYesNo, "Review") = vbYes Then
bNoReview = False
End If
i = ActiveDocument.Sections(1).Headers(1).Range.StoryType
For Each oRng In ActiveDocument.StoryRanges
Do
With oRng.Find
.Text = "[0-9][dhnrst]{2}"
.MatchWildcards = True
.Wrap = wdFindStop
.Forward = True
While .Execute
If bNoReview Then
oRng.Start = oRng.Start + 1
oRng.Font.Superscript = True
oRng.Collapse wdCollapseEnd
Else
oRng.Select
If MsgBox("Do you want to change this number", vbQuestion +
vbYesNo, "Change Number") = vbYes Then
oRng.Start = oRng.Start + 1
oRng.Font.Superscript = True
End If
oRng.Collapse wdCollapseEnd
End If
Wend
Set oRng = oRng.NextStoryRange
End With
Loop Until oRng Is Nothing
Next oRng
End Sub

Sub MakePlainNumberOrdinalNumbers()
Dim oRng As Word.Range
Dim i As Long
Dim x As Long
Dim bNoReview As Boolean
Dim bNotSpecial As Boolean
Dim pShortStr As String
bNoReview = True
If MsgBox("Do you want to review numbers before making changes?", vbQuestion
+ vbYesNo, "Review") = vbYes Then
bNoReview = False
End If
i = ActiveDocument.Sections(1).Headers(1).Range.StoryType
For Each oRng In ActiveDocument.StoryRanges
Do
With oRng.Find
.Text = "<[0-9]@>"
.MatchWildcards = True
.Wrap = wdFindStop
.Forward = True
While .Execute
bNotSpecial = True
If bNoReview Then
x = Len(oRng.Text)
If x = 2 Then
pShortStr = Right(oRng.Text, 2)
If pShortStr = "11" Or pShortStr = "12" Or pShortStr = "13" Then
ProcessoRng oRng, "th", 2
bNotSpecial = False
End If
End If
If bNotSpecial Then
Select Case oRng.Characters.Last.Text
Case Is = 1
ProcessoRng oRng, "st", x
Case Is = 2
ProcessoRng oRng, "nd", x
Case Is = 3
ProcessoRng oRng, "rd", x
Case Else
ProcessoRng oRng, "th", x
End Select
End If
Else
oRng.Select
If MsgBox("Do you want to change this number", vbQuestion +
vbYesNo, "Change Number") = vbYes Then
x = Len(oRng.Text)
If x = 2 Then
pShortStr = Right(oRng.Text, 2)
If pShortStr = "11" Or pShortStr = "12" Or pShortStr = "13"
Then
ProcessoRng oRng, "th", 2
bNotSpecial = False
End If
End If
If bNotSpecial Then
Select Case oRng.Characters.Last.Text
Case Is = 1
ProcessoRng oRng, "st", x
Case Is = 2
ProcessoRng oRng, "nd", x
Case Is = 3
ProcessoRng oRng, "rd", x
Case Else
ProcessoRng oRng, "th", x
End Select
End If
End If
oRng.Collapse wdCollapseEnd
End If
Wend
Set oRng = oRng.NextStoryRange
End With
Loop Until oRng Is Nothing
Next oRng
End Sub

Sub ProcessoRng(ByRef oRange As Range, pStr As String, i As Long)
With oRange
.Text = oRange.Text & pStr
.MoveStart wdCharacter, i
.Font.Superscript = True
.Collapse wdCollapseEnd
End With
End Sub


Graham said:
We'll have to guess about the rest of the macro, but for the matter
in hand
Dim oRng As Range
Selection.HomeKey wdStory
Selection.Find.ClearFormatting
With Selection.Find
Do While .Execute(findText:="[0-9][dhnrst]{2}", _
MatchWildcards:=True, _
Wrap:=wdFindStop, Forward:=True)
Set oRng = Selection.Range
oRng.Start = oRng.Start + 1
'oRng.Font.Superscript = True
oRng.Font.Subscript = True
Loop
End With

will work, though I suspect you meant 'superscript' which is the usual
format for ordinals.

Hi,

I want to modify my existing macro and so I can search all occurences
of 1st, 2nd, 3rd and replace them with 1st, 2nd and 3rd but only with
the st, nd and rd subscripted. How would I do that?

Thank you very much for your help.
 
S

sly007

First of all thank you very for your answer but it's not exactly what I'm
looking for.

Let me rephrase my request.

My macro is entirely based on find and replace. Let's say I want to insert a
portion to find all occurences of "Mme" (short for Madame in french) and want
to replace them with "Mme" but with the 2nd and 3rd characters superscripted,
what would be the find and replace using wildcards?

Example:
Selection.LanguageID = wdFrenchCanadian
Selection.NoProofing = True
Application.CheckLanguage = True
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = " {2;}"
.Replacement.Text = " "
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True
End With
Selection.Find.Execute Replace:=wdReplaceAll
With Selection.Find
.Text = "([0-9]) ([0-9])"
.Replacement.Text = "\1^s\2"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = True
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
ETC....


Thank you!




--
sly007


Graham Mayor said:
We'll have to guess about the rest of the macro, but for the matter in hand

Dim oRng As Range
Selection.HomeKey wdStory
Selection.Find.ClearFormatting
With Selection.Find
Do While .Execute(findText:="[0-9][dhnrst]{2}", _
MatchWildcards:=True, _
Wrap:=wdFindStop, Forward:=True)
Set oRng = Selection.Range
oRng.Start = oRng.Start + 1
'oRng.Font.Superscript = True
oRng.Font.Subscript = True
Loop
End With

will work, though I suspect you meant 'superscript' which is the usual
format for ordinals.

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>


Hi,

I want to modify my existing macro and so I can search all occurences
of 1st, 2nd, 3rd and replace them with 1st, 2nd and 3rd but only with
the st, nd and rd subscripted. How would I do that?

Thank you very much for your help.
 
G

Graham Mayor

Ther replies were exactly what you asked for, however it would not be too
difficult to modify the code I posted earlier to find Mme in addition to
ordinal numbers eg

Dim oRng As Range
Dim vFindText As Variant
vFindText = Array("[0-9][dhnrst]{2}", "Mme")
For i = 0 To UBound(vFindText)
Selection.HomeKey wdStory
Selection.Find.ClearFormatting
With Selection.Find
Do While .Execute(findText:=vFindText(i), _
MatchWildcards:=True, _
Wrap:=wdFindStop, Forward:=True)
Set oRng = Selection.Range
oRng.Start = oRng.Start + 1
oRng.Font.Superscript = True
Loop
End With
Next i

The macro performs separate searches for the ordinal numbers and Mme.

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>

First of all thank you very for your answer but it's not exactly what
I'm looking for.

Let me rephrase my request.

My macro is entirely based on find and replace. Let's say I want to
insert a portion to find all occurences of "Mme" (short for Madame in
french) and want to replace them with "Mme" but with the 2nd and 3rd
characters superscripted, what would be the find and replace using
wildcards?

Example:
Selection.LanguageID = wdFrenchCanadian
Selection.NoProofing = True
Application.CheckLanguage = True
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = " {2;}"
.Replacement.Text = " "
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True
End With
Selection.Find.Execute Replace:=wdReplaceAll
With Selection.Find
.Text = "([0-9]) ([0-9])"
.Replacement.Text = "\1^s\2"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = True
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
ETC....


Thank you!




We'll have to guess about the rest of the macro, but for the matter
in hand

Dim oRng As Range
Selection.HomeKey wdStory
Selection.Find.ClearFormatting
With Selection.Find
Do While .Execute(findText:="[0-9][dhnrst]{2}", _
MatchWildcards:=True, _
Wrap:=wdFindStop, Forward:=True)
Set oRng = Selection.Range
oRng.Start = oRng.Start + 1
'oRng.Font.Superscript = True
oRng.Font.Subscript = True
Loop
End With

will work, though I suspect you meant 'superscript' which is the
usual format for ordinals.

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>


Hi,

I want to modify my existing macro and so I can search all
occurences of 1st, 2nd, 3rd and replace them with 1st, 2nd and 3rd
but only with the st, nd and rd subscripted. How would I do that?

Thank you very much for your help.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top