How to move into other sections exception first section?

A

avkokin

Hello. There is one document that include some sections. I need to
move into the start of every sections (except the first section) and
insert some field thereto. I need to make the round all existing
sections except first section. How I can do it?
There is my example of code but it is wrong and the Word close with
error:
Sub insField()
Dim sec As Section
If Not ActiveDocument.Sections(1) Then
For Each sec In ActiveDocument.Sections
Selection.MoveStart unit:=wdSection
Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty,
preserveformatting:=False
Selection.TypeText Text:="page"
Next sec
End If
End Sub

Thank you.
 
J

Jay Freedman

Hello. There is one document that include some sections. I need to
move into the start of every sections (except the first section) and
insert some field thereto. I need to make the round all existing
sections except first section. How I can do it?
There is my example of code but it is wrong and the Word close with
error:
Sub insField()
Dim sec As Section
If Not ActiveDocument.Sections(1) Then
For Each sec In ActiveDocument.Sections
Selection.MoveStart unit:=wdSection
Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty,
preserveformatting:=False
Selection.TypeText Text:="page"
Next sec
End If
End Sub

Thank you.

This macro has more wrong than just the error message you saw, but I'll get to
that.

The cause of the error is that the expression ActiveDocument.Sections(1) returns
the section itself; it doesn't return a "true/false" value that could be acted
upon by the Not operator. The way you can say to VBA "if this is not section 1
of the document" is to place a statement _inside_ the For/Next loop to test the
loop variable 'sec':

If sec.Index > 1 Then
' do the rest of the work
End If

Once you get that corrected, you'll find that the macro no longer causes an
error, but it still doesn't work the way you want. The problem is that, except
for controlling the number of times the loop executes, you haven't used the loop
variable 'sec' for anything. The Selection.MoveStart statement will move the
cursor from wherever it happens to be (which you haven't tested in the macro) to
the following section (if there is one). In fact, the macro need not -- and
should not -- refer to the Selection at all.

Instead, declare a Range object, and use that in the loop to get access to each
section. Collapse the range to its start (which will be the start of the section
for that execution of the loop) and insert the field at the range. One more
refinement is to insert the type of field you want and avoid the TypeText
method.

Sub insField()
Dim sec As Section
Dim myRange As Range
For Each sec In ActiveDocument.Sections
If sec.Index > 1 Then
Set myRange = sec.Range
myRange.Collapse Direction:=wdCollapseStart
ActiveDocument.Fields.Add Range:=myRange, _
Type:=wdFieldPage, PreserveFormatting:=False
End If
Next sec
End Sub
 
S

StevenM

To: Avkokin,

Sub InsField()
Dim nSections As Long
Dim nIndex As Long

nSections = ActiveDocument.Sections.Count
If nSections > 1 Then
For nIndex = 2 To nSections
ActiveDocument.Sections(nIndex).Range.Select
Selection.Collapse wdCollapseStart
Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldPage,
PreserveFormatting:=False
Next nIndex
End If
End Sub

Steven Craig Miller
 
A

avkokin

To: Avkokin,

Sub InsField()
    Dim nSections As Long
    Dim nIndex As Long

    nSections = ActiveDocument.Sections.Count
    If nSections > 1 Then
        For nIndex = 2 To nSections
            ActiveDocument.Sections(nIndex).Range.Select
            Selection.Collapse wdCollapseStart
            Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldPage,
PreserveFormatting:=False
        Next nIndex
    End If
End Sub

Steven Craig Miller






- Show quoted text -

Thank you very much! I'm undestand it.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top