Another dreaded "loop through the document" question

E

Elizabeth Swoope

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
 
D

Doug Robbins - Word MVP

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


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

Doug Robbins - Word MVP

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

______________________________________


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

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

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


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

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
 
E

Elizabeth Swoope

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
 

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