dividing a document into chunks

D

Dean

I want to be able to divide documents into "paragraphs" consisting of
five words each. (I'll use these documents to train WindowsVista
Speech Recognition for accuracy.) Obviously, I can record a macro and
create one or more five-word paragraphs in the recording process,
inserting necessary carriage returns, but what kind of loop to I
create, with error-checking, so that Word will go through a document
and generate these paragraphs for me and stop obediently at the end of
the document, no matter its length?
 
J

Jonathan West

Dean said:
I want to be able to divide documents into "paragraphs" consisting of
five words each. (I'll use these documents to train WindowsVista
Speech Recognition for accuracy.) Obviously, I can record a macro and
create one or more five-word paragraphs in the recording process,
inserting necessary carriage returns, but what kind of loop to I
create, with error-checking, so that Word will go through a document
and generate these paragraphs for me and stop obediently at the end of
the document, no matter its length?

Sub SplitIntoParas
Dim iWord as Long
With ActiveDocument.Range
For iWord = .Words.Count To 1 Step -5
.Words(iWord).InsertParagraphAfter
Next iWord
End With
End Sub

As it happens, this macro works its way backwards through the document, to
get away from the problem that Word treats an inserted paragraph as a member
of the Words collection.
 
D

Dean

Sub SplitIntoParas
Dim iWord as Long
With ActiveDocument.Range
  For iWord = .Words.Count To 1 Step -5
    .Words(iWord).InsertParagraphAfter
  Next iWord
End With
End Sub
This looks neat, but when I run it on a document in Word 2007, I get "error 5904, cannot edit range."

Dean
 
D

Dean

Jonathan said:
Sub SplitIntoParas
Dim iWord as Long
With ActiveDocument.Range
For iWord = .Words.Count To 1 Step -5
.Words(iWord).InsertParagraphAfter
Next iWord
End With
End Sub

This looks neat, but when I run it on a document in Word 2007, I get
"error 5904, cannot edit range."



Dean
 
J

Jonathan West

Dean said:
This looks neat, but when I run it on a document in Word 2007, I get
"error 5904, cannot edit range."


Hi Dean

Strange, works perfectly happily with 2003. Try this instead

Sub SplitIntoParas()
Dim iWord As Long
Dim oWord As Range
With ActiveDocument.Range
For iWord = .Words.Count To 1 Step -5
Set oWord = .Words(iWord)
oWord.InsertParagraphAfter
Next iWord
End With
End Sub
 
D

Dean

Jonathan said:
Hi Dean

Strange, works perfectly happily with 2003. Try this instead

Sub SplitIntoParas()
Dim iWord As Long
Dim oWord As Range
With ActiveDocument.Range
For iWord = .Words.Count To 1 Step -5
Set oWord = .Words(iWord)
oWord.InsertParagraphAfter
Next iWord
End With
End Sub

Thank you, Jonathan. Apparently my problem with the first macro was
some quirk in the document; subsequently it worked fine, but I'll keep
this one. I'm trying to understand VBA, so the more examples I get my
hands on, the better, as I have yet to find a book or course that is
simplistic enough for me to wrap my aging mind around it.

Dean
 

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