How to format fraction in real format in ms word file

I have a macro to do this but it works only only slected string. I want to find all such strings in a document and format them. The formatting should be restricted to two digits before and 2 digits after.
The code is:
' 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
I request expert guys to modify this macro as requested.
micazoneAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

GrahamSkanRetiredCommented:
You could use wildcard searching:

Sub FormatFraction()
Dim txt As String
Dim slash_pos As Long
Dim numerator As Range
Dim denominator As Range
Dim old_size As Single
Dim rng As Range

'Initialise range objects
Set numerator = ActiveDocument.Range
Set denominator = ActiveDocument.Range

Set rng = ActiveDocument.Range
    Do While True
        rng.Find.ClearFormatting
        With rng.Find.Font
            .Superscript = False
            .Subscript = False
            .Position = 0
        End With
        With rng.Find
            .Text = "[!0-9][0-9]{1,2}/[0-9]{1,2}[!0-9]"
            .Forward = True
            .Format = True
            .MatchWildcards = True
            .Execute
            If .Found = False Then
                Exit Sub
            End If
        End With
        With rng
            txt = .Text
            slash_pos = InStr(txt, "/")
   
            numerator.Start = .Start
            numerator.End = numerator.Start + slash_pos - 1
            old_size = numerator.Font.Size
            numerator.Font.Size = old_size * 0.6
            numerator.Font.Position = old_size * 0.3
   
            denominator.Start = .Start + slash_pos
            denominator.End = .End
            denominator.Font.Size = numerator.Font.Size
   
            .Font.Spacing = -0.5
        End With
        rng.End = ActiveDocument.Range.End
    Loop
End Sub
0
micazoneAuthor Commented:
Thanks. Excellent.
Is there any other way to do this. I mean by making Superscript to first digits and Subscript to last digits. because the present script changes the font scaling and position which is not acceptable in HTML.
Thanks
0
GrahamSkanRetiredCommented:
That would certainly be possible in Word. I can't speak for HTML.
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

micazoneAuthor Commented:
I have done it this way:
Sub FormatFraction()
Dim txt As String
Dim slash_pos As Long
Dim numerator As Range
Dim denominator As Range
Dim old_size As Single
Dim rng As Range

'Initialise range objects
Set numerator = ActiveDocument.Range
Set denominator = ActiveDocument.Range

Set rng = ActiveDocument.Range
    Do While True
        rng.Find.ClearFormatting
        With rng.Find.Font
            .Superscript = False
            .Subscript = False
            .Position = 0
        End With
        With rng.Find
            .Text = "[!0-9][0-9]{1,2}/[0-9]{1,2}[!0-9]"
            .Forward = True
            .Format = True
            .MatchWildcards = True
            .Execute
            If .Found = False Then
                Exit Sub
            End If
        End With
        With rng
            txt = .Text
            slash_pos = InStr(txt, "/")
            numerator.Start = .Start
            numerator.End = numerator.Start + slash_pos - 1
            old_size = numerator.Font.Size
            numerator.Font.Size = old_size - (2)
            numerator.Font.Superscript = True
            denominator.Start = .Start + slash_pos
            denominator.End = .End
            denominator.Font.Size = numerator.Font.Size
            denominator.Font.Subscript = True
        End With
        With rng.Find
            .Text = "/"
            .Replacement.Text = ChrW(8725)
            .Forward = True
            .Format = True
            .MatchWildcards = True
            .Execute Replace:=wdReplaceAll
        End With
        rng.End = ActiveDocument.Range.End
    Loop
End Sub
BUT THE PROBLEM IT FORMATS LEADING "," ALSO. CAN U SUGGEST POSSIBLE WAY TO EXCLUDE COMAS.
Thanks
0
micazoneAuthor Commented:
The " , " problem was solved by modification of
  .Text = "[!0-9][0-9]{1,2}/[0-9]{1,2}[!0-9]"
TO
  .Text = "[0-9]{1,2}/[0-9]{1,2}"
Now,
Actually only 2 digits before and 2 digits after are requiried to be formated as per rules. How to restrict within this code?
Thanks for help.
0
GrahamSkanRetiredCommented:
I'm sorry micazone, I thought that I'd sent the correction. I can remember typing it in, but I guess I got distracted and failed to submit it.
Yes your modification shortened the selection, but removes the restriction on the number of digits.
This code adjusts the range after the Find has been done.

Sub FormatFraction()
Dim txt As String
Dim slash_pos As Long
Dim numerator As Range
Dim denominator As Range
Dim old_size As Single
Dim rng As Range

'Initialise range objects
Set numerator = ActiveDocument.Range
Set denominator = ActiveDocument.Range

Set rng = ActiveDocument.Range
    Do While True
        rng.Find.ClearFormatting
        With rng.Find.Font
            .Superscript = False
            .Subscript = False
            '.Position = 0 No longer necessary
        End With
        With rng.Find
            .Text = "[!0-9][0-9]{1,2}/[0-9]{1,2}[!0-9]"
            .Forward = True
            .Format = True
            .MatchWildcards = True
            .Execute
            If .Found = False Then
                Exit Sub
            End If
        End With
        With rng
            txt = .Text
            slash_pos = InStr(txt, "/")
            numerator.Start = .Start + 1 '<-------
            numerator.End = numerator.Start + slash_pos - 1
            old_size = numerator.Font.Size
            numerator.Font.Size = old_size - (2)
            numerator.Font.Superscript = True
            denominator.Start = .Start + slash_pos
            denominator.End = .End - 1 '<---------
            denominator.Font.Size = numerator.Font.Size
            denominator.Font.Subscript = True
        End With
        With rng.Find
            .Text = "/"
            .Replacement.Text = ChrW(8725)
            .Forward = True
            .Format = True
            .MatchWildcards = True
            .Execute Replace:=wdReplaceAll
        End With
        rng.End = ActiveDocument.Range.End
    Loop
End Sub
0
micazoneAuthor Commented:
Every thing fine except it makes slash (/) to supercript.
0
GrahamSkanRetiredCommented:
I thought that was what chrW(8725) was doing.
Actually, I moved numerator.start and so the next line was wrong
Change this:
           numerator.End = numerator.Start + slash_pos - 1
to this:
           numerator.End = numerator.Start + slash_pos - 2
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.