nextPage sectionBreak

F

FotoArt

hello everyone

I have a document which I need to break into sections(nextPage).
Im doing a search for 3 asterix marks and when found, insert the
sectionbreak.
I need to run the code until the end of document only once everytime it is
run.
Do I have to delete the existing section breaks first.

heres the code I have so far. It keeps looping nonstop

Sub letsDivide()
Selection.HomeKey wdStory
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
Do While .Execute(FindText:="***", Forward:=True, Wrap:=wdFindContinue,
MatchWildcards:=False _
) = True
Loop
End With
Selection.InsertBreak Type:=wdSectionBreakNextPage
End Sub
Any help will be appreciated.

thanx
ahmed
 
H

Helmut Weber

Hi FotoArt,

if you search for a string
and the string is found,
and you do nothing to what is then the selection,
then the selection stays the found range,
and your search finds the string again.

Thats the way I'do it:

Sub LetsDivideA()
Dim rDcm As Range
Set rDcm = ActiveDocument.Range
With rDcm.Find
.Text = "***"
While .Execute
rDcm.InsertBreak Type:=wdSectionBreakNextPage
rDcm.Start = rDcm.End + 1 '!
rDcm.End = ActiveDocument.Range.End '!
Wend
End With
End Sub

Pay special attention to the part of the code,
which redefines rDcm.

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 
F

FotoArt

hello weber
How could I insert the section break after the asterics.
What happens now is the break comes before the "***"
thanx
ahmed
 
H

Helmut Weber

Hi Ahmed,

like that:

Sub LetsDivideAA()
Dim rDcm As Range
Set rDcm = ActiveDocument.Range
With rDcm.Find
.Text = "***" & Chr(12)
While .Execute
rDcm.Select ' for testing using single step mode
rDcm.Characters.Last = ""
rDcm.Collapse direction:=wdCollapseEnd
rDcm.Select ' for testing using single step mode
rDcm.InsertBreak Type:=wdSectionBreakNextPage
rDcm.Collapse direction:=wdCollapseEnd
rDcm.Select ' for testing using single step mode
Wend
End With
End Sub

Which works, beware, on every chr(12) preceded by 3 asteriscs,
no matter, what kind of break it is.

For learning about the whole truth about chr(12), see:

KindOfBreak12
http://tinyurl.com/ynwgnv


--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 

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