Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

WORD - Inserting Page Break at end of defined text

Status
Not open for further replies.

haedyr

Programmer
May 25, 2005
18
US
I've just started programming and this is my first shot solo.
I'm trying to insert a page break at every instance where "Total Amount Payable:" occurs. I've gone to different pages to start the code...but with the code I have now, nothing executes, the cursor just ends up at the end of the doc. Here it is (don't laugh)

Public Sub Page_Breaks()


Do Until ActiveDocument.Bookmarks("\Sel") = ActiveDocument.Bookmarks("\EndOfDoc")

Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "Total Amount Payable:"
.MatchWildcards = False
.Replacement.Text = ""
.Wrap = wdFindStop
.Forward = True
End With
Selection.Find.Execute
If Selection.Find.Found = True Then
Selection.InsertBreak (wdPageBreak)
Else
Exit Do
End If
Loop

End Sub


Please let me know what I'm doing wrong....AGAIN, first timer.

Thanks! Haedyr
 
Hi Haedyr,

Only a couple of small problems. Your code almost works, but:

It doesn't start at the beginning of the document. To make it do this add this line at the beginning:
Code:
[blue]Selection.HomeKey Unit:=wdStory[/blue]
When you do this you will find that all occurrences of your string are replaced by page breaks - probably not what you want! To fix this you must deselect the string which the Find finds before inserting the page break:
Code:
[blue][green]      :[/green]
[green]      :[/green]
      If Selection.Find.Found = True Then
         Selection.Collapse wdCollapseEnd
         Selection.InsertBreak (wdPageBreak)
      Else
[green]      :[/green]
[green]      :[/green][/blue]

Now you can, of course make the code simpler!! In the interests of learning I'll let you do it yourself. Record a macro doing a Replace (Ctrl+h). Find your text and enter a "Replace with" of [blue]^&^m[/blue] and just press Replace All. The [blue]^&[/blue] means the text that was found and the [blue]^m[/blue] means a manual page break - both can be selected from the "Special" button on the Replace Dialog (if you don't see it, press More)

Do come back if you have any further problems with it.

Enjoy,
Tony

--------------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.
Excel VBA Training and more Help at VBAExpress[
 
Wonderful!!! It worked. Thank you Tony.

Here's what it ended up looking like in case you are curious.
I do have a question as to why I need to clear formatting? What exactly does that do?

Public Sub Page_Breaks()

Selection.HomeKey (wdStory)

Do Until ActiveDocument.Bookmarks("\Sel") = ActiveDocument.Bookmarks("\EndOfDoc")

Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "Total Amount Payable:"
.MatchWildcards = False
.Replacement.Text = ""
.Wrap = wdFindStop
.Forward = True
End With
Selection.Find.Execute
If Selection.Find.Found = True Then
Selection.EndKey Unit:=wdLine, Extend:=wdMove
Selection.Collapse (wdCollapseEnd)
Selection.InsertBreak (wdPageBreak)
Else
Exit Do
End If
Loop

End Sub

I really appreciate your help. I'll be posting again soon as I have a more difficult macro to write by monday.
Haedyr
 
Hi Haedyr,

I'm inclined to disagree with Gerry there.

Selection.Find is the code equivalent of using the Find/Replace Dialog in Word. Word remembers all the settings that were used last time Find was used. You can never be sure what the user (or any code they ran) did last time so any kind of formatting may have been (and remain) set.

In the dialog you can see what formatting is set (it is written under the Find What box) and if there is any, the Clear Formatting button is available to press to clear it (otherwise it's greyed out). In code you can't see anything so you use .ClearFormatting to make sure your Find is not being restricted by only looking for text in a particular format.

In principle I believe that code should not permanently alter a user's environment other than in explicitly documented ways (obviously it does something) and I would prefer, not only to clear the formatting before using the Find, but to set it back afterwards (although I confess I don't usually do this).

Enjoy,
Tony

--------------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.
Excel VBA Training and more Help at VBAExpress[
 
Thank you Tony. I appreciate your thoroughness when responding to my questions.
 
When Tony disagrees with you, it is time for serious thought.

1. I exclusively use styles, so I always include a Style (parameter) in any search, but not as a Find parameter, but an IF statement.

2. most search are, in fact, search for text itself. However, it is absolutely true that if you include a format parameter in your Find/Replace it does, in fact persist. I offer in my defence (a weak one I admit) that people rarely include a format parameter in their search. Maybe I am wrong.

However, once again Tony displays a clarity that is worthy of attention.

Gerry
See my Paintings and Sculpture
 
Hi Haedyr,

As per my reply on deleting empty spaces, you could use very similar code for this one too:

Sub Test2()
Dim MyRange As Range, i As Integer
With ActiveDocument
For i = 1 To .BuiltInDocumentProperties(wdPropertyPages)
Set MyRange = .GoTo(What:=wdGoToPage, Count:=i)
MyRange.Select
Selection.GoTo What:=wdGoToBookmark, Name:="\page"
With Selection.Find
.ClearFormatting
.Execute FindText:="Total Amount Payable:", MatchWildcards:=True, Forward:=True, ReplaceWith:="^m"
End With
Next i
End With
End Sub

Cheers
 
Hi Haedyr,

I've been thinking more about what you're trying to accomplish with this one too. Again, unless you have a particular need to process each page individually, the following code would be much more efficient:

Sub Test3()
With ActiveDocument
.Select
With Selection.Find
.ClearFormatting
.Execute FindText:="Total Amount Payable:", MatchWildcards:=True, Forward:=True, ReplaceWith:="^m"
End With
End With
Selection.HomeKey (wdStory)
End Sub

Cheers
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top