Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
MSDN Visual Basic Community
 
 
 
 
 
 
-->
TitleFormat a fraction in Word
KeywordsWord, Office, fraction, format
CategoriesOffice
 
Select the fraction text and then execute the following VBA code, either from a command button or from the VBA code editor.

This code breaks the selected text into a numerator and a denominator. It then shrinks the fonts slightly, raises the numerator up a bit, and compresses the text a little.

 
' Change point sizes to make a fraction look nice.
Sub FormatFraction()
Dim txt As String
Dim slash_pos As Long
Dim numerator As Range
Dim denominator As Range
Dim old_size As Single

    With Selection
        txt = .Text
        slash_pos = InStr(txt, "/")

        Set numerator = .Range
        numerator.End = numerator.Start + slash_pos - 1
        Do While numerator.Characters(1) = " "
            numerator.Start = numerator.Start + 1
        Loop
        old_size = numerator.Font.Size
        numerator.Font.Size = old_size * 0.6
        numerator.Font.Position = old_size * 0.3

        Set denominator = .Range
        ExcludeTrailingParagraphs denominator
        Do While _
            denominator.Characters(denominator.Characters.Count) _
            = " "
            denominator.End = denominator.End - 1
        Loop
        denominator.Start = denominator.Start + slash_pos
        denominator.Font.Size = numerator.Font.Size

        .Range.Font.Spacing = -0.5
    End With
End Sub

' Exclude any trailing empty paragraphs from the range.
Private Sub ExcludeTrailingParagraphs(ByVal rng As Range)
    Do While rng.Characters(rng.Characters.Count) = vbCr
        rng.SetRange rng.Start, rng.End - 1
    Loop
End Sub
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated