Determining which type of breaks are present on a page?

M

MrBo

All
I need to programmatically determine if a column break is present on a page.
I'm familiar with the Breaks object but can't seem to use it to get the type
of break.

Any help appreciated.
 
H

Helmut Weber

Hi,
like this or in some other ways:

If InStr(Selection.Bookmarks("\page").Range.Text, Chr(14)) > 1 Then
MsgBox "columnbreak"
End If

For the page the start of the selection on.

Greetings from Bavaria, Germany
Helmut Weber, MVP
"red.sys" & chr(64) & "t-online.de"
Word 2002, Windows 2000
 
M

MrBo

I don't understand this solution.
Are you saying that a column break is represented by Chr(14)?
If so is there any way to get a list of which characters correspond to which
break types? I need to identify the different section breaks as well e.g.
next page section break.
 
H

Helmut Weber

Hi,

the kind of break is defined not only by the character itself
but by e.g. pagesetup. sectionstart in addition:

MsgBox ActiveDocument.Sections(1).PageSetup.SectionStart

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

Greetings from Bavaria, Germany
Helmut Weber, MVP
"red.sys" & chr(64) & "t-online.de"
Word 2002, Windows 2000
 
K

Ken McGowen

Hello,

I have a related question that maybe you can help with.

I am trying to programmatically determine the type of break that is at the
selection point.

What I have tried is :
If asc(Selection.text) = 14 then
'Column break
Else if asc(selection.text) = 12 then
'Section break next page
end if

unfortunatly it doesn't work correctly. Apparently Asc(Selection.text)
returns a 12 on both a page break and a section break next page

So how do I tell the difference?

Thanks
Ken McGowen
(e-mail address removed)
 
K

kkmcg

Lets try this again with an email adress I can check from work

Thanks
Kenneth McGowen
(e-mail address removed)
 
H

Helmut Weber

Hi Ken,

an example for a section break.

The section break character ends a section.
What kind it really is, is defined by the next section.

Like this, which checks what kind of
section.pagesetup.sectionstart applies to the
section, which follows the section break character!

Select case might be more appropriate here
then all the ifs, by the way.

Greetings from Bavaria, Germany
Helmut Weber, MVP
"red.sys" & chr(64) & "t-online.de"
Word 2002, Windows 2000

Sub test012345()
Dim r As Range
Dim c As Long ' counter for sections
Set r = ActiveDocument.Range
r.End = Selection.End
c = r.Sections.Count ' in which section I am
If Asc(Selection.Text) = 12 Then
With ActiveDocument.Sections(c + 1).PageSetup
If .SectionStart = 2 Then
MsgBox "wdSectionNewPage"
End If
If .SectionStart = 0 Then
MsgBox "wdSectionContinuous"
End If
End With
End If

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

End Sub
 
K

kkmcg

Tahnk you very much for your reply and this does answer a related question I
had. However, how does this indicate if it is a sectionbreak or just a page
break since both return an ascii 12?

Thanks,
Ken McGowen
 
H

Helmut Weber

Hi Ken,

I think me need a more professional approach.

Still one more example for chr($12).
If the character after the selected character 12
is still in the same section, then we've found
an ordinary page break.
Otherwise we check pagesetup.sectionstart
as we did already. Don't forget to take care
of resetting search options, as mentioned before.

Sub test012()
Dim rDcm As Range ' the documents range
Set rDcm = ActiveDocument.Range
Selection.StartOf Unit:=wdStory, Extend:=wdMove
With Selection.Find
.Text = Chr(12)
While .Execute
MsgBox KindofBreak12(Selection.Range)
Wend
End With
'wdSectionContinuous ' 0
'wdSectionNewColumn ' 1
'wdSectionNewPage ' 2
'wdSectionEvenPage ' 3
'wdSectionOddPage '4

End Sub

Public Function KindofBreak12(ByVal rTmp As Range) As String
Dim lSct1 As Long ' counter for sections
Dim lSct2 As Long ' counter for sections
Dim lKoSc As Long ' kind of section.pagesetup.sectionstart
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 = "ordinary page break"
Exit Function
End If
' next character is in the next section
lKoSc = ActiveDocument.Sections(lSct2).PageSetup.SectionStart
Select Case lKoSc
Case 0: KindofBreak12 = "wdSectionContinuous"
Case 2: KindofBreak12 = "wdSectionNewPage"
Case 3: KindofBreak12 = "wdSectionEvenPage"
Case 4: KindofBreak12 = "wdSectionOddPage"
End Select

End Function


Greetings from Bavaria, Germany

Helmut Weber, MVP, WordVBA

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

kkmcg

Thank you very much, this is exactly what I needed.

Didn't realize it would be this convoluted.

Thanks again

Ken
 
M

MrBo

Helmut, this code works great except for the following scenario:

I'm trying to detect a wdSectionNewPage. The code you provided does this
great. However when I add an index to the bottom of my document, the call:

lKoSc = objWord.ActiveDocument.Sections(lSct2).PageSetup.SectionStart

returns 9999999 as the sectionstart. The break I'm trying to detect is on a
different page of the doc (pg3) to the index (pg5).

Any idea why?
 
H

Helmut Weber

Hi,

just to let you know that I (and all other MVPs, probably)
are reading almost everything.

Most of the suggestions in this group, I'd say, work in principle.
Very few will work under all conditions.
Elaboration is left to the original poster.

Sorry, I have no idea. No testing material.
In such cases, put your doc on a publicly available server.
Maybe someone will accept the challenge.

I admit, I am no good at indices, fields, references
and long documents at all.

Sorry.

I'd start a new thread.


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