Fractions

G

Greg's News

I am interested in a way to automatically format fractions when I paste them
into Word. I already have a macro that does 1/2 1/4 and 3/4 but not 1/3 1/8
etc. The macro uses the autoformat command but it doesn't cover the latter
examples. TIA

Greg
 
G

Greg

Greg,

Nothing covers the latter in the sense the special
characters 1/4, 1/2, etc. you mention are part of the font
set while 1/8 is not. For detailed explanation and for

formatting fraction existing in your document, see:
http://www.gmayor.com/createfraction.htm

To insert formatted fraction:

Sub InsertFraction()
Dim DenMsgBox As VbMsgBoxResult
Dim Expr, Numerator, Denominator, NewSlashChar As String
Dim SlashPos As Integer
NewSlashChar = ChrW(&H2044)
Retry:
Expr = InputBox("Enter the fraction as
numerator/denominator (e.g., 3/4, a/b, etc.):", "Enter
Fraction")
If Expr = "" Then Exit Sub
SlashPos = InStr(Expr, "/")
If SlashPos = 0 Or SlashPos = 1 Then
MsgBox "Format must be numerator/denominator (e.g., 3/4,
a/b, etc.). Please try again.", , "Format Error"
GoTo Retry
Else
Numerator = Left(Expr, SlashPos - 1)
Denominator = Right(Expr, Len(Expr) - SlashPos)
If Denominator = "0" Then
DenMsgBox = MsgBox("The denominator is a null value.
Do you want to override?", vbYesNo, "Illogical Expression")
Else: GoTo Convert
End If
If DenMsgBox = vbNo Then
GoTo Retry
Else
Convert:
Selection.Font.Superscript = True
Selection.TypeText Text:=Numerator
Selection.Font.Superscript = False
Selection.TypeText Text:=NewSlashChar
Selection.Font.Subscript = True
Selection.TypeText Text:=Denominator
Selection.Font.Subscript = False
End If
End If
End Sub
 
G

Greg's News

Thanks Greg
Greg,

Nothing covers the latter in the sense the special
characters 1/4, 1/2, etc. you mention are part of the font
set while 1/8 is not. For detailed explanation and for

formatting fraction existing in your document, see:
http://www.gmayor.com/createfraction.htm

To insert formatted fraction:

Sub InsertFraction()
Dim DenMsgBox As VbMsgBoxResult
Dim Expr, Numerator, Denominator, NewSlashChar As String
Dim SlashPos As Integer
NewSlashChar = ChrW(&H2044)
Retry:
Expr = InputBox("Enter the fraction as
numerator/denominator (e.g., 3/4, a/b, etc.):", "Enter
Fraction")
If Expr = "" Then Exit Sub
SlashPos = InStr(Expr, "/")
If SlashPos = 0 Or SlashPos = 1 Then
MsgBox "Format must be numerator/denominator (e.g., 3/4,
a/b, etc.). Please try again.", , "Format Error"
GoTo Retry
Else
Numerator = Left(Expr, SlashPos - 1)
Denominator = Right(Expr, Len(Expr) - SlashPos)
If Denominator = "0" Then
DenMsgBox = MsgBox("The denominator is a null value.
Do you want to override?", vbYesNo, "Illogical Expression")
Else: GoTo Convert
End If
If DenMsgBox = vbNo Then
GoTo Retry
Else
Convert:
Selection.Font.Superscript = True
Selection.TypeText Text:=Numerator
Selection.Font.Superscript = False
Selection.TypeText Text:=NewSlashChar
Selection.Font.Subscript = True
Selection.TypeText Text:=Denominator
Selection.Font.Subscript = False
End If
End If
End Sub
 
S

Suzanne S. Barnhill

But 1/8 *is* part of the font.

--
Suzanne S. Barnhill
Microsoft MVP (Word)
Words into Type
Fairhope, Alabama USA

Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.
 
G

Greg Maxey

I suppose I could have word the response better "... while a special
character for 1/8 is not". Even that may not be grammatically correct.
Regardless the OP has already replied and indicated that he gets it.
 
S

Suzanne S. Barnhill

Greg, look at the 2153 to 215E range of a Unicode font. The character for
1/8 is 215B.

--
Suzanne S. Barnhill
Microsoft MVP (Word)
Words into Type
Fairhope, Alabama USA

Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.
 
G

Greg Maxey

Did you mention learning something everyday recently :). I have very
little call for such characters and tend forget to even look there.

Thanks.
 
S

Suzanne S. Barnhill

It would help if I would actually learn something instead of just having my
errors pointed out so frequently. I've become loath to post because I keep
jamming my foot in my mouth so often lately. <g>

--
Suzanne S. Barnhill
Microsoft MVP (Word)
Words into Type
Fairhope, Alabama USA

Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.
 
S

Suzanne S. Barnhill

There's no way to add it to AutoFormat, since that's Word's own built-in
thing. The best you can do is enter specific, commonly used fractions as
AutoCorrect entries. This is quite straightforward for the ones that are
font characters (the thirds and eighths); for the ones you have to make up
from subscripts and superscripts, you're probably better off creating a
"dummy" as an AutoText entry, then filling in the appropriate numbers as
needed.

--
Suzanne S. Barnhill
Microsoft MVP (Word)
Words into Type
Fairhope, Alabama USA

Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.
 
G

Greg's News

Thanks Suzanne

Greg
There's no way to add it to AutoFormat, since that's Word's own built-in
thing. The best you can do is enter specific, commonly used fractions as
AutoCorrect entries. This is quite straightforward for the ones that are
font characters (the thirds and eighths); for the ones you have to make up
from subscripts and superscripts, you're probably better off creating a
"dummy" as an AutoText entry, then filling in the appropriate numbers as
needed.

--
Suzanne S. Barnhill
Microsoft MVP (Word)
Words into Type
Fairhope, Alabama USA

Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top