VBA code for section breaks

Discussion in 'Word VBA' started by Rick Charnes, Jan 25, 2005.

  1. Rick Charnes

    Rick Charnes Guest

    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, Jan 25, 2005
    #1
    1. Advertisements

  2. Rick Charnes

    Dave Lett Guest

    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


    "Rick Charnes" <> wrote in message
    news:...
    > 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, Jan 25, 2005
    #2
    1. Advertisements

  3. Rick Charnes

    Greg Guest

    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, Jan 25, 2005
    #3
  4. Rick Charnes

    Greg Guest

    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
     
    Greg, Jan 25, 2005
    #4
  5. Rick Charnes

    Dave Lett Guest

    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

    "Greg" <> wrote in message
    news:...
    > 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, Jan 25, 2005
    #5
  6. Rick Charnes

    Greg Guest

    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 :)
     
    Greg, Jan 25, 2005
    #6
  7. Rick Charnes

    Rick Charnes Guest

    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.

    In article <>,
    says...
    > 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
    >
    >
    > "Rick Charnes" <> wrote in message
    > news:...
    > > 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, Jan 25, 2005
    #7
  8. Rick Charnes

    Dave Lett Guest

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

    "Rick Charnes" <> wrote in message
    news:...
    > 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.
    >
    > In article <>,
    > says...
    > > 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
    > >
    > >
    > > "Rick Charnes" <> wrote in message
    > > news:...
    > > > 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, Jan 25, 2005
    #8
  9. Rick Charnes

    Rick Charnes Guest

    In article <>,
    says...
    > 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.
     
    Rick Charnes, Jan 25, 2005
    #9
  10. Rick Charnes

    Dave Lett Guest

    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

    "Rick Charnes" <> wrote in message
    news:...
    > In article <>,
    > says...
    > > 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.
     
    Dave Lett, Jan 25, 2005
    #10
  11. Rick Charnes

    jdaw1

    Joined:
    Nov 15, 2016
    Messages:
    1
    Likes Received:
    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.
     
    jdaw1, Nov 15, 2016
    #11
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. constance
    Replies:
    3
    Views:
    163
    Mark Tangard
    Feb 7, 2004
  2. Mark Baird

    You Will Need To Use Section Breaks

    Mark Baird, May 27, 2004, in forum: Word VBA
    Replies:
    1
    Views:
    110
    Charles Kenyon
    May 27, 2004
  3. raj

    index field and section breaks

    raj, May 31, 2004, in forum: Word VBA
    Replies:
    1
    Views:
    129
    Cindy M -WordMVP-
    May 31, 2004
  4. Steve
    Replies:
    3
    Views:
    131
    Cindy M -WordMVP-
    Jun 5, 2004
  5. roseg
    Replies:
    2
    Views:
    1,069
    Helmut Weber
    Nov 7, 2006
  6. andreas
    Replies:
    5
    Views:
    427
    andreas
    Jan 16, 2007
  7. Replies:
    3
    Views:
    476
    Jean-Guy Marcil
    Sep 10, 2008
  8. andreas
    Replies:
    4
    Views:
    235
    andreas
    Jul 5, 2010
Loading...