PDA

View Full Version : Need Help removing text after page break



Logandouglas
02-02-2015, 03:40 PM
Hey all, I've been trying to crack out a macro to automatically reformat a document after being imported into word. I've so far managed to reformat the page and put a header in the first page. Since I made the header I now have redundant information on the subsequent pages that I want to get rid of. Is there a way to find just the page breaks and delete the next 13 "paragraphs" as denoted by the symbol in formatting? The document is an unknown number of pages and I've had to pull together bits and pieces of information from around the web. The problem is, the loop keeps detecting simple paragraph returns instead of page breaks, regardless of what I put in the .text = "" and thus deletes random chunks of my documents. Any advice would be greatly appreciated.


Dim i As Integer
Dim pgCount As Integer
pgCount = ActiveDocument.ActiveWindow.ActivePane.Pages.Count
Selection.HomeKey Unit:=wdStory
i = 1
Do While (i < pgCount)
i = i + 1
With ActiveDocument.Range.Find
.Text = Chr(12)
.Wrap = wdFindContinue
End With
Selection.Find.Execute
Selection.MoveRight Unit:=wdCharacter, Count:=2
Selection.MoveDown Unit:=wdLine, Count:=13, Extend:=wdExtend
Selection.Delete
Loop

gmayor
02-04-2015, 12:01 AM
The question is impossible to answer without knowing how the document is formatted.
Are the page breaks section breaks? Manual page breaks? Or just breaks caused by text flow? Either way if you are removing parts of pages, the page count will change as you process, so process from the end of the document back to the beginning.
Are the '13 paragraphs' empty or do they contain text? If the former simply wildcard search for ^13{13} and replace with nothing. If the latter, how is that text identified?

Logandouglas
02-04-2015, 07:31 AM
Sorry I didn't provide enough info, I wasn't really sure what you would need to help. The page breaks appear as .........Page Break......... followed by a pilcrow, so the page count doesn't change as I adjust the formatting of each page.

The '13 paragraphs' are lines of text constituting a heading that was reproduced on every page and instead of reformatting it again for each page I took it off the first page and inserted it into the header and need to delete the redundant header on the following pages.

That said I have found a work around. The program that generates the file only puts in the page break/second page if there are more than 58 line items or 'paragraphs' including the header.


If ActiveDocument.BuiltInDocumentProperties(wdPropertyPages) > 1 Then
Set aRange = ActiveDocument.Range( _
Start:=ActiveDocument.Paragraphs(45).Range.Start, _
End:=ActiveDocument.Paragraphs(58).Range.End)
aRange.Delete
End If