0

How can I test whether the insertion point is at the start of a new page created by a manual page break? It seems like it should be as simple as checking if the preceding character is CHR(12), but that doesn't seem to work.

If Selection.Type = CHR(12) Then
  Selection.TypeText Text:="HARD PAGE"
Else
  Selection.TypeText Text:="NO HARD PAGE"
End If 

Is it just a syntax error or do I have the wrong approach here?

braX
  • 11,506
  • 5
  • 20
  • 33
Wally Walters
  • 75
  • 1
  • 1
  • 8

2 Answers2

1

You have to move the selection (or the range) backwards. Selection.Text (or Range.Text) always returns the character following the IP. Of course, you may not want to actually move the selection. That means you can work with a Range object to do the testing.

Since you have to move backwards, anyway, to test whether there's a hard pagebreak, I've put it in a loop so that the selection can be anywhere on the page, to begin with.

Also, I've added a check whether the macro has started on the first page, since you'd otherwise go into an infinite loop, moving backwards from the Selection to the next page.

Sub CheckWhetherHardPageBreak()
    Dim rngToCheck As word.Range
    Dim pgNr As Long
    Dim pgNrChange As Long

    Set rngToCheck = Selection.Range
    pgNr = rngToCheck.Information(wdActiveEndPageNumber)
    If pgNr = 1 Then
        MsgBox "Can't start on Page 1"
        Exit Sub
    End If

    pgNrChange = pgNr
    Do While pgNrChange = pgNr
      rngToCheck.MoveEnd wdCharacter, -1
      pgNrChange = rngToCheck.Information(wdActiveEndPageNumber)
    Loop
    'Extend the selection to include the following character
    'So that ASC() works
    rngToCheck.MoveEnd wdCharacter, 1
    If Asc(rngToCheck.Text) <> 12 Then
        'Move it back before the previous character
        'as the character immediately following a hard page break is Chr(13)
        rngToCheck.MoveEnd wdCharacter, -2
    End If
    rngToCheck.MoveEnd wdCharacter, 1

    If Asc(rngToCheck) = 12 Then
      Selection.TypeText Text:="HARD PAGE"
    Else
      Selection.TypeText Text:="NO HARD PAGE"
    End If
End Sub
Cindy Meister
  • 25,071
  • 21
  • 34
  • 43
  • I found that `If rngToCheck = Chr(12)` didn't work, but `If Asc(rngToCheck) = 12`did. Also, is that "-2" a typo? That makes the first character after a hard break also be treated as a page break, while changing it to "-1" works properly. In any event, thanks for your guidance. – Wally Walters Jan 26 '18 at 23:05
  • That makes sense, but oddly enough it worked in my tests... It's midnight here, but I'll double-check tomorrow and edit the code if/when I can confirm that. – Cindy Meister Jan 26 '18 at 23:08
  • @WallyWalters Both work for me. But if only the one works for you I can edit my Answer :-) – Cindy Meister Jan 27 '18 at 12:21
  • Appreciate that, but it's not necessary. Probably best to leave the entire thread as it is in case someone else happens upon it and my changes don't work for them, but your original code does. – Wally Walters Jan 28 '18 at 19:24
-1

I think you might be intending to use Chr(13) or Chr(10).

More information here:

ashleedawg
  • 20,365
  • 9
  • 72
  • 105