PDA

View Full Version : Solved: Remove last section w/o changing hdrs/ftrs?



clhare
08-19-2005, 07:20 PM
I have a Word document that has three sections in it.

The Document/Sections
Section 1: Headers/footers set with "Different odd/even" selected.

Section 2: Has different margins from section 1, and has headers/footers set with "different odd/even" and "different first page" selected. (NOTE: Only this section needs a different first page footer.)

Section 3: Has different margins than sections 1 or 2, and has headeres/footers set with "different odd/even" selected.

The Problem
In some circumstances, section 3 is not needed and has to be removed. I tried deleting section 3 by removing the section break at the end of section 2 and then from there to the end of the document. I'm finding that when I remove that last section, the margins/headers/footers on the pages before it change to the margins/headers/footers of the pages I just removed (section 2 now has section 3's headers/footers), but I need them to stay the way there were.

HELP!
Is there a way to prevent the margins/headers/footers from changing when the last section is removed? I am being pushed very hard to fix this and I am stumped!

Any help is greatly appreciated!

http://www.tipmaster.com/images/hairpull2.gif

mdmackillop
08-20-2005, 01:04 AM
Hi Cheryl,
Change the section 3 header and footer to Same as Previous before deleting the section break.

Try the following, 'cos its a pain in the butt doing it manually all the time!

Sub DelSection3()
Selection.GoTo What:=wdGoToSection, Which:=wdGoToFirst, Count:=3, Name:=""
ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
Selection.HeaderFooter.LinkToPrevious = True
ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageFooter
Selection.HeaderFooter.LinkToPrevious = True
ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
'Remove section break
With Selection.Find
.Text = "^b"
.Replacement.Text = "OldSection3"
.Forward = False
End With
Selection.Find.Execute Replace:=wdReplaceOne
'Remove section 3 text
With Selection.Find
.Text = "OldSection3"
.Forward = True
.Wrap = wdFindContinue
End With
Selection.Find.Execute
Selection.EndKey Unit:=wdStory, Extend:=wdExtend
Selection.Delete Unit:=wdCharacter, Count:=1
'Return to start
Selection.HomeKey Unit:=wdStory
End Sub

fumei
08-20-2005, 02:13 AM
What if the headerfooters in Sec 3 are different?

mdmackillop
08-20-2005, 02:18 AM
Hi Gerry,
I'm assuming they are different.
section 2 now has section 3's headers/footers

Run the macro DelSection3 in the attached sample.

clhare
08-20-2005, 05:44 AM
I ran the macro and found that the margins, headers, and footers still get messed up. I created a new test file and included a sample of what the settings should be, then ran the macro (which I put in the test file). I've also included a file which contains the results after the macro is run.

Any help is greatly appreciated!

http://www.tipmaster.com/images/hairpull2.gif

clhare
08-20-2005, 06:43 AM
I'm still playing with this and just got one step closer before I ran out of ideas.

I modified my test file, changing section 3 to also have "different first page". Then I added a bookmark for section 3, starting with the page break just before it. I changed the macro to delete bookmark "section3" and the hard return after it (the very end of the document).

The result is a little better...I now get the correct margins in section 2 after section 3 is removed and the first page header and first page footer in section 2 are still correct. But, I still have an incorrect header and footer on pages 2 and 3 of section 2.

Any help is greatly appreciated!

http://www.tipmaster.com/images/hairpull2.gif

mdmackillop
08-21-2005, 01:02 PM
Sorry Clare,
I don't have much time just now. I think though you'll need to reset the odd/even settings as well. Something like
With ActiveDocument.Range(Start:=Selection.Start, End:=ActiveDocument. _
Content.End).PageSetup
.OddAndEvenPagesHeaderFooter = False
.DifferentFirstPageHeaderFooter = False
End With

fumei
08-22-2005, 08:55 AM
Been looking this. T'is rather strange behaviour in Word.

1. Regardless of the number of sections, the removal of a section MOVES the later headers froward. If you have Sec 1, Sec 2, Sec 3, Sec 4, Sec 5. If you remove Section 3, then the third Section (as the previous Section is gone, Section 4 BECOMES Sec 3...so the text will be the previously existing texct for Section 4. This is anh issue of the text in the headers for Section 4, mention Section 4.

My test documents DID, and the headers definitley move forward.

2. Removal of the last section does this as well. It moves its text forward (or backward if you prefer). I find this strange. Five Sections, remove Section 5 - this similar to Cheryl's problem.

Sec 1 - no change
Sec 2 - no change
Sec 3 - no change
Sec 4 - becomes Section 5 headers
Sec 5 - no longer exists.

Therefore,k if you know the last section is going to be removed, BEFORE you do, make (as was suggested) the last section Same as previous. That way, when they are moved back (or forward if you like), they will be correct.

3. This works manually, HOWEVER,


Dim oSection As Word.Section
Set oSection = ActiveDocument.Sections(2)
oSection.Range.Delete
Set oRange = Nothing

Again, with 5 sections in the document......
OK, run this as Sections(5)
There are NO errors, but section 5 does NOT delete. You cna delete it manually, byu going and delting the Section break.

However, run this with Sections(2). Section 2 DOES delete, and the headers for 3, 4, 5 are moved back. Section 2 heards now show as Section text, etc etc.

Hmmmmmm.

clhare
08-22-2005, 09:26 AM
I figured it out--thanks to all your ideas... Here's what I had to do:

Bookmark everything in section 3 that I want to delete, include the page break the precedes it
Go to section 3 and reset margins, and header/footer options to the exact same options as section 2
Go to section 3 headers and make them all "same as previous" so they are the same as section 2
Go to section 3 footersand make them all "same as previous" so they are the same as section 2
Delete the bookmarked text
Go back into section 2's header and reset the page numbering (I tried to reset while I was in the header doing the "same as previous", but it seemed to work better if I did this step separately
PHEW!!

fumei
08-22-2005, 09:59 AM
That would work, but it DOES seem to bit strange to be forced to do it.l

clhare
08-22-2005, 10:48 AM
I know. I tried every way I could think of including the suggestions here, but this was the only way that worked.

fumei
08-22-2005, 11:00 AM
I going to still work on this. I can SORT OF see why header contents would move backward into the space previously occupied by a deleted section. That kinda makes sense.

But it does NOT make sense that a deleted LAST section should have its contents move backward as well.

I mean, if you have:

Sec 1
Sec 2
Sec 3
Sec 4

and you delete Sec 2, then practically speaking Sec 3 IS now Sec 2, and Sec 4 IS Sec 3. It is not like you can have, or maintain, a hidden Sec 2. It not longer is there. Sec 3 IS sec 2.

That seems OK. But if sec 4 is deleted, why should its DELETED values been forced back onto the previous section?

Seems odd to me.

mdmackillop
08-22-2005, 11:19 AM
Hi Gerry,
Happy to assist with testing of any solutions you come up with.
Regards
Malcolm

fumei
08-23-2005, 01:40 PM
Will do. I am presently writing a detailed FAQ on header footers, and I thought I knew quite a bit about them. However, I am humbled again. It seems to happen frequently with Word. This one has got me actually rather puzzled.

I found myself lying in bed sketching parent/child object relationship in my head. It may, in fact, be one of those bizarre things in the object model that do not totally make sense. I suspect this is the case.

Note that this backward movement of values works even in this case:

1. make a new document
2. make an immediate Section break Next Page
3. in that section (#2) put "Section 2" in the header.

Summary: two pages, the second page in a new Section and with text in the header. NO different first page, or odd/even.

4. make Same as previous FALSE
5. delete the section break - remember there is NO text at all in the document, no othe rpage breaks etc etc.

The header for Section 1 - previous blank, is now "Section 2".

Word moves values of headerfooters backwards. There is something here that is itching me to death. I know for a fact that Word propogates the values of the headerfooter COLLECTION within a Section forwards. In other words, Primary - unless DifferentFirstPage and/or Odd/Even are actually set to TRUE - propogates its value to the others. If the other are ever set to True, then even if Primary is changed, it no longer propogates. There is a propgation order though.

Primary is Index 1
DifferentFirstPage = 2
DifferentOdd/Even = 3

and they process in that order.

WHY though if the situation is:
Sec 1
Sec 2
Sec 3
Sec 4
would the removal of Sec 4 CHANGE the values of Sec 3...when Sec 3 is still Sec 3, and has not had anything done to it.

Except.....it is now the LAST section. Hmmmmmmmmmmm.

TonyJollans
08-23-2005, 02:54 PM
Hi Gerry,

This one is a real PITA. I have played around with it a bit in the past but never sat down and worked out exactly what's going on or how to 'correctly' delete a Section so I look forward to whatever you come up with.

I would just point out that it isn't really a Header/Footer issue per se. It is a Section issue and it affects the other child objects of the Section (PageSetup, Borders) as well.

fumei
08-23-2005, 11:17 PM
Hi Tony. I have done a great deal of looking into this, and here is my from on high announcement....

There is nothing to be done with it.

You are correct - this is NOT a header/footer issue. It is a Section issue. Not only that, but I went back into various old versions of Word. This goes back to Word for DOS, yes, before Windows.

The last Section is like the last paragraph mark. It cannot be deleted. Further, its values are propagated backwards. Always have. There is a Section mark in every document, as the properties of a Section are defined the Section mark. Section 1 always exists and has the properties of the first Section mark (not visible). If you create a new Section (#2) and put text into the header and footer, and then delete the Section mark, the values are propagated backwards. There is nothing you can do about it.

There are some other odd things. Say you have a document with 5 Sections, the last section having three pages utilizing DifferentFirstPage and DifferentOdd/Even.

Sub DeleteSection()
Dim oSec As Section
Set oSec = ActiveDocument.Sections(5)
oSec.Range.Delete
Set oSec = Nothing
End Sub

does NOT delete the Section. It deletes everything (in the entire range), but NOT the Section mark. Therefore there is still a section 5, and the header will be whatever is the header for DifferentFirstPage (previously set).

If you run the same code but make the Section #2 (or #3, or #4...or #1 for that matter), the entire Section (range) will be deleted, AS WILL the Section mark that defines the declared object. All other hearer/footers will propagate backwards.

PITA? Undoubtedly. It is, perhaps unfortunately, built right into the document model. It is the way Microsoft perceives what a document is.

There is nothing we can do about it. Section values propagate backwards.

In another thread I mentioned that I found the Word object model sometimes bizarre, sometime beautiful, and sometimes silly and stupid. I think this ones belongs in category 1.

As Tony points out, as Section properties hold margins, borders etc etc., these too are propagated backwards. There is NO way to change this. This is not exposed by the object model.

The only way is to pick up all the values of the preceding Section, store them, make the Section deletion, and then restore the values back into the section that was changed.

PITA