none
way of deleting a section or a chapter in word report RRS feed

  • Question

  • Hi,

    I was wondering if I can get some help with word 2010. I was trying to find a way to delete an entire of selected section or chapter in a report document in word 2010.

    I have a report consisting of Sections (or Chapters if you like) Headings.

    Because I'd like to be able to re-use the report, I need to be able to remove some of irrelevant sections according to the jobs I work on. Perhaps there are times when I need to insert new sections into the report.

    I'd like to write a macro that can prompt me for which sections number to delete. I am not sure if this is possible in VBA word.

    So here is what it looks like inside the report in respect to sections

    Section 1 Hello World

    ...

    -------------section break (next page)--------------

    Section 2 blah blah

    ...

    -------------section break (next page)--------------

    and so on...

    Any help would be greatly appreciated.

    Thank you

     

    • Moved by Max Meng Friday, January 6, 2012 5:36 AM Moving to a more appropriate forum (From:Word IT Pro Discussions)
    Thursday, January 5, 2012 5:22 AM

Answers

  • Below is a crude approximation of what you might be looking for.  You can use it as a starting point.

    Sub DeleteASection()
        Dim sec As Integer
        Dim rng As Word.Range
        On Error Resume Next
        
        Word.Selection.HomeKey unit:=Word.WdUnits.wdStory
        sec = InputBox("Enter the Section Number", "Delete a Section", 0)
        If sec = 0 Then Exit Sub
        
        Word.Selection.GoTo What:=wdGoToSection, Which:=wdGoToFirst, count:=sec, Name:=""
        Set rng = Word.Selection.Range
        rng.MoveEnd unit:=Word.WdUnits.wdSection, count:=1
        rng.Delete
    End Sub
    
    

     


    Kind Regards, Rich ... http://greatcirclelearning.com
    Thursday, January 5, 2012 11:47 AM
  • Since each section has two sections all you should have to do is change the count parameter on the rng.MoveEnd command to 2.  Thus it would read:

     rng.MoveEnd unit:=Word.WdUnits.wdSection, Count:=2

    You don't need the second goto statement. In fact that second goto statement just goofs up the starting point of the range all together.


    Kind Regards, Rich ... http://greatcirclelearning.com
    Friday, January 6, 2012 3:04 AM

All replies

  • Below is a crude approximation of what you might be looking for.  You can use it as a starting point.

    Sub DeleteASection()
        Dim sec As Integer
        Dim rng As Word.Range
        On Error Resume Next
        
        Word.Selection.HomeKey unit:=Word.WdUnits.wdStory
        sec = InputBox("Enter the Section Number", "Delete a Section", 0)
        If sec = 0 Then Exit Sub
        
        Word.Selection.GoTo What:=wdGoToSection, Which:=wdGoToFirst, count:=sec, Name:=""
        Set rng = Word.Selection.Range
        rng.MoveEnd unit:=Word.WdUnits.wdSection, count:=1
        rng.Delete
    End Sub
    
    

     


    Kind Regards, Rich ... http://greatcirclelearning.com
    Thursday, January 5, 2012 11:47 AM
  • Thank you Rich for your response.

    I tried and modified just a little of your code so that it suits my need. And it kind of works which is a good thing. It's getting closer.

    So what worked was it deleted the entire section. And what did not work was there were two section breaks at the end of section it missed out.

    At the end of each sections, there is a section break (continuous), followed by some field codes and then finished with section break (next page).

    I was trying to get rid of these sections breaks but to no avail. I was hoping if you could help me out.

    I tried to add the following code as follows

        Selection.GoTo What:=wdGoToSection, Which:=wdGoToFirst, Count:=sec, Name:=""
        Set rng = Selection.Range
    
        rng.MoveEnd unit:=Word.WdUnits.wdSection, Count:=1
       
        ' go to the position where it is at the end of section break
        With rng
            .GoTo What:=wdGoToBookmark, Name:="\Section"
            .Collapse Direction:=wdCollapseEnd
        End With
       
        rng.Delete

    Thank you very much

     

     

     

    Friday, January 6, 2012 1:45 AM
  • Since each section has two sections all you should have to do is change the count parameter on the rng.MoveEnd command to 2.  Thus it would read:

     rng.MoveEnd unit:=Word.WdUnits.wdSection, Count:=2

    You don't need the second goto statement. In fact that second goto statement just goofs up the starting point of the range all together.


    Kind Regards, Rich ... http://greatcirclelearning.com
    Friday, January 6, 2012 3:04 AM
  • It works great. I did not realize it was to do with the count. Thank you very much for your assistance, Rich. It took me a long time to try but none actually worked. You've saved me.

    So I try now to understand this. I think the number of count is based on the number of sections. correct me if I am wrong.

    for example:

    beginning of section

    ...

    end of section

    This is equivalent to count no. 1

    so in my case, because i have as follows

    beginning of section

    ...

    section break(continuous)

    section break(next page)

    the count number: 2 because it involves two units of sections.

    Thank you once again

     

     

     

    Friday, January 6, 2012 3:25 AM
  • Yes the count refers, in this case, to sections.

    If the start of my range was at the first character position of Section 1 and I moved the end of my range for a count of 2 sections, after deleting the content of my range, the first character position of the document would now be at the first character of what once was Section 3.  Of course now it would be called Section 1 but let's not get too hung up on the particulars. :-)

    That is something though to keep in mind with this process ... deleting any section renumbers the remaining sections.

    One other thing about the count parameter. Positive numbers move forward in the document and negative numbers move backwards. So for example I'm setting my range based on characters and I want to move the starting position of my range back three characters the command would look like this:

    rng.MoveStart unit:=Word.wdUnits.wdCharacter, count:=-3

    I'm glad everything worked out ... take care.

     


    Kind Regards, Rich ... http://greatcirclelearning.com
    Friday, January 6, 2012 3:55 AM