Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / February 2007

Tip: Looking for answers? Try searching our database.

Find AND Fix Fractions

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
schwammrs - 05 Feb 2007 02:41 GMT
Hi all--

I'm regularly generating documents using Mail Merge with Word and Excel that
contain fractions (usually simple ones like 1/2, 2/3, 1/8, etc.). I'd like a
Macro that would find any fractions and format them to "superscript num. --  
fraction slash -- subscript denom."  Searching through the .word groups,
I've found various references to a macro that does the formatting part ["Sub
FmtFraction()" listed at the end of this note] but the fraction needs to be
selected in order for it to work.

I tried recording an "Edit, Find" macro myself, but don't know how to
"pause" it and format each "selection" it found before finding the next one.
Then I found a macro that FINDS fractions ["Sub FindFixFractions()" listed
at end] and shows a message box for each but I couldn't figure out where/how
to stick the "Sub FmtFraction()" into it. BTW, I don't need the message
box...

I would appreciate any help you can give me, using these macros or any
others.
Thanks!!!
Karin

P.S. Don't think it matters but I'm using Word 2000.

Sub FmtFraction()
Dim OrigFrac As String
Dim Numerator As String, Denominator As String
Dim NewSlashChar As String
Dim SlashPos As Integer
NewSlashChar = ChrW(&H2044)
OrigFrac = Selection
SlashPos = InStr(OrigFrac, "/")
Numerator = Left(OrigFrac, SlashPos - 1)
Denominator = Right(OrigFrac, Len(OrigFrac) - SlashPos)
With Selection
 .Font.Superscript = True
 .TypeText Text:=Numerator
 .Font.Superscript = False
 .TypeText Text:=NewSlashChar
 .Font.Subscript = True
 .TypeText Text:=Denominator
 .Font.Subscript = False
End With
End Sub

Sub FindFixFractions()
Dim rng As Word.Range
   Dim rngFrac As Word.Range
   Set rng = ActiveDocument.Content
   With rng.Find
       .Text = "^#/^#"
       .Replacement.Text = ""
       .Forward = True
       .Wrap = wdFindAsk
       .Format = False
       .MatchCase = False
       .MatchWholeWord = False
       .MatchWildcards = False
       .MatchSoundsLike = False
       .MatchAllWordForms = False
   End With
   rng.Find.Execute
   MsgBox rng.Text
   ' change rng.text using your fraction here
   Do While rng.Find.Found = True
       rng.Find.Execute Wrap:=wdFindStop
       If rng.Find.Found = True Then
           Set rngFrac = rng.Duplicate
           rngFrac.Expand (wdWord)
           MsgBox rngFrac.Text
           ' change rngfrac.Text using your fraction here
       End If
   Loop
End Sub
Greg Maxey - 05 Feb 2007 03:00 GMT
Sub FindFixFractions()
Dim rng As Word.Range
Dim pStr As String
Set rng = ActiveDocument.Content
With rng.Find
 .Text = "[0-9]@/[0-9]@"
 .MatchWildcards = True
 .Wrap = wdFindStop
 While .Execute
   rng.Select
   FmtFraction
   rng.Collapse wdCollapseEnd
 Wend
End With
End Sub

Sub FmtFraction()
Dim OrigFrac As String
Dim Numerator As String, Denominator As String
Dim NewSlashChar As String
Dim SlashPos As Integer
NewSlashChar = ChrW(&H2044)
OrigFrac = Selection.Text
SlashPos = InStr(OrigFrac, "/")
Numerator = Left(OrigFrac, SlashPos - 1)
Denominator = Right(OrigFrac, Len(OrigFrac) - SlashPos)
With Selection
 .Font.Superscript = True
 .TypeText Text:=Numerator
 .Font.Superscript = False
 .TypeText Text:=NewSlashChar
 .Font.Subscript = True
 .TypeText Text:=Denominator
 .Font.Subscript = False
End With
End Sub

Signature

Greg Maxey/Word MVP
See:
http://gregmaxey.mvps.org/word_tips.htm
For some helpful tips using Word.

> Hi all--
>
[quoted text clipped - 70 lines]
>    Loop
> End Sub
Greg Maxey - 05 Feb 2007 03:21 GMT
The find macro I gave you wasn't very good.  Try this instead:

Sub FindFixFractions()
Dim rng As Word.Range
Dim pStr As String
Set rng = ActiveDocument.Content
With rng.Find
 .Text = "[0-9]@/[0-9]{1,}"
 .MatchWildcards = True
 .Wrap = wdFindStop
 While .Execute
   rng.Select
   FmtFraction
   rng.Collapse wdCollapseEnd
 Wend
End With
End Sub

Signature

Greg Maxey/Word MVP
See:
http://gregmaxey.mvps.org/word_tips.htm
For some helpful tips using Word.

> Hi all--
>
[quoted text clipped - 70 lines]
>    Loop
> End Sub
schwammrs - 05 Feb 2007 04:54 GMT
Sweet! Works great!! Thanks Greg!

Regarding the .Text for the .Find, I am curious about something though.

When I was trying to record my own find, I used "[0-9]{1,}/[0-9]{1,}"

After reading the first response to my post, I had to look up what the @ in
"[0-9]@/[0-9]@" did --> aah, find multiple occurences. But sure enough, it
didn't work well; when searching through "33/54 sample text 23/3465", it
only found "33/5" and "23/3". (Well, actually, it also found "3/5" and
"3/3".)

And, sure enough, "[0-9]@/[0-9]{1,}" from the second response worked great;
it found the full "33/54" and "23/3465" (and again 3/54 and 3/3465 but that
didn't seem to hurt anything).

So why doesn't @ at the end work? And if @ doesn't work at the END, then why
not replace the FIRST instance of it too, i.e. change "[0-9]@/[0-9]{1,}" to
"[0-9]{1,}/[0-9]{1,}" ?

Again, the solution Greg posted works great. I just always like to know why,
not just how -- if anyone has the time or inclination to indulge me.  : )

> The find macro I gave you wasn't very good.  Try this instead:
>
[quoted text clipped - 88 lines]
>>    Loop
>> End Sub
Greg Maxey - 05 Feb 2007 09:18 GMT
I also have to go back and refresh my memory on wild card searches.
Actually while what I provided in the second solution is working I would
probably use:

[0-9]{1,}/[0-9]{1,}

The "@" is rather lazy as and quits as soon as it figures it has none its
job.  Consider:

The word "Cheese":

Che@se

finds cheese as is finds the one or more occurrence of "e" and the "s"

Now using the word "Tree"

and searching with "Tre@

The found item is "The."  The @ says hey, my job is to find "one or more"
and quits as soon as it does.

Demonstrated another way, type a string of the letter "a" and search using

a@
and
a{1,}

Signature

Greg Maxey/Word MVP
See:
http://gregmaxey.mvps.org/word_tips.htm
For some helpful tips using Word.

> Sweet! Works great!! Thanks Greg!
>
[quoted text clipped - 112 lines]
>>>    Loop
>>> End Sub
Greg Maxey - 05 Feb 2007 13:18 GMT
Regarding your:

it found the full "33/54" and "23/3465" (and again 3/54 and 3/3465 but
that  didn't seem to hurt anything).

While that is true in your observations using the Find and Replace
dialog, the code doesn't find 3/54 or 3/3465.

If you look at the code, you will see that the the found range is
collapsed after each .exectute so the new search range is from the end
of the last found item to the end of the document.
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.