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