Continuous section break

L

LEU

I have documents that I am trying to clean up. They have numerous Section
Break(Next page) and Section Break(Continuous) in them. I am trying to write
a macro to just replace the Section Break(Continuous) with a hard return.
What I have written below replaces all the Section Breaks. How do I replace
only the Section Break(Continuous)?

Dim SearchRange As Range
Set SearchRange = ActiveDocument.Range
With SearchRange.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = "^b"
.Replacement.Text = "^p"
.Execute Replace:=wdReplaceAll
End With
 
L

LEU

Hi Helmut,

I'm not sure what you mean when you said to try "KinfofBreak12". I looked at
the site but I didn't understand it. Could you give me more detail on how to
write this?

Thanks
LEU
 
H

Helmut Weber

Hi LEU,
I'm not sure what you mean when you said to try "KinfofBreak12". I looked at
the site but I didn't understand it. Could you give me more detail on how to
write this?

You search for chr(12).
If found, you feed the found range of this chr(12)
into the function kindofbreak12.


The function returns a long number,
which represents the kind of pagebreak,
as the pagebreak character itself
can't tell you what kind of pagebreak it stands for.
What kind of pagebreak it is, is defined
in the section's pagesetup sectionstart.

In addition, if the found character 12
and the next character are in the same section,
then we got an ordinary pagebreak.

You may start with something like the code below.

Couldn't get it to work with range,
no matter how hard I tried,
maybe somebody else is smarter than me today.

This somebody should think of any number
of continuous pagebreaks and other pagebreaks
not separated by any other character.
Here and now it is over my head.

HTH nevertheless:

Public Function KindofBreak12(ByVal rtmp As Range) As Long
Dim lSct1 As Long ' counter for sections
Dim lSct2 As Long ' counter for sections
rtmp.start = ActiveDocument.Range.start
lSct1 = rtmp.Sections.Count ' count sections
rtmp.End = rtmp.End + 1 ' extend range
lSct2 = rtmp.Sections.Count ' count sections again
If lSct1 = lSct2 Then ' next caracter is in the same section
KindofBreak12 = -1 ' ordinary pagebreak
Else
KindofBreak12 = _
ActiveDocument.Sections(lSct2).PageSetup.SectionStart
End If


'wdSectionContinuous ' 0
'wdSectionNewPage ' 2
'wdSectionEvenPage ' 3
'wdSectionOddPage '4

End Function

Sub Test90012A()
ActiveDocument.Range(0, 0).Select
With Selection.Find
.Text = "^0012"
While .Execute
If KindofBreak12(Selection.Range) = 0 Then
Selection.Text = Chr(13)
Selection.End = ActiveDocument.Range.End
End If
Wend
End With
End Sub

Don't forget to reset search options beforehand, like

Public Sub ResetSearch()
With Selection.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = ""
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Execute
End With
End Sub

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

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

LEU

Hi Helmut

I copied tried your macro and and ran it. It goes through the document and
finds all of the Section Breaks but it does not replace the Section
Break(Continuous) with a hard return. I must be missing something.

LEU
 
H

Helmut Weber

Hi LEU,

this is a nightmare.

My macro finds all instances of section break (continuous),
and replaces them with a paragraph mark, IMHO.

However, if a section break (continuous) is preceded
by e.g. a section break (even page), this break turns
into a new section break (continuous).
So, one would have to remember the kind of section pagesetup
of the actual section and restore it after replacing
the section break (continuous).


Sub Test90012A()
Dim lTemp As Long
Dim xTemp As Range
Set xTemp = ActiveDocument.Range
ActiveDocument.Range(0, 0).Select
With Selection.Find
.Text = "^0012"
While .Execute
If KindofBreak12(Selection.Range) = 0 Then
xTemp.End = Selection.Range.End
lTemp = xTemp.Sections.Last.PageSetup.SectionStart
Selection.Text = Chr(13)
xTemp.Sections.Last.PageSetup.SectionStart = lTemp
Selection.End = ActiveDocument.Range.End
End If
Wend
End With
End Sub

Seemed to be a simple question at first.
But as it happens, the complications are endless.

Once again, all co-readers are invited.

This is driving me up the wall...

--
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