Detect empty page and delete?

E

Ed

I have a macro that runs through a database output report that can be
several hundred pages long and inserts a page break at a certain text
string. Sometimes, because of the way the database spits out the info and
Word translates it into text, I wind up with a blank page. Actually, it's
several blank paragraph marks and a page break. Is there an easy way to
detect a page with nothing on it except blank paragraphs and a page break,
and delete the empty paragraphs until the page break is at the bottom of the
previous page?

Ed
 
J

Jean-Guy Marcil

Hi Ed,

The following should get you going:

'_______________________________________
Sub Remove_Page_Paragraph_Mark()

Dim CurDoc As Document
Dim Pagerange As Range
Dim MsgDelete As Long

'set current document
Set CurDoc = ActiveDocument

'set page range to current page
'reduce range by one to remove page break from selection
Set Pagerange = _
CurDoc.Range(CurDoc.Bookmarks("\page").Range.Start, _
CurDoc.Bookmarks("\page").Range.End - 1)

'Remove all Carriage returns
If Replace(Pagerange, Chr(13), "") = "" Then
MsgDelete = MsgBox("This page contains nothing " _
& "but paragraph marks!" _
& vbCrLf & "Do you want to delete it?", _
vbYesNo + vbInformation, "Hey ho!")
If MsgDelete = 6 Then 'User said yes
'reintroduce page break to range
Set Pagerange = _
CurDoc.Range(CurDoc.Bookmarks("\page").Range.Start, _
CurDoc.Bookmarks("\page").Range.End + 1)
Pagerange.Delete
End If
Else
MsgBox "This page contains other characters than " _
& "pararaph marks.", vbInformation, "Hey ho!"
End If
'Note: You will have to delete the last page manually as it
'may contain a single paragraph mark,
'in which case you have to delete the preceding page break

End Sub
'_______________________________________

HTH
--
Cheers!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
E

Ed

Thank you. Can I wrap this in a loop to check each page? Maybe:
For i=1 to Pages.Count -1
Pages(i).Select
' do code
Next i

(I can't get the loop to work.)
Ed
 
J

Jean-Guy Marcil

Hi Ed,

No, you can't... there is no such thing as a page collection in Word. Here
is a nice Russian doll description: Word is a collection of documents, a
document is made of a collection of stories, a story can be made of a
collection of sections (for main story types), a section is made of a
collection of paragraphs, a paragraph is made of a collection of words, a
word is made of a collection of characters!

A page is just a convenience for printing on paper, the actual content is
always changing as you change the margins, the font size, an object size,
add/delete text, etc.

You are going to have to look at your code and decide how to best check for
"blank" pages.

Since I do not know your particulars, I can only guess that the easiest
would be to check each page as you create them by calling a function that
does more or less what I suggested in my previous post.
Alternatively, you could figure out a way to scan each page after your main
operation is finished... Maybe by finding each page break, and then applying
the code I suggested.

HTH
--
Cheers!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
E

Ed

Okay - so *that's* why the Help file didn't tell me how to do this! The
Pages collection I was looking at is in the Multiage object, which has to do
with Control Boxes (I think I got that right!). Nothing, in any case
though, to do with a document.

Let's see - the GoTo Page Number command takes me to the first character
position on that page. Could I:
Dim cntPages As Integer
Dim PgNo As Integer
cntPages = ActiveDocument.BuiltInDocumentProperties(wdPropertyPages)
PgNo = 1

Do
ActiveDocument.Goto What:=wdGoToPage, Which:=wdGoToAbsolute, Count:=PgNo
Selection.MoveEndUntil ' **until it hits a page break - ^m??**
' run code

PgNo = PgNo + 1
Loop Until PgNo > cntPages

If this would work, would I have to set Selection.Range and run the code
within the range?

Ed
 
J

Jean-Guy Marcil

Hi Ed,

Almost there!

Try this instead:
'_______________________________________
Dim cntPages As Integer
Dim PgNo As Integer
cntPages = ActiveDocument.BuiltInDocumentProperties(wdPropertyPages)
PgNo = 1

'To place cursor at beginning of doc
Selection.HomeKey wdStory

Do

' run code before going to the next page
'put the Dim statements from my code at the top with your Dim
'put the rest of the code (modified to fit your needs) here
'You do not need to select the page,
'my code does that with the line:
'Set Pagerange = _
' CurDoc.Range(CurDoc.Bookmarks("\page").Range.Start, _
' CurDoc.Bookmarks("\page").Range.End - 1)

'go to next page before looping
Selection.GoTo What:=wdGoToPage, Which:=wdGoToNext, _
Count:="1"
PgNo = PgNo + 1

Loop Until PgNo > cntPages
'_______________________________________

HTH
--
Cheers!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
E

Ed

Beautiful! A few small modifications:
I recounted pages (cntPages) after running THEN Pagerange.Delete.
Otherwise, my counters would be off because my page count has changed.
I put the PgNo+1 and GoTo Next page within ELSE. That way, if I ran
THEN, the page was checked again without changing pages, because the
information on the page changed. But if ELSE, the PgNo counter changed and
the code moved on.

On a 500-pg document, *this* will save MUCH time! Thanks so much!
Ed
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top