Results 1 to 5 of 5

Thread: Delete Section

  1. #1
    5 Star Lounger
    Join Date
    Mar 2001
    Location
    New York, NY
    Posts
    853
    Thanks
    0
    Thanked 9 Times in 8 Posts
    Using VBA, I want to delete the final section of my document. I know the section number, based on other criteria. Say the section count is 7:

    ActiveDocument.Sections(lSectionCount).Range.Delet e

    deletes section 7 as expected, but the document still has 7 sections because the section break after section 6 remains. How can I delete that final section break?

    I've set section 6 as a range, moved the end by -1 character, set another range from that point to the end of the document, selected this range, and deleted the selection. That seems like an awkward way to accomplish this.

  2. Subscribe to our Windows Secrets Newsletter - It's Free!

    Get our unique weekly Newsletter with tips and techniques, how to's and critical updates on Windows 7, Windows 8, Windows XP, Firefox, Internet Explorer, Google, etc. Join our 480,000 subscribers!

    Excel 2013: The Missing Manual

    + Get this BONUS — free!

    Get the most of Excel! Learn about new features, basics of creating a new spreadsheet and using the infamous Ribbon in the first chapter of Excel 2013: The Missing Manual - Subscribe and download Chapter 1 for free!

  3. #2
    Plutonium Lounger
    Join Date
    Nov 2001
    Posts
    10,550
    Thanks
    0
    Thanked 7 Times in 7 Posts
    Quote Originally Posted by richardbarrett View Post
    ...
    I've set section 6 as a range, moved the end by -1 character, set another range from that point to the end of the document, selected this range, and deleted the selection. That seems like an awkward way to accomplish this.
    The problem is not that this is "a bit awkward" but that it may not be doing exactly what you want.

    The header, footer, page setup and similar attributes for the last section are stored in the final paragraph mark for the document. If you delete the final section AND the section break just before this section then the section that now becomes the last section will take on these attributes from the deleted section.

    I am not sure of the best way to do this, but you may need quite complex code to make sure that you capture all the section specific information and preserve it.

  4. #3
    5 Star Lounger
    Join Date
    Mar 2001
    Location
    New York, NY
    Posts
    853
    Thanks
    0
    Thanked 9 Times in 8 Posts
    I understand the issue regarding the header, footer and other information that's stored in the section break. At the end of my code, I clear all headers and footers and add what I need for the document: Section 1 has page numbering in the primary footer (but not the first page footer), and all other sections have nothing in the footer. My code actually begins with a "container" document and then inserts several other documents, some of which might have section breaks. By the time this document is assembled, the contents of the headers and footers are an unknown... which is why I clear everything and then add what I need.

  5. #4
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts
    Quote Originally Posted by richardbarrett View Post
    How can I delete that final section break?
    This might be slightly less awkward. It's a lot of code, but easy to call:

    Code:
    Function DeleteSection(doc As Word.Document, indx As Integer) As Boolean
    With doc.Sections
        If indx < 1 Or indx > .Count Then
            MsgBox "Incorrect section index!"
            DeleteSection = False
            Exit Function
        End If
        Dim rngTemp As Word.Range
        Set rngTemp = .Item(indx).Range
        If indx < .Count Then   'Delete!
            rngTemp.Delete
        Else                    'Adjust range to clear prior section break
            If rngTemp.Start > 0 Then
                rngTemp.MoveStart Unit:=wdCharacter, Count:=-1
            End If
            rngTemp.Delete
        End If
        Set rngTemp = Nothing
        DeleteSection = True
    End With
    End Function

  6. #5
    5 Star Lounger
    Join Date
    Mar 2001
    Location
    New York, NY
    Posts
    853
    Thanks
    0
    Thanked 9 Times in 8 Posts
    Thanks. That's just what I need.

    Richard

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •