peroron2000
01-11-2014, 03:27 PM
I created a macro that contains the following code. The code is supposed to loop through my for next loop iCount times and each time extract out a specific string and store it in a string array. It basically works fine except that it always finds the second instance of the string the first time and then it continues on sequentially. I thought maybe there was something wrong with the actual first string causing it to not be found but even when I move the first to the end of the document it then skips over the new first item and starts with the second. Can anyone tell me why this is happening? Thank you in advance.
For j = 1 To iCount
Selection.HomeKey Unit:=wdStory
'clear out any previous find formatting
Selection.Find.ClearFormatting
'clear out any previous replacement formatting
Selection.Find.Replacement.ClearFormatting
'find label, data item label
sOneJobRecord(j) = "O(NSOURCE ASSIGNMENT DETAILS" & "*" & "ASSIGNMENT INSTRUCTIONS)"
With Selection.Find
.Text = sOneJobRecord(j)
.Replacement.Text = "U\1"
.MatchWildcards = True
End With
Selection.Find.Execute
With Selection
If .Find.Forward = True Then
.Collapse Direction:=wdCollapseStart
Else
.Collapse Direction:=wdCollapseEnd
End If
.Find.Execute Replace:=wdReplaceOne
If .Find.Forward = True Then
.Collapse Direction:=wdCollapseEnd
Else
.Collapse Direction:=wdCollapseStart
End If
.Find.Execute
End With
sOneJobRecord(j) = Application.Selection.Text
Next j
For j = 1 To iCount
Selection.HomeKey Unit:=wdStory
'clear out any previous find formatting
Selection.Find.ClearFormatting
'clear out any previous replacement formatting
Selection.Find.Replacement.ClearFormatting
'find label, data item label
sOneJobRecord(j) = "O(NSOURCE ASSIGNMENT DETAILS" & "*" & "ASSIGNMENT INSTRUCTIONS)"
With Selection.Find
.Text = sOneJobRecord(j)
.Replacement.Text = "U\1"
.MatchWildcards = True
End With
Selection.Find.Execute
With Selection
If .Find.Forward = True Then
.Collapse Direction:=wdCollapseStart
Else
.Collapse Direction:=wdCollapseEnd
End If
.Find.Execute Replace:=wdReplaceOne
If .Find.Forward = True Then
.Collapse Direction:=wdCollapseEnd
Else
.Collapse Direction:=wdCollapseStart
End If
.Find.Execute
End With
sOneJobRecord(j) = Application.Selection.Text
Next j