PDA

View Full Version : VBA code to start a section in new page, always



volabos
01-03-2008, 10:39 PM
Hi all, I would like to tell the story of my problem, if anyone have a look on it and, help me out, I will be really grateful.

I have a word file with 5 pages (currently of course). It is divided in two sections. 1st section is from page 1-3 and next section is on page 4-5. For the first section, page 1 and 2 are completely filled and 3rd page is half filled. Now I want to do following :

If I want to add more sentences (anywhere in first section) in first section, as long as end of this section keeps remaining in page 3, start of the second section would be at the beginning of page 4. as soon as first section crosses page 3 (ie come to page 4), start of the second section should directly jump to page 5. i.e. now second section would start at line-1 of page 5 and end at page 6. and so on.

Definitely I can do it manually, by pressing lot of ENTERS before the beginning of second section so that all the time, second section starts from line-1 of a fresh page. However this is very cumbersome for a long document. Therefore I want to automate this process. Is there any VBA code or anything else for that?

Hope I could explain my problem efficiently. However if anyone need more clarification please let me know.

Thanks and Regards,
:think:

TonyJollans
01-04-2008, 01:22 AM
When you insert the section break just make sure it's a new page section break. Or do you want some VBA to do this on lots of documents?

volabos
01-04-2008, 02:01 AM
If possible, then I would like to have some VBA code because, in my document I have lot of sections, total 25

Regards,

TonyJollans
01-04-2008, 03:51 AM
Try this ...
For Each s In ActiveDocument.Sections
s.PageSetup.SectionStart = wdSectionBreakNextPage
Next

fumei
01-04-2008, 11:02 AM
...with s declared....

Dim s As Section
For Each s In ...

"It is divided in two sections. 1st section is from page 1-3 and next section is on page 4-5. "


Have to ask. When you state "section", do you mean a real Section? Is there an existing SectionBreak between page 3 and page 4? Tony's code will work with that situation.

As another option, you could consider using a explicit Style for the opening text of a "section". Set its Paragraph attribute to Page break before. Now that text will always have a page break immediately prior to it, regardless of what you do, or where you move it.

TonyJollans
01-04-2008, 12:33 PM
I did wonder about the sections - whether they were real or not; if not then that's a good solution.

And, of course, mea culpa for the missing declaration :)

fumei
01-04-2008, 02:39 PM
mea culpa is not needed. I was delighted to be able to toss a (hopefully understood) playful comment.

It is one of the things that really bugs me with VBA Help. There are lots of code examples that have undeclared variables. Unless you know the score (and you are using Option Explicit) it can cause hair pulling trying to figure out why the darn thing is not working. I know this now, but when I was first starting out there was much confusion.

RE: the "sections". Yup. If they are real Sections (as Word understand this), then it can be dealt with as Sections. If they are, ummmm, typographical? textual? content-dividing "sections", then it is another issue.

TonyJollans
01-04-2008, 03:00 PM
I'm always up for a bit of banter - and you're right, about all of it really.

volabos
01-05-2008, 09:51 AM
Thank you everyone who have a look on my problem especially Tony and Fumei. However I have decided to go with inserting a page break with new page section break. I think this will be sufficient for my problem. I want to keep things simple :)

Regards,