Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How to format fraction in real format in ms word file

Posted on 2004-10-22
8
Medium Priority
?
200 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
8 Comments
 
LVL 76

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 76

Expert Comment

by:GrahamSkan
ID: 12381863
That would certainly be possible in Word. I can't speak for HTML.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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 76

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 76

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
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…
Suggested Courses

610 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