Using VBA to split a document into sections

C

Colin Hayes

Hi

I need to run a small macro to split a passage into sections each
containing 250 characters (including spaces) or less.

Ideally it would split the passage on the page with a couple of carriage
return between each section.

Is this possible?

Grateful for any help.



Best Wishes


Colin
 
J

Jay Freedman

Hi Colin,

If the split should occur somewhere other than exactly at the 250th
character, what determines where it is?

Also, from your second pararaph, I assume that you don't want to
insert _section breaks_ (what Word considers a "section") but just "a
couple of carriage returns" (paragraph marks). Are you sure about
this? What's the point of the exercise -- what will you do with the
document after the macro runs?
 
C

Colin Hayes

Jay Freedman said:
Hi Colin,

If the split should occur somewhere other than exactly at the 250th
character, what determines where it is?

Also, from your second pararaph, I assume that you don't want to
insert _section breaks_ (what Word considers a "section") but just "a
couple of carriage returns" (paragraph marks). Are you sure about
this? What's the point of the exercise -- what will you do with the
document after the macro runs?


Hi Jay

Thanks for getting back.

I'm actually splitting a variety of passages of varying lengths so I can
paste the sections into Access. The maximum it will allow in any one
cell is 250 characters. That's the reason for the split at that point. I
would paste each part into consecutive cells in Access.

As the size of the passage I'm splitting will vary , it may be that if
it's less than 250 then it won't have to split it in any case. Also ,
as it comes to the last section it will be that this has fewer than the
250 characters. These would be the reasons why it might need to split at
somewhere other than 250 characters. The macro would need to recognise
that the split would be at up to 250 , but not fall over if it's less.
If you see what I mean!

It would have to count characters and space and divide accordingly. A
passage of 300 characters would give two paragraphs of 250 and 50
separated on the page. And so on. A passage of less than 250 would be
left as is.

It would very useful to run a macro which would divide whichever length
of passage it finds into parts of 250 , and a remainder paragraph and
then just arrange them with a double space between each so I can paste
from there. I'm not sure I'm using the word 'section' in the 'Word'
sense here - you're right on that.

Grateful if you can help.


Best Wishes


Colin
 
J

Jay Freedman

Hi Jay

Thanks for getting back.

I'm actually splitting a variety of passages of varying lengths so I can
paste the sections into Access. The maximum it will allow in any one
cell is 250 characters. That's the reason for the split at that point. I
would paste each part into consecutive cells in Access.

As the size of the passage I'm splitting will vary , it may be that if
it's less than 250 then it won't have to split it in any case. Also ,
as it comes to the last section it will be that this has fewer than the
250 characters. These would be the reasons why it might need to split at
somewhere other than 250 characters. The macro would need to recognise
that the split would be at up to 250 , but not fall over if it's less.
If you see what I mean!

It would have to count characters and space and divide accordingly. A
passage of 300 characters would give two paragraphs of 250 and 50
separated on the page. And so on. A passage of less than 250 would be
left as is.

It would very useful to run a macro which would divide whichever length
of passage it finds into parts of 250 , and a remainder paragraph and
then just arrange them with a double space between each so I can paste
from there. I'm not sure I'm using the word 'section' in the 'Word'
sense here - you're right on that.

Grateful if you can help.


Best Wishes


Colin

The following macro will separate the text into as many chunks to 250
characters as possible, leaving the final chunk at whatever smaller
size is necessary. The separation is two paragraph marks, which aren't
counted in the 250 characters. If the split point occurs in the middle
of a word, that's where the paragraph marks will be inserted -- if you
want the macro to look for spaces between words and only split there,
that's a bit more work.

Sub SplitIntoChunks()
Const chunkSize = 250
Dim oRg As Range
Dim actualSize As Long

Set oRg = ActiveDocument.Range
With oRg
.Collapse wdCollapseStart
actualSize = .MoveEnd(Unit:=wdCharacter, Count:=chunkSize)
Do While actualSize = chunkSize
.InsertAfter vbCr & vbCr
.Collapse wdCollapseEnd
actualSize = .MoveEnd(Unit:=wdCharacter, Count:=chunkSize)
Loop
End With
End Sub
 
D

Doug Robbins - Word MVP

Please do not post the same question separately to multiple newsgroups. I
have just responded to your post in the vba beginners newsgroup only to find
that Jay has already answered it here.

HOWEVER, it seems to me that you should be using a Memo type field in Access
and then the 250 character restriction will NOT apply.

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

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