Results 1 to 6 of 6
  1. #1
    5 Star Lounger st3333ve's Avatar
    Join Date
    May 2003
    Location
    Los Angeles, California, USA
    Posts
    705
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Unexpected collapse (Word 2002 SP-2)

    <img src=/S/confused3.gif border=0 alt=confused3 width=45 height=45> OK, here's today's Word mystery. Can someone explain to me why the following line (which simply assigns a value to a Long variable based on 3 existing settings) collapses any existing selection if you're working within a footer?

    lngRightTab = Selection.PageSetup.PageWidth - Selection.PageSetup.LeftMargin - Selection.PageSetup.RightMargin

    I solved the problem by assigning the existing selection to a range prior to the above calculation and then re-selecting the range after the calculation, but figuring out what was causing the problem took q--u--i--t--e...s--o--m--e....t--i--m--e. (And note that the same line doesn't mess with a selection if it's in the MainDocument pane.)

    Probably needless to say, this is in the context of a macro used to reset the "flush right" tab in the footer when you adjust the left and right margins of the corresponding section. If there's a way to have Word do this automatically, or if anyone's got a slick-as-s--t macro for this purpose, please step right up.

  2. #2
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    5,055
    Thanks
    2
    Thanked 417 Times in 346 Posts

    Re: Unexpected collapse (Word 2002 SP-2)

    Hi,

    It might be related to headers and footer dimensions being independent of the underlying section dimensions. This would be especially evident if you were working with a document with sections having different page margins/layouts, but the header of footer was set to 'same as previous'. How then would Word know which page setup metrics to use? The answer would be to not have 'same as previous' set and to query the metrics for the underlying section.

    Cheers
    Cheers,

    Paul Edstein
    [MS MVP - Word]

  3. #3
    Super Moderator
    Join Date
    Jan 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    3,853
    Thanks
    4
    Thanked 259 Times in 239 Posts

    Re: Unexpected collapse (Word 2002 SP-2)

    I've got a macro for this purpose but it probably doesn't meet your stated quality requirements. FWIW here it is. In case you can't work it out, my headers use tables and the footers use tabs to line up items.
    <pre>Public Sub PageSetupFlip()
    'Created by Andrew Lockton, Chrysalis Design Pty Limited
    'Formats current section page setup to landscape and adjusts margins accordingly.
    'Modifies the Headers and Footers to match.
    Dim iSectCount As Integer, iSect As Integer
    Dim lngWidth As Long
    iSectCount = ActiveDocument.Sections.Count
    iSect = Selection.Sections.First.Index
    With ActiveDocument.Range
    If iSect < iSectCount Then 'Stop subsequent linking of headers and footers
    .Sections(iSect + 1).Headers(wdHeaderFooterPrimary).LinkToPrevious = False
    .Sections(iSect + 1).Footers(wdHeaderFooterPrimary).LinkToPrevious = False
    Else
    .Sections(iSect).Headers(wdHeaderFooterPrimary).Li nkToPrevious = False
    .Sections(iSect).Footers(wdHeaderFooterPrimary).Li nkToPrevious = False
    End If
    If iSect > 1 Then
    .Sections(iSect).Headers(wdHeaderFooterPrimary).Li nkToPrevious = False
    .Sections(iSect).Footers(wdHeaderFooterPrimary).Li nkToPrevious = False
    End If
    End With 'ActiveDocument End

    With ActiveDocument.Sections(iSect).Range.PageSetup
    .Gutter = CentimetersToPoints(0)
    .SectionStart = wdSectionNewPage
    .OddAndEvenPagesHeaderFooter = False
    .DifferentFirstPageHeaderFooter = False
    If .PageHeight > .PageWidth Then 'starts as Portrait - make it landscape
    .Orientation = wdOrientLandscape
    .PageHeight = CentimetersToPoints(21)
    .PageWidth = CentimetersToPoints(29.7)
    .TopMargin = CentimetersToPoints(3.75)
    .BottomMargin = CentimetersToPoints(3)
    .LeftMargin = CentimetersToPoints(2)
    .RightMargin = CentimetersToPoints(2)
    .HeaderDistance = CentimetersToPoints(1.75)
    .FooterDistance = CentimetersToPoints(1.2)
    Else 'starts as Landscape - make it portrait
    .Orientation = wdOrientPortrait
    .PageHeight = CentimetersToPoints(29.7)
    .PageWidth = CentimetersToPoints(21)
    .TopMargin = CentimetersToPoints(3.75)
    .BottomMargin = CentimetersToPoints(3)
    .LeftMargin = CentimetersToPoints(3)
    .RightMargin = CentimetersToPoints(2)
    .HeaderDistance = CentimetersToPoints(0.75)
    .FooterDistance = CentimetersToPoints(1.2)
    End If
    lngWidth = .PageWidth - .LeftMargin - .RightMargin 'in points
    HFTabChange lngWidth, iSect 'Change footer tab placements
    HeaderTableChange lngWidth, iSect 'Change table column widths
    End With
    End Sub
    '-----------------------------------------------------------------
    Public Function HeaderTableChange(lngWidth As Long, iSect As Integer)
    'called by PageSetupFlip sub and PageSetupA3 sub
    Dim aCol As Column
    With ActiveDocument.Sections(iSect).Headers(wdHeaderFoo terPrimary).Range.Tables(1)
    .Rows.Alignment = wdAlignRowLeft
    .Rows.SetLeftIndent LeftIndent:=0, RulerStyle:=wdAdjustNone
    For Each aCol In .Columns
    aCol.Width = lngWidth / .Columns.Count
    Next aCol
    End With
    End Function
    '-----------------------------------------------------------------
    Public Function HFTabChange(lngWidth As Long, iSect As Integer)
    'called by PageSetupFlip sub and PageSetupA3 sub
    With ActiveDocument.Sections(iSect).Footers(wdHeaderFoo terPrimary).Range.ParagraphFormat
    .TabStops.ClearAll
    .TabStops.Add lngWidth, wdAlignTabRight, wdTabLeaderSpaces
    End With
    With ActiveDocument.Sections(iSect).Headers(wdHeaderFoo terPrimary).Range.ParagraphFormat
    .TabStops.ClearAll
    .TabStops.Add lngWidth, wdAlignTabRight, wdTabLeaderSpaces
    End With
    End Function</pre>

    Andrew Lockton, Chrysalis Design, Melbourne Australia

  4. #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

    Re: Unexpected collapse (Word 2002 SP-2)

    Well, there is a perverse consistency: when you have text selected in a footer and call up the Page Setup dialog using either the icon on the Header and Footer toolbar or File>Page Setup, the selection returns to position 0 in the footer. Is that an explanation? No, of course, not, but I think once upon a time you couldn't even access this dialog from inside a footer, so maybe this was the best compromise they could build.

    The perverse consistency continues! Here's a method that doesn't use the Selection object directly, and it also changes the Selection:

    ?activedocument.Sections(selection.Information(wdA ctiveEndSectionNumber)).PageSetup.PageWidth

    Okay, I give up.

  5. #5
    3 Star Lounger
    Join Date
    Apr 2004
    Location
    Boston, Massachusetts, USA
    Posts
    389
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Unexpected collapse (Word 2002 SP-2)

    That is bizarre behavior.

    This is a bit of a kludge, but it'll get the selection back to where it was:

    <pre>Dim rngSelection As Range
    Set rngSelection = Selection.Range.Duplicate
    '
    ' do stuff here that moves selection
    '
    rngSelection.Select
    </pre>


  6. #6
    5 Star Lounger st3333ve's Avatar
    Join Date
    May 2003
    Location
    Los Angeles, California, USA
    Posts
    705
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Re: Unexpected collapse (Word 2002 SP-2)

    Thanks for the code sample. I've used Access VBA for a while but I'm new to Word. Referring to the .ParagraphFormat of the Footers(wdHeaderFooterPrimary).Range is simpler than the code I'd devised for the purpose. (I started out selecting the footer and working with the selection, but had moved from there to looping through the paragraphs in the range. I'm still going to loop through the tabstops and only clear the right-align tabs, since I also use center tabs in footers.)

Posting Permissions

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