Index Property

Microsoft Word Visual Basic

Show All

Index Property

       

Index property as it applies to the HeaderFooter object.

Returns a WdHeaderFooterIndex that represents the specified header or footer in a document or section. Read-only.

WdHeaderFooterIndex can be one of these WdHeaderFooterIndex constants.
wdHeaderFooterEvenPages Returns all headers or footers on even-numbered pages.
wdHeaderFooterFirstPage Returns the first header or footer in a document or section.
wdHeaderFooterPrimary Returns the header or footer on all pages other than the first page of a document or section.

expression.Index

expression   Required. An expression that returns a HeaderFooter object.

Index property as it applies to all other objects in the Applies To list.

Returns a Long that represents the position of an item in a collection. Read-only.

expression.Index

expression   Required. An expression that returns one of the objects in the Applies To list as mentioned above.

Example

As it applies to the Field object.

This example returns the position of the selected field in the Fields collection.

num = Selection.Fields(1).Index

As it applies to the HeaderFooter object.

This example adds a shape to the first page header in the active document if the specified variable references the first page header.

Sub ChangeFirstPageFooter()
    Dim hdrFirstPage As HeaderFooter

    Set hdrFirstPage = ActiveDocument.Sections(1).Headers(wdHeaderFooterFirstPage)

    If hdrFirstPage.Index = wdHeaderFooterFirstPage Then
        With hdrFirstPage.Shapes.AddShape(Type:=msoShapeHeart, _
                Left:=36, Top:=36, Width:=36, Height:=36)
            .Fill.ForeColor.RGB = RGB(Red:=255, Green:=0, Blue:=0)
        End With
    End If

End Sub

As it applies to the Variable object.

This example adds a document variable to the active document and then returns the position of the specified variable in the Variables collection.

Set myVar = ActiveDocument.Variables.Add(Name:="Name", _
    Value:="Joe")
num = myVar.Index

As it applies to the Window object.

This example returns the number of the first window in the Windows collection. If there are at least two windows in the Windows collection, the macro activates the next window, copies the first word, switches back to the original window, and inserts the Clipboard contents there.

Set myWindow = Windows(1)
winNum = myWindow.Index
If Windows.Count >= 2 Then
    myWindow.Next.Activate
    ActiveDocument.Words(1).Copy
    Windows(winNum).Activate
    Selection.Range.Paste
End If