Find AND Fix Fractions

S

schwammrs

Hi all--

I'm regularly generating documents using Mail Merge with Word and Excel that
contain fractions (usually simple ones like 1/2, 2/3, 1/8, etc.). I'd like a
Macro that would find any fractions and format them to "superscript num. --
fraction slash -- subscript denom." Searching through the .word groups,
I've found various references to a macro that does the formatting part ["Sub
FmtFraction()" listed at the end of this note] but the fraction needs to be
selected in order for it to work.

I tried recording an "Edit, Find" macro myself, but don't know how to
"pause" it and format each "selection" it found before finding the next one.
Then I found a macro that FINDS fractions ["Sub FindFixFractions()" listed
at end] and shows a message box for each but I couldn't figure out where/how
to stick the "Sub FmtFraction()" into it. BTW, I don't need the message
box...

I would appreciate any help you can give me, using these macros or any
others.
Thanks!!!
Karin

P.S. Don't think it matters but I'm using Word 2000.

Sub FmtFraction()
Dim OrigFrac As String
Dim Numerator As String, Denominator As String
Dim NewSlashChar As String
Dim SlashPos As Integer
NewSlashChar = ChrW(&H2044)
OrigFrac = Selection
SlashPos = InStr(OrigFrac, "/")
Numerator = Left(OrigFrac, SlashPos - 1)
Denominator = Right(OrigFrac, Len(OrigFrac) - SlashPos)
With Selection
.Font.Superscript = True
.TypeText Text:=Numerator
.Font.Superscript = False
.TypeText Text:=NewSlashChar
.Font.Subscript = True
.TypeText Text:=Denominator
.Font.Subscript = False
End With
End Sub

Sub FindFixFractions()
Dim rng As Word.Range
Dim rngFrac As Word.Range
Set rng = ActiveDocument.Content
With rng.Find
.Text = "^#/^#"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindAsk
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
rng.Find.Execute
MsgBox rng.Text
' change rng.text using your fraction here
Do While rng.Find.Found = True
rng.Find.Execute Wrap:=wdFindStop
If rng.Find.Found = True Then
Set rngFrac = rng.Duplicate
rngFrac.Expand (wdWord)
MsgBox rngFrac.Text
' change rngfrac.Text using your fraction here
End If
Loop
End Sub
 
G

Greg Maxey

Sub FindFixFractions()
Dim rng As Word.Range
Dim pStr As String
Set rng = ActiveDocument.Content
With rng.Find
.Text = "[0-9]@/[0-9]@"
.MatchWildcards = True
.Wrap = wdFindStop
While .Execute
rng.Select
FmtFraction
rng.Collapse wdCollapseEnd
Wend
End With
End Sub

Sub FmtFraction()
Dim OrigFrac As String
Dim Numerator As String, Denominator As String
Dim NewSlashChar As String
Dim SlashPos As Integer
NewSlashChar = ChrW(&H2044)
OrigFrac = Selection.Text
SlashPos = InStr(OrigFrac, "/")
Numerator = Left(OrigFrac, SlashPos - 1)
Denominator = Right(OrigFrac, Len(OrigFrac) - SlashPos)
With Selection
.Font.Superscript = True
.TypeText Text:=Numerator
.Font.Superscript = False
.TypeText Text:=NewSlashChar
.Font.Subscript = True
.TypeText Text:=Denominator
.Font.Subscript = False
End With
End Sub


--
Greg Maxey/Word MVP
See:
http://gregmaxey.mvps.org/word_tips.htm
For some helpful tips using Word.

Hi all--

I'm regularly generating documents using Mail Merge with Word and
Excel that contain fractions (usually simple ones like 1/2, 2/3, 1/8,
etc.). I'd like a Macro that would find any fractions and format them
to "superscript num. -- fraction slash -- subscript denom." Searching
through the .word groups, I've found various references to
a macro that does the formatting part ["Sub FmtFraction()" listed at
the end of this note] but the fraction needs to be selected in order
for it to work.
I tried recording an "Edit, Find" macro myself, but don't know how to
"pause" it and format each "selection" it found before finding the
next one. Then I found a macro that FINDS fractions ["Sub
FindFixFractions()" listed at end] and shows a message box for each
but I couldn't figure out where/how to stick the "Sub FmtFraction()"
into it. BTW, I don't need the message box...

I would appreciate any help you can give me, using these macros or any
others.
Thanks!!!
Karin

P.S. Don't think it matters but I'm using Word 2000.

Sub FmtFraction()
Dim OrigFrac As String
Dim Numerator As String, Denominator As String
Dim NewSlashChar As String
Dim SlashPos As Integer
NewSlashChar = ChrW(&H2044)
OrigFrac = Selection
SlashPos = InStr(OrigFrac, "/")
Numerator = Left(OrigFrac, SlashPos - 1)
Denominator = Right(OrigFrac, Len(OrigFrac) - SlashPos)
With Selection
.Font.Superscript = True
.TypeText Text:=Numerator
.Font.Superscript = False
.TypeText Text:=NewSlashChar
.Font.Subscript = True
.TypeText Text:=Denominator
.Font.Subscript = False
End With
End Sub

Sub FindFixFractions()
Dim rng As Word.Range
Dim rngFrac As Word.Range
Set rng = ActiveDocument.Content
With rng.Find
.Text = "^#/^#"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindAsk
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
rng.Find.Execute
MsgBox rng.Text
' change rng.text using your fraction here
Do While rng.Find.Found = True
rng.Find.Execute Wrap:=wdFindStop
If rng.Find.Found = True Then
Set rngFrac = rng.Duplicate
rngFrac.Expand (wdWord)
MsgBox rngFrac.Text
' change rngfrac.Text using your fraction here
End If
Loop
End Sub
 
G

Greg Maxey

The find macro I gave you wasn't very good. Try this instead:

Sub FindFixFractions()
Dim rng As Word.Range
Dim pStr As String
Set rng = ActiveDocument.Content
With rng.Find
.Text = "[0-9]@/[0-9]{1,}"
.MatchWildcards = True
.Wrap = wdFindStop
While .Execute
rng.Select
FmtFraction
rng.Collapse wdCollapseEnd
Wend
End With
End Sub
 
S

schwammrs

Sweet! Works great!! Thanks Greg!

Regarding the .Text for the .Find, I am curious about something though.

When I was trying to record my own find, I used "[0-9]{1,}/[0-9]{1,}"

After reading the first response to my post, I had to look up what the @ in
"[0-9]@/[0-9]@" did --> aah, find multiple occurences. But sure enough, it
didn't work well; when searching through "33/54 sample text 23/3465", it
only found "33/5" and "23/3". (Well, actually, it also found "3/5" and
"3/3".)

And, sure enough, "[0-9]@/[0-9]{1,}" from the second response worked great;
it found the full "33/54" and "23/3465" (and again 3/54 and 3/3465 but that
didn't seem to hurt anything).

So why doesn't @ at the end work? And if @ doesn't work at the END, then why
not replace the FIRST instance of it too, i.e. change "[0-9]@/[0-9]{1,}" to
"[0-9]{1,}/[0-9]{1,}" ?

Again, the solution Greg posted works great. I just always like to know why,
not just how -- if anyone has the time or inclination to indulge me. : )

Greg Maxey said:
The find macro I gave you wasn't very good. Try this instead:

Sub FindFixFractions()
Dim rng As Word.Range
Dim pStr As String
Set rng = ActiveDocument.Content
With rng.Find
.Text = "[0-9]@/[0-9]{1,}"
.MatchWildcards = True
.Wrap = wdFindStop
While .Execute
rng.Select
FmtFraction
rng.Collapse wdCollapseEnd
Wend
End With
End Sub

--
Greg Maxey/Word MVP
See:
http://gregmaxey.mvps.org/word_tips.htm
For some helpful tips using Word.

Hi all--

I'm regularly generating documents using Mail Merge with Word and
Excel that contain fractions (usually simple ones like 1/2, 2/3, 1/8,
etc.). I'd like a Macro that would find any fractions and format them
to "superscript num. -- fraction slash -- subscript denom." Searching
through the .word groups, I've found various references to
a macro that does the formatting part ["Sub FmtFraction()" listed at
the end of this note] but the fraction needs to be selected in order
for it to work.
I tried recording an "Edit, Find" macro myself, but don't know how to
"pause" it and format each "selection" it found before finding the
next one. Then I found a macro that FINDS fractions ["Sub
FindFixFractions()" listed at end] and shows a message box for each
but I couldn't figure out where/how to stick the "Sub FmtFraction()"
into it. BTW, I don't need the message box...

I would appreciate any help you can give me, using these macros or any
others.
Thanks!!!
Karin

P.S. Don't think it matters but I'm using Word 2000.

Sub FmtFraction()
Dim OrigFrac As String
Dim Numerator As String, Denominator As String
Dim NewSlashChar As String
Dim SlashPos As Integer
NewSlashChar = ChrW(&H2044)
OrigFrac = Selection
SlashPos = InStr(OrigFrac, "/")
Numerator = Left(OrigFrac, SlashPos - 1)
Denominator = Right(OrigFrac, Len(OrigFrac) - SlashPos)
With Selection
.Font.Superscript = True
.TypeText Text:=Numerator
.Font.Superscript = False
.TypeText Text:=NewSlashChar
.Font.Subscript = True
.TypeText Text:=Denominator
.Font.Subscript = False
End With
End Sub

Sub FindFixFractions()
Dim rng As Word.Range
Dim rngFrac As Word.Range
Set rng = ActiveDocument.Content
With rng.Find
.Text = "^#/^#"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindAsk
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
rng.Find.Execute
MsgBox rng.Text
' change rng.text using your fraction here
Do While rng.Find.Found = True
rng.Find.Execute Wrap:=wdFindStop
If rng.Find.Found = True Then
Set rngFrac = rng.Duplicate
rngFrac.Expand (wdWord)
MsgBox rngFrac.Text
' change rngfrac.Text using your fraction here
End If
Loop
End Sub
 
G

Greg Maxey

I also have to go back and refresh my memory on wild card searches.
Actually while what I provided in the second solution is working I would
probably use:

[0-9]{1,}/[0-9]{1,}

The "@" is rather lazy as and quits as soon as it figures it has none its
job. Consider:

The word "Cheese":

Che@se

finds cheese as is finds the one or more occurrence of "e" and the "s"

Now using the word "Tree"

and searching with "Tre@

The found item is "The." The @ says hey, my job is to find "one or more"
and quits as soon as it does.

Demonstrated another way, type a string of the letter "a" and search using

a@
and
a{1,}



--
Greg Maxey/Word MVP
See:
http://gregmaxey.mvps.org/word_tips.htm
For some helpful tips using Word.

schwammrs said:
Sweet! Works great!! Thanks Greg!

Regarding the .Text for the .Find, I am curious about something though.

When I was trying to record my own find, I used "[0-9]{1,}/[0-9]{1,}"

After reading the first response to my post, I had to look up what the @
in "[0-9]@/[0-9]@" did --> aah, find multiple occurences. But sure enough,
it didn't work well; when searching through "33/54 sample text 23/3465",
it only found "33/5" and "23/3". (Well, actually, it also found "3/5" and
"3/3".)

And, sure enough, "[0-9]@/[0-9]{1,}" from the second response worked
great; it found the full "33/54" and "23/3465" (and again 3/54 and 3/3465
but that didn't seem to hurt anything).

So why doesn't @ at the end work? And if @ doesn't work at the END, then
why not replace the FIRST instance of it too, i.e. change
"[0-9]@/[0-9]{1,}" to "[0-9]{1,}/[0-9]{1,}" ?

Again, the solution Greg posted works great. I just always like to know
why, not just how -- if anyone has the time or inclination to indulge me.
: )

Greg Maxey said:
The find macro I gave you wasn't very good. Try this instead:

Sub FindFixFractions()
Dim rng As Word.Range
Dim pStr As String
Set rng = ActiveDocument.Content
With rng.Find
.Text = "[0-9]@/[0-9]{1,}"
.MatchWildcards = True
.Wrap = wdFindStop
While .Execute
rng.Select
FmtFraction
rng.Collapse wdCollapseEnd
Wend
End With
End Sub

--
Greg Maxey/Word MVP
See:
http://gregmaxey.mvps.org/word_tips.htm
For some helpful tips using Word.

Hi all--

I'm regularly generating documents using Mail Merge with Word and
Excel that contain fractions (usually simple ones like 1/2, 2/3, 1/8,
etc.). I'd like a Macro that would find any fractions and format them
to "superscript num. -- fraction slash -- subscript denom." Searching
through the .word groups, I've found various references to
a macro that does the formatting part ["Sub FmtFraction()" listed at
the end of this note] but the fraction needs to be selected in order
for it to work.
I tried recording an "Edit, Find" macro myself, but don't know how to
"pause" it and format each "selection" it found before finding the
next one. Then I found a macro that FINDS fractions ["Sub
FindFixFractions()" listed at end] and shows a message box for each
but I couldn't figure out where/how to stick the "Sub FmtFraction()"
into it. BTW, I don't need the message box...

I would appreciate any help you can give me, using these macros or any
others.
Thanks!!!
Karin

P.S. Don't think it matters but I'm using Word 2000.

Sub FmtFraction()
Dim OrigFrac As String
Dim Numerator As String, Denominator As String
Dim NewSlashChar As String
Dim SlashPos As Integer
NewSlashChar = ChrW(&H2044)
OrigFrac = Selection
SlashPos = InStr(OrigFrac, "/")
Numerator = Left(OrigFrac, SlashPos - 1)
Denominator = Right(OrigFrac, Len(OrigFrac) - SlashPos)
With Selection
.Font.Superscript = True
.TypeText Text:=Numerator
.Font.Superscript = False
.TypeText Text:=NewSlashChar
.Font.Subscript = True
.TypeText Text:=Denominator
.Font.Subscript = False
End With
End Sub

Sub FindFixFractions()
Dim rng As Word.Range
Dim rngFrac As Word.Range
Set rng = ActiveDocument.Content
With rng.Find
.Text = "^#/^#"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindAsk
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
rng.Find.Execute
MsgBox rng.Text
' change rng.text using your fraction here
Do While rng.Find.Found = True
rng.Find.Execute Wrap:=wdFindStop
If rng.Find.Found = True Then
Set rngFrac = rng.Duplicate
rngFrac.Expand (wdWord)
MsgBox rngFrac.Text
' change rngfrac.Text using your fraction here
End If
Loop
End Sub
 
G

Greg Maxey

Regarding your:

it found the full "33/54" and "23/3465" (and again 3/54 and 3/3465 but
that didn't seem to hurt anything).

While that is true in your observations using the Find and Replace
dialog, the code doesn't find 3/54 or 3/3465.

If you look at the code, you will see that the the found range is
collapsed after each .exectute so the new search range is from the end
of the last found item to the end of the document.
 

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