PDA

View Full Version : Solved: Empty Paragraphs



Opv
06-26-2012, 06:56 PM
I've only recently delved into VBA in WORD, although I've worked on a number of projects in Excel. I'm finding my way around OK; however, I've run into a phenomenon that I can't seem to resolve on my own.

Whenever I insert a Page Break or a Section Break, there is a resulting empty paragraph at the top of the newly created next page. Prior to utilizing VBA in Word, I always manually deleted these paragraphs so that my already existing Headings would be properly placed at the top of their respective pages. For some reason, the resulting empty paragraphs do not get captured when I loop through the document and delete empty paragraphs. My code is otherwise working as it deletes all other empty paragraphs except created in association with inserting the Page Breaks and Section Breaks. Is there something that distinguishes such paragraphs from other paragraphs in the document that would cause them not to to be recognized as empty? Here is my existing code:


Sub DeleteFirstParagraphsIfEmpty()

Dim d As Document, p As Paragraph

Set d = ThisDocument

Selection.HomeKey wdStory

For Each p In d.Paragraphs

If Len(p.Range) = 1 Then p.Range.Delete
Set p = p.Next
Next

End Sub

macropod
06-26-2012, 10:06 PM
Whenever I insert a Page Break or a Section Break, there is a resulting empty paragraph at the top of the newly created next page.
That would normally only be so if the break is being inserted at the very end of the document. The creation of an empty paragraph after a break at the very end of the document is so that there's actually somewhere you can add content; otherwise you'd end up with an inacessible last page.

In any event, if you want your headings to start on a new page, you don't need to insert either a page break or a Section break - simply format the heading Style with the 'page break before' attribute.

Opv
06-27-2012, 08:15 AM
That would normally only be so if the break is being inserted at the very end of the document. The creation of an empty paragraph after a break at the very end of the document is so that there's actually somewhere you can add content; otherwise you'd end up with an inacessible last page.

In any event, if you want your headings to start on a new page, you don't need to insert either a page break or a Section break - simply format the heading Style with the 'page break before' attribute.

Thanks. That resolves the problem nicely, particularly as it relates to Section Breaks. I think I've been using Page Breaks where they are likely not needed, i.e., to keep lines of text together when, for example, only one line of text would otherwise end up on the last page of a particular Section. I'm presuming either KeepTogether and/or WidowControl would handle that issue without the need for forced page breaks. I will give that a try at any rate. Thanks again.