Another dreaded "loop through the document" question

E

Elizabeth Swoope

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
 
D

Doug Robbins - Word MVP

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.

--
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
 
E

Elizabeth Swoope

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.
________________________________________


Doug Robbins - Word MVP said:
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.


:
 
D

Doug Robbins - Word MVP

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


--
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
 
E

Elizabeth Swoope

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

Doug Robbins - Word MVP said:
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


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

Elizabeth Swoope said:
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.
________________________________________





:
 
D

Doug Robbins - Word MVP

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.

________________________________________


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

Elizabeth Swoope said:
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

Doug Robbins - Word MVP said:
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


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

Elizabeth Swoope said:
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.


:
 
E

Elizabeth Swoope

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
 
E

Elizabeth Swoope

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.
 
D

Doug Robbins - Word MVP

Make sure that when you perform the Replace, the selection is at the
beginning of the document.

--
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
 
E

Elizabeth Swoope

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
 
D

Doug Robbins - Word MVP

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


--
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
 
E

Elizabeth Swoope

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
 
E

Elizabeth Swoope

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

Doug Robbins - Word MVP said:
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


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

Elizabeth Swoope said:
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
 
E

Elizabeth Swoope

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

Doug Robbins - Word MVP said:
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


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

Elizabeth Swoope said:
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
 
D

Doug Robbins - Word MVP

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.

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

Elizabeth Swoope said:
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

Doug Robbins - Word MVP said:
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


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

Elizabeth Swoope said:
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.

--
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
 
E

Elizabeth Swoope

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
 
D

Doug Robbins - Word MVP

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.

--
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
 
D

David Sisson

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.
 
E

Elizabeth Swoope

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
 

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

Top