Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / January 2005

Tip: Looking for answers? Try searching our database.

VBA code for section breaks

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Rick Charnes - 25 Jan 2005 16:01 GMT
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.
Dave Lett - 25 Jan 2005 16:38 GMT
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

> 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.
Greg - 25 Jan 2005 17:27 GMT
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
Greg - 25 Jan 2005 17:28 GMT
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
Dave Lett - 25 Jan 2005 17:32 GMT
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

> Dave,
>
[quoted text clipped - 7 lines]
>
> Thanks
Greg - 25 Jan 2005 18:50 GMT
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 :-)
Rick Charnes - 25 Jan 2005 19:34 GMT
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.

> Hi Rick,
>
[quoted text clipped - 14 lines]
> > 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.
Dave Lett - 25 Jan 2005 19:41 GMT
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.)

> 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
[quoted text clipped - 31 lines]
> > > 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.
Rick Charnes - 25 Jan 2005 19:56 GMT
> Hi Rick,
>
[quoted text clipped - 4 lines]
> 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.
Dave Lett - 25 Jan 2005 20:18 GMT
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

> > Hi Rick,
> >
[quoted text clipped - 16 lines]
> and the Section Breaks are miraculously changed, the headers that I need
> (First Page and Primary from pp. 1 and 2) are perfect.

Rate this thread:






 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.