Taking find and replace to another level

V

Vanessa

Hi all MS helpers!

I would like to be able to run the find and replace facility whereby parts
of the replacement text are either subscript or a different font size. For
example replace TiO2 with TiO*2* (the *2* is either subscript or a smaller
font size).

Is it possible to add coding to the "replace" line or do I need to create a
macro? I've never used Visual Basic, so I will probably need to be spoon fed
with the solution.

Thanking you,
Vanessa
 
G

Greg Maxey

Vanessa,

Doug Robbins and I (mostly Doug) worked on something for this a year so ago.
Say you want to replace H2O with the Hsubcript2O then in the first message
box that appears, type UP. The rest is self explanatory in the prompts.

Sub Exponents()
Dim Message, Title, Default, Characterformat As String
Starthere:
Message = "Enter Up for superscript; Down for Subscript"
Default = "Up"
Characterformat = UCase(InputBox(Message, Title, Characterformat))
Message = "Enter the search pattern" ' Set prompt.
Title = "Search Pattern" ' Set title.
Default = "H2O2" ' Set default.


GetInput:
SearchString = InputBox(Message, Title, Default)
If SearchString = "" Then
End 'quit subroutine
End If


Dim Message1, Title1, Default1, FirstScriptCharacter
Message1 = "Counting from the left, enter the number of the first
script" _
& " character" ' Set prompt."
Title1 = "First Script Character" ' Set title.
Default1 = "2" ' Set default.


GetInput1:
FirstScriptCharacter = InputBox(Message1, Title1, Default1)


Dim Message2, Title2, Default2, SecondScriptCharacter
Message2 = "Counting from the left, enter the number of the second
script character" ' Set prompt.
Title2 = "Second Script Character" ' Set title.
Default2 = "4" ' Set default.


GetInput2:
SecondScriptCharacter = InputBox(Message2, Title2, Default2)


Dim Message3, Title3, Default3, ThirdScriptCharacter
Message3 = "Counting from the left, enter the number of third script " _
& " character" ' Set prompt."
Title3 = "Third Script Character" ' Set title.
Default3 = "" ' Set default.


GetInput3:
ThirdScriptCharacter = InputBox(Message3, Title3, Default3)


Dim Message4, Title4, Default4, FourthScriptCharacter
Message4 = "Counting from the left enter the number of the fourth script
" _
& "character " ' Set prompt."
Title4 = "FourthScriptCharacter" ' Set title.
Default4 = "" ' Set default.


GetInput4:
FourthScriptCharacter = InputBox(Message4, Title4, Default4)


Dim myrange As Range
Selection.HomeKey Unit:=wdStory
Selection.Find.ClearFormatting
With Selection.Find
Do While .Execute(FindText:=SearchString, MatchWildcards:=True,
Wrap:=wdFindStop, Forward:=True) = True
Set myrange = Selection.Range
If Characterformat = "DOWN" Then
If IsNumeric(FirstScriptCharacter) Then
myrange.Characters(FirstScriptCharacter).Font.Subscript = True
If IsNumeric(SecondScriptCharacter) Then
myrange.Characters(SecondScriptCharacter).Font.Subscript = True
If IsNumeric(ThirdScriptCharacter) Then
myrange.Characters(ThirdScriptCharacter).Font.Subscript = True
If IsNumeric(FourthScriptCharacter) Then
myrange.Characters(FourthScriptCharacter).Font.Subscript = True
ElseIf Characterformat = "UP" Then
If IsNumeric(FirstScriptCharacter) Then
myrange.Characters(FirstScriptCharacter).Font.Superscript = True
If IsNumeric(SecondScriptCharacter) Then
myrange.Characters(SecondScriptCharacter).Font.Superscript = True
If IsNumeric(ThirdScriptCharacter) Then
myrange.Characters(ThirdScriptCharacter).Font.Superscript = True
If IsNumeric(FourthScriptCharacter) Then
myrange.Characters(FourthScriptCharacter).Font.Superscript = True
Else
MsgBox "Invalid Entry. You must enter either Up or Down."
GoTo Starthere
End If
Loop
End With
End Sub
 
D

Doug Robbins - Word MVP

Your memory is better than mine<g>

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
G

Graham Mayor

To replace TiO2 with a subscripted 2 you don't even need a macro. Type the
correct version. Copy that version with the subscripted 2 to the clipboard
then replace TiO2 with ^c throughout the document.

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


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

Greg Maxey

Graham,

I dreamed about that method last night. Just got up and returned here to
post. LOL
 
G

Graham Mayor

If you are dreaming about Word, you need that extra glass of JB to stop it
;)

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


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

Graham Mayor

Watch out for that lightning ;)

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


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
V

Vanessa

GM,

So kind of you to recall a previous response, dust off the cob webbs and
re-post for my benefit. I'll give it a bash. Not so keen on pasting correct
format of TiO2 over every incorrect instance though.

Graham, go have another JB and let Greg do all the work.

Cheers and sweet dreams guys
 
G

Greg Maxey

Vanessa,

My good buddy Graham and I conjured up the replace with the content of the
clipboard long after the older forgetful gentleman Doug and I grunted our
way through this code.

Fact remains if you want to replace CO2 with COss2 once or a billion time
you can simply type COss2 once, copy it to the clipboard and then use the
standard Edit Replace dialog.

Type: CO2 in the find what field
Type: ^c in the replace with
press enter

Vanessa ... pretty name. You have stirred an ember. Never mind, I will go
back to the dream ;-)
 
G

Greg

Graham,

Stangest thing. It was clear as a bell here today. I went out for
lunch and like a bolt out of the blue, BLAM!!
 
G

Graham Mayor

There never was an intention to paste over every instance. Replace does that
for you.
JB is Greg's tipple. I prefer Jack - or the local brandy here ;)

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
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