[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

How to format fraction in real format in ms word file

Posted on 2004-10-22
8
Medium Priority
?
207 Views
Last Modified: 2010-05-02
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.
0
Comment
Question by:micazone
  • 4
  • 4
8 Comments
 
LVL 77

Expert Comment

by:GrahamSkan
ID: 12379190
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
 

Author Comment

by:micazone
ID: 12381380
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
 
LVL 77

Expert Comment

by:GrahamSkan
ID: 12381863
That would certainly be possible in Word. I can't speak for HTML.
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 

Author Comment

by:micazone
ID: 12386956
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
 

Author Comment

by:micazone
ID: 12391676
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
 
LVL 77

Expert Comment

by:GrahamSkan
ID: 12391695
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
 

Author Comment

by:micazone
ID: 12392043
Every thing fine except it makes slash (/) to supercript.
0
 
LVL 77

Accepted Solution

by:
GrahamSkan earned 500 total points
ID: 12392230
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

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses
Course of the Month8 days, 13 hours left to enroll

611 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question