Training
Module
Iterate through a code block using for statement in C# - Training
Use the for iteration statement to loop a pre-set number of times and control the iteration process.
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
There are several different ways you can loop on the elements of a collection. However, the recommended method for looping on a collection is to use the For Each...Next loop. In this structure, Visual Basic repeats a block of statements for each object in a collection. The following example displays the name of each document in the Documents collection.
Sub LoopThroughOpenDocuments()
Dim docOpen As Document
For Each docOpen In Documents
MsgBox docOpen.Name
Next docOpen
End Sub
Instead of displaying each element name in a message box, you can use an array to store the information. This example uses an array to store the name of each bookmark contained in the active document.
Sub LoopThroughBookmarks()
Dim bkMark As Bookmark
Dim strMarks() As String
Dim intCount As Integer
If ActiveDocument.Bookmarks.Count > 0 Then
ReDim strMarks(ActiveDocument.Bookmarks.Count - 1)
intCount = 0
For Each bkMark In ActiveDocument.Bookmarks
strMarks(intCount) = bkMark.Name
intCount = intCount + 1
Next bkMark
End If
End Sub
You can loop through a collection to conditionally perform a task on members of the collection. For example, the following code updates the DATE fields in the active document.
Sub UpdateDateFields()
Dim fldDate As Field
For Each fldDate In ActiveDocument.Fields
If InStr(1, fldDate.Code, "Date", 1) Then fldDate.Update
Next fldDate
End Sub
You can loop through a collection to determine if an element exists. For example, the following code displays a message if an AutoText entry named "Filename" is part of the AutoTextEntries collection.
Sub FindAutoTextEntry()
Dim atxtEntry As AutoTextEntry
For Each atxtEntry In ActiveDocument.AttachedTemplate.AutoTextEntries
If atxtEntry.Name = "Filename" Then _
MsgBox "The Filename AutoText entry exists."
Next atxtEntry
End Sub
Have questions or feedback about Office VBA or this documentation? Please see Office VBA support and feedback for guidance about the ways you can receive support and provide feedback.
Training
Module
Iterate through a code block using for statement in C# - Training
Use the for iteration statement to loop a pre-set number of times and control the iteration process.