VBA to enter page breaks

N

ND Pard

I have a Word 2007 file.

Located at the beginning of a sentence, may be found the text: 08/09/09.

The text ‘may’ be the first item in the document.

I need a macro that will put a page break before all text of “08/09/09â€,
unless it is at the beginning of the document.

Please help. Thanks in Advance.
 
T

Tony Jollans

Do you really need a macro?

There is no way to simply find the start of a sentence with Find and
Replace, but, unless you have the string in the middle of sentences as well
(and don't want it replaced there) finding it preceded by a space is almost
the same. If this is not suitable then code would be something like:

With ActiveDocument
For Each Sent In .Range(.Range.Sentences(2).Start, .Range.End).Sentences
If Left(Sent.Text, 8) = "08/09/09" Then _
.Range(Sent.Start, Sent.Start).InsertBreak wdPageBreak
Next
End With
 
D

DaveLett

Hi ND,
You should be able to use something like the following:

Dim oRng As Range

Set oRng = ActiveDocument.Range
oRng.MoveStart Unit:=wdParagraph, Count:=1

With oRng.Find
.ClearFormatting
.Text = "([0-9]{2,2}\/[0-9]{2,2}\/[0-9]{2,2})"
.MatchWildcards = True
With .Replacement
.ClearFormatting
.Text = "^m\1"
End With
.Execute Replace:=wdReplaceAll
End With

HTH,
Dave
 
D

DaveLett

Hi Tony,
Finding something at the beginning of the sentence is relatively easy with
Find/Replace. Sentences have end punctuation (.!?), so you could use the
following wildcard search:

([.\?\!] {1,})([0-9]{2,2}\/[0-9]{2,2}\/[0-9]{2,2})

You would then have to account for sentences that begin a paragraph. Even
two replaces would be much faster than cycling through all of the sentences
in a document, don't you think?

Dave
 
T

Tony Jollans

Well, it depends on your document but sentences can start following
graphics, section breaks, etc., as well as paragraph marks. In US English
(though less so in UK English) a sentence can end with a closing quote. Then
there are other whitespace characters (tabs, for example) that can precede
the start of a sentence; the list goes on, and a comprehensive solution is
far from easy.

My guess in this case is that using F&R for space followed by the date would
probably suffice as it isn't really a bit of text that often occurs in (or,
for that matter, starts) a sentence, and I do agree that it would be much
quicker than walking the sentences collection.

--
Enjoy,
Tony

www.WordArticles.com

DaveLett said:
Hi Tony,
Finding something at the beginning of the sentence is relatively easy with
Find/Replace. Sentences have end punctuation (.!?), so you could use the
following wildcard search:

([.\?\!] {1,})([0-9]{2,2}\/[0-9]{2,2}\/[0-9]{2,2})

You would then have to account for sentences that begin a paragraph. Even
two replaces would be much faster than cycling through all of the
sentences
in a document, don't you think?

Dave

Tony Jollans said:
Do you really need a macro?

There is no way to simply find the start of a sentence with Find and
Replace, but, unless you have the string in the middle of sentences as
well
(and don't want it replaced there) finding it preceded by a space is
almost
the same. If this is not suitable then code would be something like:

With ActiveDocument
For Each Sent In .Range(.Range.Sentences(2).Start,
.Range.End).Sentences
If Left(Sent.Text, 8) = "08/09/09" Then _
.Range(Sent.Start, Sent.Start).InsertBreak wdPageBreak
Next
End With

--
Enjoy,
Tony

www.WordArticles.com
 
N

ND Pard

Thanks fellas ... with your help I modified it to the following and it worked
GREAT!!!

Sub PgBrk_Macro()

‘declare a variable named oRng
Dim oRng As Range

‘set the variable: oRng to be the entire document
Set oRng = ActiveDocument.Range

‘Move to the start of the document (oRng)
oRng.MoveStart Unit:=wdParagraph, Count:=1

With oRng.Find
.ClearFormatting
.Text = "([0-9]{2,2}\/[0-9]{2,2}\/[0-9]{2,2})"
.MatchWildcards = True
‘find the 1st instance of the text
Selection.Find.Execute
‘find the 2nd instance of the text
Selection.Find.Execute
With .Replacement
.ClearFormatting
‘the replacement text will be a Page Break
.Text = "^m\1"
End With
‘Replace all the rest
.Execute Replace:=wdReplaceAll
End With


End Sub
 

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