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 / December 2006

Tip: Looking for answers? Try searching our database.

Using VBA to split a document into sections

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Colin Hayes - 28 Dec 2006 23:19 GMT
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
Jay Freedman - 29 Dec 2006 00:05 GMT
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?

--
Regards,
Jay Freedman
Microsoft Word MVP        FAQ: http://word.mvps.org

>Hi
>
[quoted text clipped - 11 lines]
>
>Colin
Colin Hayes - 29 Dec 2006 00:41 GMT
>Hi Colin,
>
[quoted text clipped - 10 lines]
>Regards,
>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

>Microsoft Word MVP        FAQ: http://word.mvps.org
>
[quoted text clipped - 13 lines]
>>
>>Colin
Jay Freedman - 29 Dec 2006 05:35 GMT
>>Hi Colin,
>>
[quoted text clipped - 44 lines]
>
>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

--
Regards,
Jay Freedman
Microsoft Word MVP        FAQ: http://word.mvps.org
Doug Robbins - Word MVP - 29 Dec 2006 08:07 GMT
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.

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

> Hi
>
[quoted text clipped - 11 lines]
>
> Colin

Rate this thread:






 
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.