Results 1 to 11 of 11
  1. #1
    New Lounger
    Join Date
    Oct 2002
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Is current Page empty (Word 2000)

    When I finish with my routine I need to know if the current page has data on the (View = Normal) Page. The page can contain Text, Tables, or Inserted Objects (Picture, Drawings, or Graphs). I just need to know if something is there. If not I would like to delete the page.

  2. #2
    Plutonium Lounger
    Join Date
    Nov 2001
    Posts
    10,550
    Thanks
    0
    Thanked 7 Times in 7 Posts

    Re: Is current Page empty (Word 2000)

    I am trying to imagine what you could be doing in Word to be able to create a page with nothing on it?
    Are you using a manual page break at the beginning and end of the page?

    You could try something like

    If ActiveDocument.Bookmarks("Page").Range.Paragraphs. Count = 1 Then
    If Selection.Paragraphs(1).Range.Characters.Count = 1 And _
    Selection.Paragraphs(1).Range.InlineShapes.Count = 0 And _
    Selection.Paragraphs(1).Range.ShapeRange.Count = 0 Then
    ' Put your code for an empty page here
    End If
    End If

    StuartR

    Edited by StuartR.
    I just tested this code and the ShapeRange.Count gives me an error if there are no shapes, I'm not sure if this is a problem on this PC or some mistake in the code.

  3. #3
    New Lounger
    Join Date
    Oct 2002
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Is current Page empty (Word 2000)

    OK this kind of worked. Here is my code

    ************************************************** **********************
    Private Sub TestBlankPage()
    Selection.GoTo what:=wdGoToPage, which:=wdGoToLast
    If ActiveDocument.Bookmarks("Page").Range.Paragraphs. Count >= 1 Then
    If (Selection.Paragraphs(1).Range.Characters.Count = 1) And _
    (Selection.ShapeRange.Count = 0) Then
    Selection.Bookmarks("Page").Range.Delete
    Selection.TypeBackspace
    End If
    End If
    End Sub
    ************************************************** ********************************
    But on my second application of the Sub the line "Selection.Paragraphs(1).Range.Characters.Coun t" shows a count of two (2)instead of one (1) on that page there are three hard returnes, nothing else on the page. When I ran this while I was testing it, this line would always show (1) like your exaple expected. This is a different section, similar formating.
    What gives?

    In response to your question of what I'm doing, I am building a report (Proposal) the main body of which is mostly text and tables (for formatting purposes). There are up to about 12 sections do to various attachments each can have a different header and page orientations. At the end I build a TOC. I have most of it working great except for the two questions I have posted. After each of my file attachments I seem to get a blank page.

  4. #4
    Plutonium Lounger
    Join Date
    Nov 2001
    Posts
    10,550
    Thanks
    0
    Thanked 7 Times in 7 Posts

    Re: Is current Page empty (Word 2000)

    > "Selection.Paragraphs(1).Range.Characters.Coun t" shows a count of two (2)instead of one (1)

    To find out what these two characters are.
    <UL><LI>Make sure the selection is in the problem paragraph.
    <LI>Go to the Visual Basic window (Alt-F11)
    <LI>View > Immediate to show the immediate window
    <LI>type "? ASC(Selection.Paragraphs(1).Range.Characters(1)"
    <LI>type "? ASC(Selection.Paragraphs(1).Range.Characters(2))"[/list]This will give us the Ascii values of the two characters, which we can look up somewhere like http://www.ascii.cl

    StuartR

  5. #5
    Star Lounger
    Join Date
    Mar 2001
    Location
    Cheltenham, Pennsylvania, USA
    Posts
    99
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Is current Page empty (Word 2000)

    This code determines whether the last page contains anything more than a paragraph mark (which is what typically comprises an unwanted last page). If anything else at all is on the page, the code will leave it there and advise you. If the last page is empty, it will be deleted. Seems to work for me (tested in Word 2000).

    Sub RemoveEmptyLastPage()
    ' Macro recorded 11/1/2002 by langert
    Dim endpage As Long
    Dim currpage As Long
    Selection.GoTo what:=wdGoToPage, which:=wdGoToLast 'goes to first character of last page
    endpage = Selection.Information(wdActiveEndAdjustedPageNumbe r)
    Selection.MoveRight Unit:=wdCharacter, Count:=1, Extend:=wdExtend
    Selection.Delete Unit:=wdCharacter, Count:=1
    currpage = Selection.Information(wdActiveEndAdjustedPageNumbe r)
    If currpage < endpage Then 'the deleted character (presumably a paragraph mark) was the ONLY thing on the old last page
    MsgBox ("Stripped the only character from last page. The last page has been deleted.")
    Else
    MsgBox ("There was more than one character on the last page. No action taken.")
    ActiveDocument.Undo
    Selection.Collapse (wdCollapseStart)
    End If
    End Sub

  6. #6
    Plutonium Lounger
    Join Date
    Nov 2001
    Posts
    10,550
    Thanks
    0
    Thanked 7 Times in 7 Posts

    Re: Is current Page empty (Word 2000)

    This method works if you are only worried about text in paragraphs. If the single paragraph mark on the last page is the Anchor paragraph for a graphic object (such as a text box) then you will delete the object with the paragraph mark.

    StuartR

  7. #7
    Star Lounger
    Join Date
    Mar 2001
    Location
    Cheltenham, Pennsylvania, USA
    Posts
    99
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Is current Page empty (Word 2000)

    Thanks for noticing that oversight. I added something to handle that situation. Can you think of anything else that might cause the same problem?

    One problem, though: If the last page begins with a picture holder (I tested only with a pasted bit of clipart), this code results in a new, empty paragraph mark just in front of the picture. I can't see why this side effect is happening, and I'd be curious to learn what's going on.

    Sub RemoveEmptyLastPage()
    ' Macro recorded 11/1/2002 by langert
    Dim endpage As Long
    Dim currpage As Long
    Selection.GoTo what:=wdGoToPage, which:=wdGoToLast 'goes to first character of last page
    endpage = Selection.Information(wdActiveEndAdjustedPageNumbe r)
    Selection.MoveRight Unit:=wdCharacter, Count:=1, Extend:=wdExtend

    If Selection.Text = "^p" Then
    Selection.Delete Unit:=wdCharacter, Count:=1
    currpage = Selection.Information(wdActiveEndAdjustedPageNumbe r)
    If currpage < endpage Then 'the deleted character (presumably a paragraph mark) was the ONLY thing on the old last page
    MsgBox ("Stripped the only character from last page. The last page has been deleted.")
    End If
    Else
    ActiveDocument.Undo
    Selection.Collapse (wdCollapseStart)
    MsgBox ("There was more than one character on the last page. No action taken.")
    End If

    End Sub

  8. #8
    Plutonium Lounger
    Join Date
    Nov 2001
    Posts
    10,550
    Thanks
    0
    Thanked 7 Times in 7 Posts

    Re: Is current Page empty (Word 2000)

    There are still a few problems with this code...
    <UL><LI>ActiveDocument.Undo happens when you haven't done the delete, causing my last action to be undone
    <LI>The test <font color=blue>If Selection.Text = "^p" </font color=blue> doesn't appear to test for a paragraph mark correctly, maybe you want to try
    <font color=blue>If asc(selection.characters(1)) = 13</font color=blue>
    <LI>I still don't see how avoid deleting the paragraph if there is a graphic anchored in it[/list]StuartR

  9. #9
    Star Lounger
    Join Date
    Mar 2001
    Location
    Cheltenham, Pennsylvania, USA
    Posts
    99
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Is current Page empty (Word 2000)

    All I can tell you is that it works for me, and deletes a last page when it contains nothing other than a paragraph mark. It does NOT delete a last page that contains anything other than a paragraph mark (for example, a picture placeholder). In that instance, the deletion is undone after the page check is performed.

    I've modified the script slightly to use a different check for the paragraph mark and to change the undo behavior a little.

    NOTE that the on-screen indicators of page breaks in Normal view are unreliable (or at least inconsistent), but the page number indicator at the bottom of the screen is accurate. Also, you can see the operation more clearly in the Print Layout view, but it works either way.

    I've attached a test document with the macro included.
    Attached Files Attached Files

  10. #10
    Plutonium Lounger
    Join Date
    Nov 2001
    Posts
    10,550
    Thanks
    0
    Thanked 7 Times in 7 Posts

    Re: Is current Page empty (Word 2000)

    The attached document is exactly the same as the one you posted, except that I have added a text box on the last page. When you run the macro the last page is still deleted.

    StuartR
    Attached Files Attached Files

  11. #11
    Star Lounger
    Join Date
    Mar 2001
    Location
    Cheltenham, Pennsylvania, USA
    Posts
    99
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Is current Page empty (Word 2000)

    OK. I finally stopped rushing, took a careful look at it over lunch, and think I've got it nailed. In the attached doc, try running it with the clipart (anywhere) on the last page, with the textbox anchored to the first paragraph mark on the last page, and with nothing but a paragraph mark on the last page. I think the logic covers all cases that you might encounter, but let me know if you think of more. (It checks for shapes, inline shapes, tables, and plain old paragraphs now.)

    This is much more fun than working.
    Attached Files Attached Files

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •