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