PDA

View Full Version : VBA script to split document at Heading2



MikeLC
03-05-2012, 07:34 AM
I've read the posting "Split a mail merge document into subdocuments" at

answers.microsoft.com/en-us/office/forum/office_2003-word/split-a-mail-merge-document-into-subdocuments/898e11ce-5d81-4ca1-a2a2-8ee86f5273b2

and Graham Skan's VBA Express article getarticle.php?kb_id=727

But I'd like to use a VBA script to split a huge Word document at the Heading2 items.

I'd like each Heading2 section to be put into a separate subdocument that still maintains the section numbering from the original, main document.

The VBA scripts that I've seen break the main document at page breaks.

How can the VBA script be modified to create subdocuments at the Heading2 level?

Thanks, Mike

macropod
03-09-2012, 12:25 AM
Hi Mike,

You could, of course, insert a manual page break before all Heading2 items (eg via Find/Replace), then use the existing document-splitting macro that uses page breaks. A bigger issue, though, is going to be the maintenance of the heading numbers. For each document, you're going to have to either tell Word what number to start Heading2 in that document from, or delete the auto-numbering and insert fixed numbers programmatically.

MikeLC
03-12-2012, 11:51 AM
Thanks for the reply.

I have other breaks in the document to change from portrait to landscape, back to portrait, and so on. There may be some page breaks already in the document. And there are tables.

Adding new page breaks probably wouldn't be sufficient.

I do have some control over the XML source for the Word document, and would try to use fixed heading numbers if I knew which WordML elements to use.

Thanks, Mike

macropod
03-12-2012, 07:19 PM
Hi Mike,

Having pages with different orientations adds a whole new dimension to the problem. That tells me the document breaks required every time a Heading2 is encountered also requires consideration of page layout issues including, presumably, differences in page headers and footers. And, of course, if you have cross-refernces that refer to a destination outside the range contained in the new document, you'll end up with a broken link in every case. Right now, code to account for all of these issues would take more time than I have available. Maybe someone else can take it on. The alternative, of course, is to do the job manually.

MikeLC
03-14-2012, 05:39 AM
Thanks for considering this request.

I was hoping to be able to do something like:
1. Put cursor at beginning of first heading.
2. Search to the next heading and select all that text.
3. Copy text to new document and save it.
4. Repeat from 2.

But VBA examples that I've seen indicate that this is difficult in scripting, but maybe not so hard manually.

The document I'm working with is huge (80MB), so manual processing would be tedious.

Thanks again,
Mike