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 / January 2008

Tip: Looking for answers? Try searching our database.

Another dreaded "loop through the document" question

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Elizabeth Swoope - 11 Jan 2008 00:47 GMT
Using Word 2007.

I promise that I have looked through the code here but my programming skills
are rusty and VBA is NOT my "thing".

I have a long (200+ page) document that begins life as an HTML file
generated by a test bank program. I've written a long macro that does a lot
of cleanup work using find/replace and it works great. However, I'm not quite
satisfied with my results. This test bank doesn't get monkeyed with often so
I don't care how slow or clunky the macro is and I don't care to spend hours
and hours getting it perfect.

The edited document contains between 450 and 500 questions, each in the
format shown below. All paragraphs are in the Normal style (with no space
above or below) except the question number (e.g, 13 of 465), which is in
Normal (Web) (which has space before and after). I'll show returns as <r>. =
indicates a horizontal rule that is the full width of the page, - indicates a
horizontal rule that is half the width of the page.

===========================================
13 of 465<r>
This is the question. It may be one or more paragraphs. Each paragraph ends
with two returns.<r>
<r>
(some questions have a graphic)<r>
<r>
a. This is the first answer.<r>
b. This is the second answer.<r>
c. This is the third answer.<r>
d. This is the fourth answer.<r>
-------------------------------------------<r>
This is the feedback shown if the student gives the wrong answer. It may be
more than one paragraph.<r>
<r>
a.<r>
==================================================
14 of 465<r>
etc.

I want the information below the possible answers to show the correct answer
before the feedback, like this:

-------------------------------------------<r>
a.<r>
This is the feedback shown if the student gives the wrong answer. It may be
more than one paragraph.<r>
==================================================

I have recorded a macro which finds the correct answer, cuts it, then
searches for the graphic immediately above it (which is always the short
horizontal rule) and pastes it after that graphic, then formats with a style
that has space below. I'm not worried about two hard returns in a row right
now because I can search and replace those later. I just want to get the
answer and the feedback swapped for now.

Here's the code that I recorded. I just need to know what to put around it
to make it run repeatedly until it gets to the end of the document. I'm sure
this is a stupidly simple thing, but I'm having no luck. I understand the
concept of loops, I just don't know what variable to check for to do this
until the end of the document is reached.

If there's a way to search for a horizontal rule (and differentiate between
full-width and 50% width), I'm all eyes because I may need to differentiate
between graphics that are pictures and those that are lines.

Sub SwapAnswer()
'
' SwapAnswer Macro
'
'
   Selection.Find.ClearFormatting
   With Selection.Find
       .Text = "^p^$.^p"
       .Replacement.Text = ""
       .Forward = True
       .Wrap = wdFindAsk
       .Format = False
       .MatchCase = False
       .MatchWholeWord = False
       .MatchWildcards = False
       .MatchSoundsLike = False
       .MatchAllWordForms = False
   End With
   Selection.Find.Execute
   Selection.Cut
   Selection.Find.ClearFormatting
   With Selection.Find
       .Text = "^g^p"
       .Replacement.Text = ""
       .Forward = False
       .Wrap = wdFindAsk
       .Format = False
       .MatchCase = False
       .MatchWholeWord = False
       .MatchWildcards = False
       .MatchSoundsLike = False
       .MatchAllWordForms = False
   End With
   Selection.Find.Execute
   Selection.MoveRight Unit:=wdCharacter, Count:=1
   Selection.PasteAndFormat (wdPasteDefault)
   Selection.MoveUp Unit:=wdLine, Count:=1
   Selection.Style = ActiveDocument.Styles("Body Text")
   Selection.TypeBackspace
End Sub

Thanks in advance for any and all help. I'm sure I'm missing something very
simple.

liz
Doug Robbins - Word MVP - 11 Jan 2008 04:04 GMT
There is little doubt that what you want to do is possible, but you
facsimile example does not provide sufficient information.

Copy and paste several question/answer segments into a message that you post
back here so that we can get a better idea of what you are starting with.

Signature

Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP

> Using Word 2007.
>
[quoted text clipped - 123 lines]
>
> liz
Elizabeth Swoope - 11 Jan 2008 11:25 GMT
Doug,

Okay, I've posted several questions. The code I posted works ONCE each time
the macro runs. It finds the right answer, moves it above the explanation,
and changes the style to one that has space below rather than using Enter
Enter.

I just need to know how to make the code repeat until it gets to the end of
the document. I need a loop, I just don't know what the magic "end of file"
condition is. I could fake it by looping 465 times but there aren't always
465 questions and I don't really want to have to remember to change the magic
number each time.

Thanks,

liz

-------

1 of 465
The fastest, most powerful computers often must be fed data by several fast,
powerful, multi-user and multitasking computers. In which category are the
computers that feed the data?

a. super computer
b. mainframe
c. minicomputer
d. microcomputer
________________________________________
Mainframes are used to feed data to the fastest, most powerful computers
(supercomputers).

b.
________________________________________
2 of 465
The smallest, least powerful, least expensive computers are in which category?

a. super computer
b. mainframe
c. minicomputer
d. microcomputer
________________________________________
Microcomputers are the smallest, least powerful, least expensive computers.

d.
________________________________________
3 of 465
Computers that are usually multi-user and multitasking, but which can
sometimes be devoted to performing a single task, are in which category?

a. super computer
b. mainframe
c. minicomputer
d. microcomputer
________________________________________
Minicomputers are usually multi-user and multitasking, but can sometimes be
devoted to performing a single task.

c.
________________________________________
4 of 465
Which category of computers is the largest, fastest, and most powerful and
is both multi-user and multitasking?

a. super computer
b. mainframe
c. minicomputer
d. microcomputer
________________________________________
Super computers are the largest, fastest, and most powerful computers and
are both multi-user and multitasking.

a.
________________________________________

> There is little doubt that what you want to do is possible, but you
> facsimile example does not provide sufficient information.
>
> Copy and paste several question/answer segments into a message that you post
> back here so that we can get a better idea of what you are starting with.
Doug Robbins - Word MVP - 11 Jan 2008 20:37 GMT
You do not need to use a macro for this.

In the Replace dialog, click on the "More" button and then check the "Use
wildcards" box and in the "Find what:" control, enter

([A-z.,\- \(\)]{1,}^13)(^13)([a-d]{1})(.^13)

and in the "Replace with:" control, enter

\3\4\1

Then click on "Replace All"

For an explanation, See the article "Finding and replacing characters using
wildcards" at:

http://www.word.mvps.org/FAQs/General/UsingWildcards.htm

Signature

Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP

> Doug,
>
[quoted text clipped - 85 lines]
>> post
>> back here so that we can get a better idea of what you are starting with.
Elizabeth Swoope - 11 Jan 2008 21:44 GMT
Doug,

Alas, that did not work. I got 0 replacements. When I have time to look at
your answer, I will figure out what those long strings mean and I may be able
to figure out where the problem is. I'll be tickled if a find/replace will do
the trick!

I will get back to you later with the results after I've had time to tinker.
Thanks for pointing me in the direction that will probably result in a
solution!

liz

> You do not need to use a macro for this.
>
[quoted text clipped - 103 lines]
> >> post
> >> back here so that we can get a better idea of what you are starting with.
Doug Robbins - Word MVP - 11 Jan 2008 22:01 GMT
I copied and pasted the text from your message into a Word document, then
used Find and Replace to replace the line breaks ^l with ^p, then removed
the superfluous carriage returns at the ends of the individual lines to
restore the proper paragraph arrangement.

Then, using a Wild Card replace with the strings that I gave you, the
resulted in 4 replacements with the following being produced:

1 of 465

The fastest, most powerful computers often must be fed data by several fast,
powerful, multi-user and multitasking computers. In which category are the
computers that feed the data?

a. super computer

b. mainframe

c. minicomputer

d. microcomputer

________________________________________

b.

Mainframes are used to feed data to the fastest, most powerful computers
(supercomputers).

________________________________________

2 of 465

The smallest, least powerful, least expensive computers are in which
category?

a. super computer

b. mainframe

c. minicomputer

d. microcomputer

________________________________________

d.

Microcomputers are the smallest, least powerful, least expensive computers.

________________________________________

3 of 465

Computers that are usually multi-user and multitasking, but which can

sometimes be devoted to performing a single task, are in which category?

a. super computer

b. mainframe

c. minicomputer

d. microcomputer

________________________________________

c.

Minicomputers are usually multi-user and multitasking, but can sometimes be
devoted to performing a single task.

________________________________________

4 of 465

Which category of computers is the largest, fastest, and most powerful and
is both multi-user and multitasking?

a. super computer

b. mainframe

c. minicomputer

d. microcomputer

________________________________________

a.

Super computers are the largest, fastest, and most powerful computers and
are both multi-user and multitasking.

________________________________________

Signature

Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP

> Doug,
>
[quoted text clipped - 132 lines]
>> >> back here so that we can get a better idea of what you are starting
>> >> with.
Elizabeth Swoope - 12 Jan 2008 00:14 GMT
Doug,

I was in a hurry when I started tinkering with this (school starts Monday
and my computer lab wasn't ready and the computer guys weren't around so I
was in panic mode), but when I slowed down a bit and went back through it, it
worked! I'd not checked Use wildcards, which explains why it didn't work.\

That is really slick!

However, there's at least one instance in which it isn't working properly.
Haven't had time to try to figure out exactly why.

When the text to be flipped is:

EXST2000 is a folder.

b.

The search  highlights only " is a folder. <p><p>b.<p>" and not "EXST2000"
so the swap doesn't work properly. The result is "EXST2000b.<p> is a
folder.<p>"

I don't think it's because "EXST2000" is bold because the entry before it
(which flips properly) is "Masterfiles is a folder." and "Masterfiles" is
bold.

I'll have to spend some time playing with this, but this is a good start. I
may want to clean up the correct answer (remove the extra return and format
as Body Text) before doing the swap, which will require a little bit of
tweaking to the string you gave me.

Anyway, thanks for taking the time to help!

liz
Elizabeth Swoope - 12 Jan 2008 13:44 GMT
Doug,

I have made some progress. I changed your recommended search string to the
following because the feedback includes all sorts of special characters that
were resulting in the only part of the feedback being selected. Rather than
adding more exceptions, I just changed the range:

([ -~)]{1,}^13)(^13)([a-d]{1})(.^13)

However, even though [ and ] characters are included in that range, when the
feedback includes [ (as in "[Shift]"), the feedback is split at the first one
of those characters. I tried adding \[\] to the string and even (\[)(\]) but
that just gave me error messages. I'm sure I'm doing something obvious, but
this is new enough to me that I didn't catch it. I'm going to get away from
it for awhile and maybe fresh perspective will help.

Second problem: Some feedback paragraphs appear perfectly normal (no unusual
characters or formatting) but get split. For example, I get

You should never type spaces at the beginning of a parac.

graph or line ....

instead of

c.

You should never type spaces at the beginning of a paragraph or line....

(ellipsis not in original text)

Third problem:

Some of the feedback is more than one paragraph (two or three, there may be
some that are more than that but I'm not sure). The "flip" only flips the
correct answer and the last feedback paragraph.

The pattern is this:

d. last answer
---------(horizontal line 50% width)
feedback (one or more paragraphs, separated by two hard returns)

feedback (example second paragraph)

a.
=========================(horizontal line, full width)

I have also been able to change the style of the correct answer to Body Text
so that it has space between it and the feedback, so that's more progress.
Doug Robbins - Word MVP - 12 Jan 2008 20:39 GMT
Make sure that when you perform the Replace, the selection is at the
beginning of the document.

Signature

Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP

> Doug,
>
[quoted text clipped - 56 lines]
> Text
> so that it has space between it and the feedback, so that's more progress.
Elizabeth Swoope - 12 Jan 2008 23:31 GMT
Doug,

I always start with the cursor at the beginning of the document. I open the
document then start the replaces. The problems that I mentioned in my latest
message occur when the cursor is at the beginning of the document and nothing
is selected. The cursor location isn't the source of my problems.

liz

> Make sure that when you perform the Replace, the selection is at the
> beginning of the document.
Doug Robbins - Word MVP - 14 Jan 2008 03:56 GMT
Looks like a macro method might be required.

The following will re-arrange the answers to all of the questions except for
the last one in the document.

    Dim myrange As Range
    Dim i As Long, j As Long
    Selection.HomeKey wdStory
    Selection.Find.ClearFormatting
    With Selection.Find
        Do While .Execute(findText:="[0-9]{1,} of [0-9]{1,}",
Forward:=True, _
            MatchWildcards:=True, MatchCase:=True, Wrap:=wdFindStop) = True
            Set myrange = Selection.Range
            With ActiveDocument
               If Not .Paragraphs(1).Range.start = myrange.start Then
                   For i = 1 To .Paragraphs.Count
                       If .Paragraphs(i).Range.start = myrange.start Then
                           Set myrange = .Paragraphs(i - 2).Range
                           Exit For
                       End If
                   Next i
                   j = 3
                   Do While Left(.Paragraphs(i - j).Range, 1) <> "d"
                       j = j + 1
                   Loop
                   .Paragraphs(i - j + 2).Range.InsertBefore myrange.Text '
& vbCr
                   myrange.Delete
               End If
            End With
            Selection.Collapse wdCollapseEnd
        Loop
    End With

Signature

Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP

> Doug,
>
[quoted text clipped - 10 lines]
>> Make sure that when you perform the Replace, the selection is at the
>> beginning of the document.
Elizabeth Swoope - 14 Jan 2008 04:19 GMT
Doug,

Just wanted to acknowledge your response. Tomorrow is my first day of class
for the semester and it's five hours of talking to the students so I may not
have a chance to try the macro until Tuesday. I'll let you know how it works
and I promise I'll try to translate what the macro is doing into English so I
can understand it.

Thanks,

liz

> Looks like a macro method might be required.
>
[quoted text clipped - 30 lines]
>          Loop
>      End With
Elizabeth Swoope - 14 Jan 2008 11:09 GMT
Doug,

I'm up early this morning and decided I'd try the macro.

The following lines are in red, which I assume means there's some sort of
error. Unfortunately, I'm not familiar with VB so can't diagnose the problem,
although I'll dig out some books and see if I can figure it out.

Do While .Execute(findText:="[0-9]{1,} of [0-9]{1,}",
Forward:=True, _
MatchWildcards:=True, MatchCase:=True, Wrap:=wdFindStop) = True

and

& vbCr

Thanks,

liz

> Looks like a macro method might be required.
>
[quoted text clipped - 45 lines]
> >> Make sure that when you perform the Replace, the selection is at the
> >> beginning of the document.
Elizabeth Swoope - 14 Jan 2008 11:20 GMT
Doug,

I realized that the red text might indicate lines that were wrapped that
shouldn't be and that fixed the red problem.

However, the following line produces the error "Run-time error '5941': The
requested member of the collection does not exist."

Do While Left(.Paragraphs(i - j).Range, 1) <> "d"

I will try to do some reasearch and see if I can at least understand the
keywords and what the macro is trying to do.

Thanks,

liz

> Looks like a macro method might be required.
>
[quoted text clipped - 45 lines]
> >> Make sure that when you perform the Replace, the selection is at the
> >> beginning of the document.
Doug Robbins - Word MVP - 14 Jan 2008 11:44 GMT
That problem is probably caused by the first paragraph in the document not
being the one that contains 1 of 465

If you were to temporarily cut any paragraphs before that one and copy them
to a new document, then run the macro on the document containing the
questions, it should work OK.  After running it, then you could copy the
information back into the document.

What is happening is that the macro is finding the # of ### string and then
stepping back through the paragraphs in the document, skipping the previous
couple until it comes to the d. of the fourth choice item.  It then copies
the a. b. c. or d. of the correct answer into the position ahead of the
explanation, before deleting that a. b. c. or d. from its original location.

Signature

Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP

> Doug,
>
[quoted text clipped - 66 lines]
>> >> Make sure that when you perform the Replace, the selection is at the
>> >> beginning of the document.
Elizabeth Swoope - 14 Jan 2008 15:10 GMT
Doug,

Cutting the text at the top of the document resolved the problem and I don't
mind editing the last question myself.

However (and please don't take this as criticism because it's not; I'm just
reporting what's happening and I may be expecting too much)... the macro
bebops along fairly quickly for the first few questions. Then it slows to a
crawl. I finally had to shut the computer down after an hour and it was about
1/3 of the way through. At this rate, it's going to take 3-4 hours (maybe
more since it seems slower on each subsequent questions) to process the
entire file, which is 212 pages long. I'm just reporting the behavior on the
off chance there's some little tweak that can be made.

I had to turn off Word's autosave and the computer's screen saver to keep
things from locking up.

Anyway, it appears that the macro functions exactly as it needs to and I
think I can make the other formatting changes (mainly style assignments and
changes to try to "glue" the entire question together and get the spacing the
way I want it) myself.

I'm going to take some time to study the macro within the next day or two. I
understand in theory what it's doing but need to absorb the details.

Thanks again for all your help. You've definitely earned your MVP title!

liz
Doug Robbins - Word MVP - 14 Jan 2008 20:11 GMT
The reason that it slows down is because each time it finds a question
number, this block of code is going back to the start of the document and
working through each paragraph if turn to see if it is the one containing
the found question number so that it knows what paragraph number (i) it is
in the document.  I'll have a look at changing the code so that perhaps it
only has to go back to the last question for which it found (i) and then
count forward from that spot.

Signature

Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP

> Doug,
>
[quoted text clipped - 32 lines]
>
> liz
Elizabeth Swoope - 15 Jan 2008 01:57 GMT
Doug,

Any additional help you can give will be much appreciated, but I feel like
I've taken way more of your time than I should. If you can come up with a
change that works without spending too much time on it, I'm happy to use it.
However, this is something that I don't do that often and I can live with the
file in the out-of-order order if I have to so please don't feel obligated.

I'm going to look at the macro tomorrow and try to digest it and I'm going
to play with what David posted, too.

I don't know if it helps that the  "x of y" text is in a different style
from all other text in the document...

liz

> The reason that it slows down is because each time it finds a question
> number, this block of code is going back to the start of the document and
[quoted text clipped - 40 lines]
> >
> > liz
Doug Robbins - Word MVP - 15 Jan 2008 02:43 GMT
The following might be quicker:

    Dim myrange As Range
    Dim i As Long, j As Long, k As Long
    Dim Flag As Boolean
    Flag = False
    Selection.HomeKey wdStory
    Selection.Find.ClearFormatting
    With Selection.Find
        Do While .Execute(findText:="[0-9]{1,} of [0-9]{1,}",
Forward:=True, _
            MatchWildcards:=True, MatchCase:=True, Wrap:=wdFindStop) = True
            Set myrange = Selection.Range
            With ActiveDocument
               If Not .Paragraphs(1).Range.start = myrange.start Then
                   If Flag = False Then
                       For i = 1 To .Paragraphs.Count
                           If .Paragraphs(i).Range.start = myrange.start
Then
                               Set myrange = .Paragraphs(i - 2).Range
                               k = i
                               Flag = True
                               Exit For
                           End If
                       Next i
                   Else
                       For i = k To .Paragraphs.Count
                           If .Paragraphs(i).Range.start = myrange.start
Then
                               Set myrange = .Paragraphs(i - 2).Range
                               k = i
                               Exit For
                           End If
                       Next i
                   End If
                   j = 3
                   Do While Left(.Paragraphs(k - j).Range, 1) <> "d"
                       j = j + 1
                   Loop
                   .Paragraphs(k - j + 2).Range.InsertBefore myrange.Text '
& vbCr
                   myrange.Delete
               End If
            End With
            Selection.Collapse wdCollapseEnd
        Loop
    End With

Signature

Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP

> Doug,
>
[quoted text clipped - 32 lines]
>
> liz
Elizabeth Swoope - 15 Jan 2008 03:36 GMT
Doug,

The macro is still running on the file, but the execution speed is obviously
MUCH faster. It's running 1-2 seconds per question so it's still doing to
take 8-10 minutes minimum to run, but that beats the heck out of the earlier
version. I don't modify the test bank all that often, so that sort of
execution time isn't a problem, and well worth it for getting the information
in the order I want it.

There are some questions that aren't in the format I showed you and I can't
tell how the macro is going to work on them until after the macro is done and
I have a chance to look at the file so I can't be sure just yet that
everything is perfect, but if I have to deal with those questions manually
(if the macro didn't work on them or made a hash of them), that's doable.

I'm too tired to do a thorough job of looking at my file so I'll get back to
you tomorrow with a full report after I've had a chance to look over the
results. I think I can manage some of the other things I want to do with
simple find and replace.

I also want to understand what the code is doing and that's going to take
some thought.

You have no idea how much I appreciate all your work.

liz

> The following might be quicker:
>
[quoted text clipped - 80 lines]
> >
> > liz
Elizabeth Swoope - 15 Jan 2008 20:39 GMT
Doug,

Here's an update.

I have looked through the whole humongous edited file and the only fly in
the ointment is that if the first character of the feedback has an attribute
like bold, italic, or a color, the correct answer takes on that attribute. I
can probably fix that with a simple find/replace, I just haven't had time to
try it.

There are several (four or five? not many, for sure) blocks of non-standard
questions (some where the student has to type a number and some that are a
multi-part question with multiple answers) that the macro doesn't handle
well. However, I think the solution for those is to move them to another
document temporarily (just as I have to move the first couple or three lines
of the document) and handle them separately. They need relatively little
editing, and I think all of that can be done with a simple find/replace
macro. It's not worth the effort to modify your macro to handle them. After
your macro is run on the standard questions, I can just put the maverick
questions back in the file where they belong.

I'm off to do some macro studying and tinkering now, but I wanted you to
know that your macro works beautifully.

Thanks,

liz
David Sisson - 14 Jan 2008 22:30 GMT
Here's my shot at it.

The range collapses as it goes, so hopefully it will run steadly.

You may have to cut and paste a couple of places in the code since I'm
looking for text in the find.  The translation from text to web and
back again may have distorted the actual values of the underscore in
your document.

Sub MoveAnsBeforeInfo()

Dim Rng3 As Range
Dim Rng2 As Range
Dim Rng As Range

Set Rng3 = ActiveDocument.Range
Set Rng = Rng3.Duplicate

Do
   With Rng.Find
   .ClearFormatting
   .Wrap = wdFindStop
   'Looking for last three characters of line.
   'You may have to copy and paste to get this to work.
   .Execute findText:="__ ", Forward:=True
       If .Found Then
           Rng3.SetRange Rng.Start, Rng3.End
           InsertPt = Rng.End
           'Move Rng down one sentence. Part of loop mechanism.
           Rng.Move wdSentence
           Do
           'Expand down one sentence
           Rng.Expand wdSentence
'            Rng.Select
           Select Case Left(Rng, 2)
               Case "a.", "b.", "c.", "d."
               MyText$ = Left(Rng, 2)
               Set Rng2 = ActiveDocument.Range(InsertPt + 1, InsertPt
+ 1)
               Rng2.InsertAfter Rng
               Rng.MoveEnd wdCharacter, 2
               Rng.Cut
               Exit Do
           End Select
           'Next Question check.  If question unanswered, this should
catch it.
           'Looking for first three characters of line.
           'You may have to copy and paste to get this to work.
           If Left(Rng, 3) = "___" Then Exit Do
           'End of document check
           If Rng.Move(wdSentence) = 0 Then Exit Do
           Loop
       End If
   End With

Rng.MoveStart wdWord
Rng.End = Rng3.End

Loop Until Not Rng.Find.Found

End Sub

Sub Meth2()
'Define and set ranges
Dim RngToSrch As Range
Dim RngResult As Range

Set RngToSrch = ActiveDocument.Range
Set RngResult = RngToSrch.Duplicate

Do
   With RngResult.Find
   .ClearFormatting
   .Text = Chr(49) & Chr(32) & Chr(111)
   .Forward = True
   .Wrap = wdFindStop
   .Execute
   End With

   If Not RngResult.Find.Found Then Exit Do

   RngResult.MoveStart wdSentence, 1
   RngResult.Collapse wdCollapseStart

Loop Until Not RngResult.Find.Found

End Sub

Most of this technique is from Steven Roman's book, Writing Word
Macros 1999 O'Reilly.
David Sisson - 14 Jan 2008 22:34 GMT
Disregard the Meth2 Sub, it was a working area and was unintentionally
copied
Doug Robbins - Word MVP - 15 Jan 2008 03:01 GMT
Definitely quicker, but it gives me:

1 of 465

The fastest, most powerful computers often must be fed data by several fast,
powerful, multi-user and multitasking computers. In which category are the
computers that feed the data?

a. super computer

b. mainframe

c. minicomputer

d. microcomputer

____b.

____________________________________

Mainframes are used to feed data to the fastest, most powerful computers
(supercomputers).

______________________________________

Signature

Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP

> Disregard the Meth2 Sub, it was a working area and was unintentionally
> copied
Elizabeth Swoope - 15 Jan 2008 02:04 GMT
David,

Thanks for the code. I will try it tomorrow and see if I can make any sense
of it. I will be able to concentrate on the macros all day tomorrow so maybe
I can make some progress on understanding what is being done and perhaps be
able to make simple adjustments myself if necessary.

VB is so unlike any other programming language I've used and I get sensory
overload just looking at the code. I greatly admire those of you who are
fluent. I'm sure if I had good teachers and devoted myself to learning it, I
could do so (I've learned several other programming languages reasonably
well) but it's not something I have any desire to do.

liz

> Here's my shot at it.
>
[quoted text clipped - 86 lines]
> Most of this technique is from Steven Roman's book, Writing Word
> Macros 1999 O'Reilly.
 
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.