Convert "13" in all the "13C" to superscript

J

John Smith

How do I convert the "13" in all the "13C" in a document to superscript?
"13C" is a word, i.e. it is enclosed in two spaces.

I tried the macro recording but couldn't select anything once the
recording started.

Thanks.
 
G

Graham Mayor

You cannot use the macro recorder to format part of a search string . You
need something like the following

Sub Superscript13_in_13C()
Dim rText As Range
With Selection
.HomeKey wdStory
With .Find
.ClearFormatting
.Replacement.ClearFormatting
Do While .Execute(findText:="13C", _
MatchWildcards:=False, _
Wrap:=wdFindStop, Forward:=True) = True
Set rText = Selection.Range
rText.MoveEnd Unit:=wdCharacter, Count:=-1
rText.Font.Superscript = True
Loop
End With
End With
End Sub

http://www.gmayor.com/installing_macro.htm

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


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

Graham Mayor

Incidentally you don't need a macro to do this.
Format 13C as you want it to appear in the document and copy to the
clipboard.
Now replace
13C
with
^c

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


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
J

John Smith

Thank you. I do need a macro because I need to make several conversions,
not just 13C but also 1H, 19F, etc. I only asked for one because I can
take the answer and modify it to fit my needs.

BTW, why doesn't the following code work? I have to enclose each section
in its own "with selectin/end with" to make it work.

=====================================
With Selection
.HomeKey wdStory
With .Find
' .ClearFormatting
' .Replacement.ClearFormatting
Do While .Execute(findText:="13C", _
MatchWildcards:=False, _
Wrap:=wdFindStop, Forward:=True) = True
Set rText = Selection.Range
rText.MoveEnd Unit:=wdCharacter, Count:=-1
rText.Font.Superscript = True
Loop
End With

With .Find
' .ClearFormatting
' .Replacement.ClearFormatting
Do While .Execute(findText:="1H", _
MatchWildcards:=False, _
Wrap:=wdFindStop, Forward:=True) = True
Set rText = Selection.Range
rText.MoveEnd Unit:=wdCharacter, Count:=-1
rText.Font.Superscript = True
Loop
End With
End With
 
G

Graham Mayor

Had you said there was a variety of numbers, it would have been simpler to
create a macro to cater for that. The following will do the trick with any
combination of two digits followed by an upper case letter. Incidentally
your macro didn't work because you didn't take the start point back to the
start of the document, so your additional bit was searching from the end to
the end.

Dim rText As Range
With Selection
.HomeKey wdStory
With .Find
.ClearFormatting
.Replacement.ClearFormatting
Do While .Execute(findText:="[0-9]{1,}[A-Z]", _
MatchWildcards:=True, _
Wrap:=wdFindStop, Forward:=True) = True
Set rText = Selection.Range
rText.MoveEnd Unit:=wdCharacter, Count:=-1
rText.Font.Superscript = True
Loop
End With
End With


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


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

Graham Mayor

That should have read 1 or more digits followed by an upper case letter.

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


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


Graham said:
Had you said there was a variety of numbers, it would have been
simpler to create a macro to cater for that. The following will do
the trick with any combination of two digits followed by an upper
case letter. Incidentally your macro didn't work because you didn't
take the start point back to the start of the document, so your
additional bit was searching from the end to the end.

Dim rText As Range
With Selection
.HomeKey wdStory
With .Find
.ClearFormatting
.Replacement.ClearFormatting
Do While .Execute(findText:="[0-9]{1,}[A-Z]", _
MatchWildcards:=True, _
Wrap:=wdFindStop, Forward:=True) = True
Set rText = Selection.Range
rText.MoveEnd Unit:=wdCharacter, Count:=-1
rText.Font.Superscript = True
Loop
End With
End With



John said:
Thank you. I do need a macro because I need to make several
conversions, not just 13C but also 1H, 19F, etc. I only asked for one
because I can take the answer and modify it to fit my needs.

BTW, why doesn't the following code work? I have to enclose each
section in its own "with selectin/end with" to make it work.

=====================================
With Selection
.HomeKey wdStory
With .Find
' .ClearFormatting
' .Replacement.ClearFormatting
Do While .Execute(findText:="13C", _
MatchWildcards:=False, _
Wrap:=wdFindStop, Forward:=True) = True
Set rText = Selection.Range
rText.MoveEnd Unit:=wdCharacter, Count:=-1
rText.Font.Superscript = True
Loop
End With

With .Find
' .ClearFormatting
' .Replacement.ClearFormatting
Do While .Execute(findText:="1H", _
MatchWildcards:=False, _
Wrap:=wdFindStop, Forward:=True) = True
Set rText = Selection.Range
rText.MoveEnd Unit:=wdCharacter, Count:=-1
rText.Font.Superscript = True
Loop
End With
End With
 
J

John Smith

Thank you. After some testing, I found that this code will work on every
instance of 13C, not just those who are standalone words, i.e. enclosed
in one or two spaces. Is it possible to modify the code so that it only
applies to " 13C ", " 1H " (there won't be a leading space if they
appear in the beginning of a paragraph) but not xxx13Cxxx nor xxx1Hxxx,
etc.?

BTW, Is there a detailed instruction for findtext somewhere on the net?
I went to the msdn side and found only real simple instructions and
examples, nothing as complex as "[0-9]{1,}[A-Z]". I need to do some
really complex conversions, like converting n and 2n+1 in CnH2n+1OH to
subscript.
 
G

Graham Mayor

For your first dilemma - change the search string to
"<[0-9]{1,}[A-Z]>"

The second problem is more complex and will have to be done in stages eg

With .Find
.ClearFormatting
.Replacement.ClearFormatting
Do While .Execute(findText:="<Cn", _
MatchWildcards:=True, _
Wrap:=wdFindStop, Forward:=True) = True
Set rText = Selection.Range
rText.MoveStart Unit:=wdCharacter, Count:=1
rText.Font.Subscript = True
Loop
Selection.HomeKey wdStory
Do While .Execute(findText:="<CnH2n+1OH>", _
MatchWildcards:=True, _
Wrap:=wdFindStop, Forward:=True) = True
Set rText = Selection.Range
rText.MoveStart Unit:=wdCharacter, Count:=3
rText.MoveEnd Unit:=wdCharacter, Count:=-2
rText.Font.Subscript = True
Loop

The method I have adopted here is to find enough of the strings to eliminate
false positives and then manipulate the start and end positions of the range
to allow the formattig parameter to be applied. Eg in the first part of the
loop I have selected <Cn ie words beginning Cn. The search is case sensitive
because the wildcard option is set, so it will not find 'CN' or 'cn'.

I could have selected the whole string i.e. "<CnH2n+1OH>" and Moved the End
Unit by -7 for the same result (Positive numbers move the positions to the
left negative numbers to the right).

The wildcard functions are documented (by me - with the aid of colleagues)
at http://www.gmayor.com/replace_using_wildcards.htm

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


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


John said:
Thank you. After some testing, I found that this code will work on
every instance of 13C, not just those who are standalone words, i.e.
enclosed in one or two spaces. Is it possible to modify the code so
that it only applies to " 13C ", " 1H " (there won't be a leading
space if they appear in the beginning of a paragraph) but not
xxx13Cxxx nor xxx1Hxxx, etc.?

BTW, Is there a detailed instruction for findtext somewhere on the
net? I went to the msdn side and found only real simple instructions
and examples, nothing as complex as "[0-9]{1,}[A-Z]". I need to do
some really complex conversions, like converting n and 2n+1 in
CnH2n+1OH to subscript.

Graham said:
Had you said there was a variety of numbers, it would have been
simpler to create a macro to cater for that. The following will do
the trick with any combination of two digits followed by an upper
case letter. Incidentally your macro didn't work because you didn't
take the start point back to the start of the document, so your
additional bit was searching from the end to the end.

Dim rText As Range
With Selection
.HomeKey wdStory
With .Find
.ClearFormatting
.Replacement.ClearFormatting
Do While .Execute(findText:="[0-9]{1,}[A-Z]", _
MatchWildcards:=True, _
Wrap:=wdFindStop, Forward:=True) = True
Set rText = Selection.Range
rText.MoveEnd Unit:=wdCharacter, Count:=-1
rText.Font.Superscript = True
Loop
End With
End With
 
G

Graham Mayor

Oops again :(

(Positive numbers move the positions to the
left negative numbers to the right).

should read

(Negative numbers move the positions to the
left positive numbers to the right).


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


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


Graham said:
For your first dilemma - change the search string to
"<[0-9]{1,}[A-Z]>"

The second problem is more complex and will have to be done in stages
eg
With .Find
.ClearFormatting
.Replacement.ClearFormatting
Do While .Execute(findText:="<Cn", _
MatchWildcards:=True, _
Wrap:=wdFindStop, Forward:=True) = True
Set rText = Selection.Range
rText.MoveStart Unit:=wdCharacter, Count:=1
rText.Font.Subscript = True
Loop
Selection.HomeKey wdStory
Do While .Execute(findText:="<CnH2n+1OH>", _
MatchWildcards:=True, _
Wrap:=wdFindStop, Forward:=True) = True
Set rText = Selection.Range
rText.MoveStart Unit:=wdCharacter, Count:=3
rText.MoveEnd Unit:=wdCharacter, Count:=-2
rText.Font.Subscript = True
Loop

The method I have adopted here is to find enough of the strings to
eliminate false positives and then manipulate the start and end
positions of the range to allow the formattig parameter to be
applied. Eg in the first part of the loop I have selected <Cn ie
words beginning Cn. The search is case sensitive because the wildcard
option is set, so it will not find 'CN' or 'cn'.
I could have selected the whole string i.e. "<CnH2n+1OH>" and Moved
the End Unit by -7 for the same result (Positive numbers move the
positions to the left negative numbers to the right).

The wildcard functions are documented (by me - with the aid of
colleagues) at http://www.gmayor.com/replace_using_wildcards.htm


John said:
Thank you. After some testing, I found that this code will work on
every instance of 13C, not just those who are standalone words, i.e.
enclosed in one or two spaces. Is it possible to modify the code so
that it only applies to " 13C ", " 1H " (there won't be a leading
space if they appear in the beginning of a paragraph) but not
xxx13Cxxx nor xxx1Hxxx, etc.?

BTW, Is there a detailed instruction for findtext somewhere on the
net? I went to the msdn side and found only real simple instructions
and examples, nothing as complex as "[0-9]{1,}[A-Z]". I need to do
some really complex conversions, like converting n and 2n+1 in
CnH2n+1OH to subscript.

Graham said:
Had you said there was a variety of numbers, it would have been
simpler to create a macro to cater for that. The following will do
the trick with any combination of two digits followed by an upper
case letter. Incidentally your macro didn't work because you didn't
take the start point back to the start of the document, so your
additional bit was searching from the end to the end.

Dim rText As Range
With Selection
.HomeKey wdStory
With .Find
.ClearFormatting
.Replacement.ClearFormatting
Do While .Execute(findText:="[0-9]{1,}[A-Z]", _
MatchWildcards:=True, _
Wrap:=wdFindStop, Forward:=True) = True
Set rText = Selection.Range
rText.MoveEnd Unit:=wdCharacter, Count:=-1
rText.Font.Superscript = True
Loop
End With
End With
 
J

John Smith

Thank you for your help.

The article on your web site is really good. It helps me a lot.

I have one comment. The articles has the following to say about @:

"@ is used to find one or more occurrences of the previous character.
e.g. lo@t will find lot or loot, ful@ will find ful or full etc."

If lo@t finds lot, doesn't that mean @ is used to find "zero", not one,
or more occurrences?
 
G

Graham Mayor

John said:
If lo@t finds lot, doesn't that mean @ is used to find "zero", not
one, or more occurrences?

Good point - I'll revisit that ;)

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


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

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