Re: Find AND Fix Fractions



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,}



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

"schwammrs" <schwammrs(removethis)@charterinternet.com> wrote in message
news:e0t3zGOSHHA.3996@xxxxxxxxxxxxxxxxxxxxxxx
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.
: )

"Greg Maxey" <gmaxey@xxxxxxxxxxxxxxxxxxx> wrote in message
news:eYC4oSNSHHA.3428@xxxxxxxxxxxxxxxxxxxxxxx
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: 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)
  • Excel Listing tool using VB
    ... Sub ListFiles2() ... Dim directories() As String, CurrentDirectory As String ... Dim dirtopaste, dirok ...
    (microsoft.public.vb.general.discussion)
  • Form Error
    ... SMSDS_CallerID As String ... Private Declare Sub Sleep Lib "kernel32" ... Dim ComString As String ... Dim AppPath As String, FreeFileNo% ...
    (microsoft.public.vb.bugs)
  • Re: Encrypt/hide Password
    ... Public Sub New(ByVal strCryptoName As String) ... ' instantiated crypto class. ... Dim fsKey As New FileStream(strSaveToPath, FileMode.OpenOrCreate, _ ...
    (microsoft.public.scripting.wsh)