Peculiar search and Replace

H

Henry Stock

I wonder if anyone can help me with the following search and replace.

I want to replace the character string CO2 with the equivalent string
except with the 2 subscripted. I tried highlighting the 2 in the
replacement string and then selecting special formating and choosing a
subscripted font, but that just resulted in the entire string being
subscripted.

I am using Word 2007
 
S

Suzanne S. Barnhill

Format the string as desired in your document and copy it to the Clipboard,
then use ^c (Clipboard contents) as your "Replace with" text.

--
Suzanne S. Barnhill
Microsoft MVP (Word)
Words into Type
Fairhope, Alabama USA
http://word.mvps.org
 
M

macropod

Hi Henry,

You're asking for more than the standard Find/Replace functions allow.

The following macro will search the active document for chemical formulae (represented by numbers preceded by a letter or a right
bracket) and subscript just the numbers. Unless you're working with isotopes, the results should be correct - you'll need to apply
the isotope superscripting yourself (if the numbers are already superscripted, they'll be left alone).

If your document has other alphanumeric strings in which a non-superscripted number follows a letter (eg Table cell references), you'll
need to select only the range(s) containing the text to be converted and answer 'No' to the prompt.

Sub ChemicalFormatter()
Dim oRng As Range, fRng As Range, bState As Boolean
Application.ScreenUpdating = False
Select Case MsgBox("Do you want to process the whole document?", _
vbYesNoCancel + vbQuestion, "Chemical Formatter")
Case vbYes
bState = True
Case vbNo
bState = False
Case vbCancel
End
End Select
With Selection
Set oRng = .Range
With .Find
.ClearFormatting
.Text = "[A-Za-z)][0-9]{1,}"
.MatchWildcards = True
.Wrap = wdFindContinue
.Forward = True
Do While .Execute = True
Set fRng = ActiveDocument.Range(Start:=Selection.Start + 1, End:=Selection.End)
If bState = False Then
If fRng.Start >= oRng.End Then Exit Do
If fRng.End >= oRng.End Then fRng.End = oRng.End
End If
If fRng.Font.Superscript = False Then fRng.Font.Subscript = True
fRng.Collapse Direction:=wdCollapseEnd
Loop
End With
End With
oRng.Select
Set fRng = Nothing
Set oRng = Nothing
Application.ScreenUpdating = True
End Sub
 
S

Suzanne S. Barnhill

It absolutely works here. See if you're following all the right steps.

1. In the document, select one instance of CO2 and subscript the 2.

2. Select the result and copy it (Ctrl+C).

3. Open the Replace dialog (Ctrl+H).

4. In the "Find what" box, type "CO2" (without the quotation marks).

5. In the "Replace with" box, type "^c" (without the quotation marks).
Alternatively, click More, place the insertion point in the "Replace with
box" and click Special and choose "Clipboard Contents," which will insert ^c
for you.

6. Make sure that there is no formatting indicated on either box.

7. Replace All.

--
Suzanne S. Barnhill
Microsoft MVP (Word)
Words into Type
Fairhope, Alabama USA
http://word.mvps.org
 
K

Klaus Linke

Henry Stock said:
Sorry, but that did not work either. The old CO2s are still in normal
font

Strange... Did you really format the CO2 with subscripted "2", then selected
and copied it to the clipboard, then used
Find what: CO2
Replace with: ^c
?

Another way to do it... Use the font character for the subscripted 2
(Unicode: 2082):
Find what: CO2
Replace with: CO2

You can insert the 2 (in the Find dialog, as in the document) by typing the
code 2082 and then Alt+X.

Regards,
Klaus
 
K

Klaus Linke

[Sorry, the subscripted 2 didn't make it. I posted the same text in the test
group, and it did show.
No idea why... One more try — and if it doesn't work now, I give up.]

Another way to do it... Use the font character for the subscripted 2
(Unicode: 2082):

â–º Find what: CO2
â–º Replace with: COâ‚‚
(In “Replace withâ€, it should be the subscripted 2)

You can insert the subscripted â‚‚ (in the Find dialog, as in the document) by
typing the code 2082 and then Alt+X.

Klaus
 
H

Henry Stock

Okay, It worked the second time. I screwed up on inserting the ^c the
first time.
Thank you Suzanne.
 

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