Reputation: 90
I have written a macro that replaces a file path with the current files location in Word 2016.
Dim i As Long, j As Long
Dim s As String
s = ActiveDocument.Path
Dim sa As String
sa = Replace(s + "\test1.xlxs", "\", "\\")
For Each myStoryRange In ActiveDocument.StoryRanges
With myStoryRange.Find
.Text = "C:*test1.xlsx"
.Replacement.Text = sa
.MatchWildcards = True
.Wrap = wdFindContinue
.Execute Replace:=wdReplaceAll
End With
Next myStoryRange
End Sub
This shows the error "the replacement text contains a group number that is out of range"
The 'sa' variable has the correct string in it, i checked this at run time. Also when i replace
.Replacement.Text = sa
with
.Replacement.Text = "bla"
It works. At run time an example of 'sa' would be "C:\\Users\\Me\\Documents\\test1.xlsx"
Does anyone have any suggestions what might be the problem?
Upvotes: 1
Views: 306
Reputation: 50144
Wildcards and backslashes do not work well together when finding and replacing text - the backslash has its own particular use.
To replace a backslash in this case, use its ASCII equivalent: "^92^92"
instead of "\\"
.
Upvotes: 2