Re: Find AND Fix Fractions

Tech-Archive recommends: Fix windows errors by optimizing your registry



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

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


schwammrs wrote:
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


.



Relevant Pages

  • Project Error
    ... Private Declare Sub Sleep Lib "Kernel32" ... Dim strDataSrc As String ...
    (microsoft.public.vb.bugs)
  • Re: condition formula in Word 2003
    ... The following macro saved in the document template ... Sub AutoNew() ... Dim SettingsFile As String ...
    (microsoft.public.word.docmanagement)
  • Re: condition formula in Word 2003
    ... The following macro saved in the document template ... Sub AutoNew() ... Dim SettingsFile As String ...
    (microsoft.public.word.docmanagement)
  • Re: Is there a way to prevent a RichTextBox from scrolling?
    ... Private _isRegex As Boolean ... Public Sub New(ByVal thispattern As String, ... Dim entry As tDict ...
    (microsoft.public.dotnet.framework.windowsforms.controls)
  • Re: Form refresh code
    ... Are you aware when running the macro the cursor runs to every form field? ... Sub ResetForm() ... Dim oFld As FormFields ... Dim sQuery As String ...
    (microsoft.public.word.docmanagement)