Format Fractions

Home Up Odds & Ends Photo Gallery Search Contact Me Privacy Notice What's New?

 

 

The information in this website is provided without risk or obligation and free of charge.  However, if you have benefitted from my efforts here and would like to make a contribution to help me continue and maintain this work then any donation will be greatly appreciated. Please click the adjacent button to access PayPal.  Thank you.
 
The field is pretty full of tips and techniques for entering formatted fraction symbols in a Word document or applying formatting to pre-existing plain text fractions in your documents.

Suzanne Barnhill has addressed most of the methods in detail in her Word MVP FAQ article at: Create Fraction

Word MVP Graham Mayor has posted a very effective macro for converting plain text fractions like "1/5" into a formatted fraction like 1/5 in his website article:  Create a fraction

This Tips Page refines the macro referenced in Suzanne's article.   It expands the scope to include fractions involving variables (e.g., x/y) and variable expressions (e.g., 12a/4b).  The graphic below illustrates how some example Word text will be processed using the macro:

Processing rules in the procedure prevent processing items such as dates formatted like mm/dd/yyyy.
Caution:  Dates formatted as mm/yy, mm/dd, etc. will be automatically processed as a fraction
Here is the procedure:
Option Explicit
Public Sub FractionFormatter()
System.Cursor = wdCursorWait
Dim lngProcessed As Long
lngProcessed = FormatFractions
If lngProcessed = 0 Then
MsgBox "No fractions where processed"
Else
MsgBox lngProcessed & " fractions were processed."
End If
System.Cursor = wdCursorNormal
End Sub

Public Function FormatFractions(Optional Scope As Range) As Long
Dim pSlashChr As String
Dim oRng As Word.Range
Dim Divisor As Word.Range
Dim Dividend As Word.Range
Dim SlashPos As Long
Dim pDivSym As Word.Range
Dim IsFraction As Boolean
Dim PreceedingChr As String
Dim TrailingChr As String
Dim oFldRng As Word.Range
Const UrlText As String = "%#_|$/"

If Scope Is Nothing Then Set Scope = ActiveDocument.StoryRanges(wdMainTextStory)
pSlashChr = ChrW$(8260)
Set oRng = Scope
With oRng.Find
    .Text = "[A-Za-z0-9]{1,}^47[A-Za-z0-9]{1,}"
    .Forward = True
    .Wrap = wdFindStop
    .MatchWildcards = True
    While .Execute
      
 'ID the location of the division symbol "/"
        SlashPos = InStr(oRng, "/")
        'Define the range of the dividend text
        Set Dividend = oRng.Duplicate
        Dividend.End = Dividend.Start + SlashPos - 1
        'Difine the range of the divisor text
        Set Divisor = oRng.Duplicate
        Divisor.Start = Divisor.Start + SlashPos
        'Define the range of the division symbol
        Set pDivSym = oRng.Duplicate
        pDivSym.Start = pDivSym.Start + SlashPos - 1
        pDivSym.End = pDivSym.Start + 1
        IsFraction = True 'Found text is considered a fraction unless determined otherwise.
        'Weed out false positives
       

        Set oFldRng = oRng.Duplicate
        oFldRng.Start = oFldRng.Start - 1
        oFldRng.End = oFldRng.End + 1
        'If found range is a part of a field it is probably a hyperlink or similar. Don't process it
        If oFldRng.Fields.Count > 0 Then
            IsFraction = False
        End If
        'ID the preceeding and trailing chacter of the found text
        PreceedingChr = Dividend.Characters.First.Previous.Text
        TrailingChr = Divisor.Characters.Last.Next.Text
        'Prevents processing dd/mm/yyyy formatted dates and other typical URL text
        If IsFraction And InStr(1, UrlText & ".?", PreceedingChr) > 0 Or _
            InStr(1, UrlText, TrailingChr) > 0 Then
            IsFraction = False
        End If
        'Explicitly process alpha and alphanumeric expressions.
        If IsFraction And Not IsNumeric(Dividend.Text) Or IsFraction And Not IsNumeric(Divisor.Text) Then
            oRng.Select
            If MsgBox("Do you want to process this text as a fraction?", _
                               vbYesNo, oRng.Text) = vbNo Then
                IsFraction = False
             End If
        End If
        'Format the fraction
        If IsFraction Then
            Dividend.Font.Superscript = True
            Divisor.Font.Subscript = True
            pDivSym.Text = pSlashChr
            FormatFractions = FormatFractions + 1
        End If
        oRng.Collapse wdCollapseEnd
   Wend
End With
End Function
Need help applying macros?  See Word MVP Graham Mayor's  Guide for Installing Macros 

Looking for something else?

Google