VBA code for section breaks

R

Rick Charnes

Is there VBA code to search for a section break, then convert it from a
New Page Section Break to a Continuous Section Break? Thanks.
 
D

Dave Lett

Hi Rick,

I'm using Word 2003 and the following routine works well in my environment:

Dim iSec As Integer
For iSec = ActiveDocument.Sections.Count To 2 Step -1
With ActiveDocument.Sections(iSec).Range.PageSetup
If .SectionStart = wdSectionNewPage Then
.SectionStart = wdSectionContinuous
End If
End With
Next iSec

HTH,
Dave
 
G

Greg

Rick

Maybe something like:

Sub ScratchMacro()
Dim i As Integer
For i = 2 To ActiveDocument.Sections.Count
If ActiveDocument.Sections(i).PageSetup.SectionStart = 2 Then
ActiveDocument.Sections(i).PageSetup.SectionStart = 0
End If
Next
End Sub
 
G

Greg

Dave,

I just saw you post pop up. I only did a simple test with the method
that I proposed. Would you mind explaining the advantage of:

ActiveDocument.Sections.Count To 2 Step -1
over:

2 to ActiveDocument.Sections.Count

Thanks
 
D

Dave Lett

Hi Greg,

I'm not sure that there's an advantage in this situation. However, I prefer
to cycle through collections this way so that I'm certain that I get all the
items in the collection. For example, if you were working with the
Hyperlinks collection and deleted one of them, then you'd never get to the
last hyperlink by going "forward" (i.e., 1 through x). Instead, you could
only be sure of accessing each item in the collection by going "in reverse"
(i.e., x through 1). I chose this process out of an abundance of caution or
habit, take your pick. :)

Dave
 
G

Greg

Dave,

I see your point. Yesterday while leaving wads of hair around my
workstation, I was trying to figure out a way to remove all items from
a listbox. Taking baby steps and ignorant of a better way first I
tried:

For i = 0 To txtDateFormat.ListCount
txtDateFormat.RemoveItem (i)
Next i

Which generated an error when (i) = 7. (there where 14 items in the
list)

Next I tried:

For i = txtDateFormat.ListCount - 1 To 0 Step -1
txtDateFormat.RemoveItem (i)
Next i

Which worked.

Thinking that there most be a better way especially if there where
hundreds of items in the list, I stumbed upon.
txtDateFormat.Clear

Go figure :)
 
R

Rick Charnes

Both this code and Greg's work perfectly in Word 2000; THANK YOU VERY
MUCH. My ultimate need: change all Section Breaks to Continuous, AND
then delete all text between current cursor position and the end of
document. In other words, delete all text between what *had been* Next
Page Section Breaks, starting from my current cursor position to end of
document. But I need to keep the actual Section Breaks so the headers
remain untouched.

I envision this latter task as:

FOR 1 until eof
delete text from here to next section break
step over section break
NEXT


Any assistance greatly appreciated.
 
D

Dave Lett

Hi Rick,

If you select the first Next Page break, change it to a Continuous page
break, and then delete all the "text between what *had been* Next
Page Section Breaks, starting from my current cursor position to end of
document", then you will not have any other Next Page breaks. Did you mean,
as you show in your pseudo-code to delete from the beginning of the selected
section to the end of the selected section?

"So the headers remain untouched". Well, by changing the section breaks, you
risk losing that. That is, when you change a Next Page break to a Continuous
page break, then (at a minimum) the first heading will not be until the
first page of the new section. (I hope this makes sense.)
 
R

Rick Charnes

Hi Rick,

If you select the first Next Page break, change it to a Continuous page
break, and then delete all the "text between what *had been* Next
Page Section Breaks, starting from my current cursor position to end of
document", then you will not have any other Next Page breaks. Did you mean,
as you show in your pseudo-code to delete from the beginning of the selected
section to the end of the selected section?

Yeah, loop through all sections and delete text therein. How would I
code that?
"So the headers remain untouched". Well, by changing the section breaks, you
risk losing that. That is, when you change a Next Page break to a Continuous
page break, then (at a minimum) the first heading will not be until the
first page of the new section. (I hope this makes sense.)

Right. I was worried about that happening, but after I run your code
and the Section Breaks are miraculously changed, the headers that I need
(First Page and Primary from pp. 1 and 2) are perfect.
 
D

Dave Lett

Hi Rick,

I think the following does the trick:

Dim iSec As Integer
Dim oRng As Range
For iSec = ActiveDocument.Sections.Count To 2 Step -1
With ActiveDocument.Sections(iSec).Range.PageSetup
If .SectionStart = wdSectionNewPage Then
.SectionStart = wdSectionContinuous
Set oRng = ActiveDocument.Sections(iSec).Range
oRng.MoveEnd Unit:=wdCharacter, Count:=-1
oRng.delete
End If
End With
Next iSec

HTH,
Dave
 
Joined
Nov 15, 2016
Messages
1
Reaction score
0
Mac Word 2010 has lots of bugs with sections. Deleting one affects the previous; manually changing to continuous does nothing. Really Microsoft: we are locked in; but if you want users to welcome being locked to Microsoft, it needs to be better. Employ several people to read forums and identify bugs. Fix the bugs that week.

However, Dave Lett’s code in the second post rescued me. Thank you.
 
Joined
Sep 13, 2017
Messages
1
Reaction score
0
'Use this code below:
'(It will Delete the Section brakes from entire document)
'------------------------------------
Private Sub Delete_SectionBrakes()
re:
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = Chr(12)
.Forward = True
.Wrap = wdFindContinue
.Execute
If .Found = True Then
Selection.Delete
GoTo re:
End If
End With
End Sub
 

macropod

Microsoft MVP
Joined
Mar 2, 2012
Messages
578
Reaction score
50
I seriously doubt any of the original contributors are following this thread some 12 years later...

More importantly, as the note at the top of this page says:
This forum sub-section is an archive which contains old newsgroup posts. If you wish to post a query, please do so in one of our main forum sections
 

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