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