PDA

View Full Version : [SOLVED:] Find and Replace in Word macro always starts with 2nd instance of string?



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

gmaxey
01-11-2014, 05:13 PM
Typically arrays are zero based. Try:

For j = 0 To iCount

Not sure what and where iCount is defined, but you may have to change and use:

For j = 0 To iCount - 1

peroron2000
01-11-2014, 05:43 PM
Thanks for the reply. I actually tried that already. The loop seems to work just fine. I can't understand why word starts by finding the second instance of the string.

fumei
01-11-2014, 07:06 PM
I am not sure what the array is for as:

sOneJobRecord(j) = "O(NSOURCE ASSIGNMENT DETAILS" & "*" & "ASSIGNMENT INSTRUCTIONS)"
With Selection.Find
.Text = sOneJobRecord(j)
means that every time the .Text is exactly the same.

Further, I do not know why you have the string broken up into three parts because there is no other variable in the value to sOneJobRecord(j).

"O(NSOURCE ASSIGNMENT DETAILS" & "*" & "ASSIGNMENT INSTRUCTIONS)"

is exactly the same as

"O(NSOURCE ASSIGNMENT DETAILS*ASSIGNMENT INSTRUCTIONS)"

I am inclined to agree with macropod that it may have something to do with using 1 versus 0. But in any case, you have

sOneJobRecord(j) = Application.Selection.Text

but that means nothing because you explicitly change it in the next j with

sOneJobRecord(j) = "O(NSOURCE ASSIGNMENT DETAILS" & "*" & "ASSIGNMENT INSTRUCTIONS)"

gmaxey
01-11-2014, 09:10 PM
When and where did macropod enter the picture?:dunno

macropod
01-11-2014, 10:00 PM
When and where did macropod enter the picture?:dunno
Someone look'n for me???

fumei
01-12-2014, 01:50 PM
Oh crap...sorry Greg. I had my brain stuck on another thread. my bad.

gmaxey
01-12-2014, 03:41 PM
Gerry,

No harm, no foul.

BREAK

Paul, if your ears are a burn'in and have something to offer then jump right in ;-)

fumei
01-13-2014, 12:01 AM
peroron2000, can you post a document (removing any sensitive information). That way we can see your full code as well.

peroron2000
01-19-2014, 04:23 PM
peroron2000, can you post a document (removing any sensitive information). That way we can see your full code as well.


I actually figured this out a day or two after I originally posted it. The problem was the collapse direction.


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

This bit of code evaluates the state of find.forward. The code was setting .collapsedirection to wdcollapseend and that was making it skip the first instance it found and move to the second. Thank you for your help though.