Stumped with Field.Add

G

Greg

Can someone explain why test1 generates a run-time error 4605
but if I change myRng to Selection.Range in test2 and put the cursor in
Row 1 Column 1 of the first table it works fine? Thanks.

Sub Test1()
Dim myRng As Range
Set myRng = ActiveDocument.Tables(1).Cell(1, 1).Range
ActiveDocument.Fields.Add Range:=myRng, _
Type:=wdFieldIncludeText, Text:="""c:\\Job 1.doc"""
End Sub

Sub Test2()
ActiveDocument.Fields.Add Range:=Selection.Range, _
Type:=wdFieldIncludeText, Text:="""c:\\Job 1.doc"""
End Sub
 
G

Greg

I found the cause.

Range Required Range. The range where you want to add the field. If
the range isn't collapsed, the field replaces the range.

I added a line:
Set myRng = ActiveDocument.Tables(1).Cell(­1, 1).Range
myRng.Collapse
ActiveDocument.Fields.Add Range:=myRng, _
Type:=wdFieldIncludeText, Text:="""c:\\Job 1.doc"""

Apparently the .Cell.Range is fixed and can't be replaced. Makes sense.
 
Top