How to replace one last character to superscript?

A

avkokin

Hello.
There is document which has some such words as m2, m3 with different
case. I need to change these numbers to superscript. I use my macro
but I get the cycle. Why and how to repair it? Thank you.

Sub M2()
Dim rng As Range
Set rng = ActiveDocument.Range
With rng.Find
.ClearFormatting
.Replacement.ClearFormatting
.Forward = True
.MatchWildcards = True
.Wrap = wdFindContinue
.Text = "[M-Í][1-9]{1}"
.Execute
While .Found
Selection.MoveRight wdCharacter, 1, wdExtend
Selection.Font.Superscript = True
.Execute
Wend
End With
End Sub
 
H

Helmut Weber

Hi Anton,

without covering all details,
you are mixing range and selection.
As long as e.g. m2 is found in the range "rng",
the selection is extended.
As found is always true, if you don't redefine
the range after found,
it stays the activedocument's range and the
next search is true again, ad infinitum.

By the way, if you define a new range object,
a new find object, it's properties
are set to default values, so
.ClearFormatting
.Replacement.ClearFormatting
.Forward = True
and others can be omitted.

Sub M2a()
Dim rng As Range
Set rng = ActiveDocument.Range
With rng.Find
.MatchWildcards = True
.Text = "[A-Z][1-9]{1}"
.MatchCase = False
While .Execute
' rng.Select ' for testing
rng.Characters.Last.Font.Superscript = True
rng.Collapse Direction:=wdCollapseEnd
Wend
End With
End Sub

--

Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Vista Small Business, Office XP
 
K

Klaus Linke

avkokin said:
There is document which has some such words as m2, m3 with different
< case. I need to change these numbers to superscript. I use my macro
but I get the cycle. Why and how to repair it? Thank you.

You could also replace with m² and m³.
I prefer the symbols, because superscript formatting is easily removed by
accident.

Regards,
Klaus
 

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