Results 1 to 7 of 7
  1. #1
    3 Star Lounger
    Join Date
    Jan 2001
    Location
    Wellington, Wellington, New Zealand
    Posts
    378
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Determining section number (Office 2000)

    Hi

    I'm sure it's simple, and I'm sure it's in WOPR somewhere, but I can't find it.

  2. #2
    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: Determining section number (Office 2000)

    Both the Selection and Range objects have an .Information collection. Somewhere in there is a way to get at the section number. (I've shut down and am out of here. If you don't find it, let us know.)

  3. #3
    Platinum Lounger
    Join Date
    Nov 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    5,016
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Determining section number (Office 2000)

    You could probably adapt the following:
    <pre>MsgBox "The selection is in Section " & _
    Selection.Information(wdActiveEndSectionNumber)</pre>


    Alan

  4. #4
    Bronze Lounger
    Join Date
    Nov 2001
    Location
    Arlington, Virginia, USA
    Posts
    1,394
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Re: Determining section number (Office 2000)

    Each Section object in a Word Document object has a Headers or Footers property, which returns a HeadersFooters Collection of HeaderFooter objects that may or may not exist in that section. The Parent property of a HeaderFooter object returns a Section object. To determine section number, get the Section object's Index property. Sample VBA code that loops thru each Section in active doc & prints out the primary header/footer text (may be blank) & which section it belongs to:

    Public Sub GetHeaderAndFooterSectionIndex()

    Dim obj As Object
    Dim hdr As HeaderFooter
    Dim ftr As HeaderFooter
    Dim intSections As Integer
    Dim n As Integer

    intSections = ActiveDocument.Sections.Count

    For n = 1 To intSections
    Set hdr = ActiveDocument.Sections(n).Headers(wdHeaderFooterP rimary)
    Debug.Print "Section " & n & " Primary Header text: " & hdr.Range.Text
    Set obj = hdr.Parent
    Debug.Print "Parent object type: " & TypeName(obj) & "; Index: " & hdr.Parent.Index

    Debug.Print 'blank line
    Set ftr = ActiveDocument.Sections(n).Footers(wdHeaderFooterP rimary)
    Debug.Print "Section " & n & " Primary Footer text: " & ftr.Range.Text
    Set obj = ftr.Parent
    Debug.Print "Parent object type: " & TypeName(obj) & "; Index: " & ftr.Parent.Index
    Debug.Print 'blank line
    Next n

    Set obj = Nothing
    Set hdr = Nothing
    Set ftr = Nothing

    End Sub

    When I ran sub with dummy doc with 3 sections the Index returned for Header/Footer Parent object corresponded to value of "n" variable (ie, Section index). Note that there are 3 types of HeaderFooter objects that may exist, there's always a Primary Header/Footer (denoted by wdHeaderFooterPrimary constant) even if blank; if using a first-page only (wdHeaderFooterFirstPage) or odd/even header/footer (wdHeaderFooterEvenPages), you'd have to test its Exists property first or error will occur when trying to reference Parent property if does not exist. What I'm not sure about, how do you get a reference to a HeaderFooter object without first knowing what section it belongs to?

    HTH

  5. #5
    Bronze Lounger
    Join Date
    Nov 2001
    Location
    Arlington, Virginia, USA
    Posts
    1,394
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Re: Determining section number (Office 2000)

    To answer my own question, I realized that a Selection object has a HeaderFooter property that returns a HeaderFooter object. Otherwise do not know how to get a HeaderFooter object w/o first knowing the Section number it belongs to...

  6. #6
    3 Star Lounger
    Join Date
    Jan 2001
    Location
    Wellington, Wellington, New Zealand
    Posts
    378
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Re: Determining section number (Office 2000)

    Thank you, all.

  7. #7
    Bronze Lounger
    Join Date
    Nov 2001
    Location
    Arlington, Virginia, USA
    Posts
    1,394
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Re: Determining section number (Office 2000)

    The Parent property, which returns the parent object of a specified object, can be useful in determining this type of information in many cases. Note the use of VBA TypeName function in sample code, which returns a String that provides information about the variable specified in the varname argument. For an object variable, the string returned indicates the object type. This is useful in Word & other VBA apps where it is not always obvious just what type of object represents the "parent" of another object. For example, it may not be obvious if a given object "belongs" to a Section, Document, or other object. I used TypeName in this example to "confirm" that the HeaderFooter's "Parent" was in fact a Section object.

    HTH

Posting Permissions

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