I'm working on a macro that inserts an IncludeText field at the end of a document, after which I need to add a paragraph mark and a page break. For some reason that I've not been able to determine but which appears to be due to the field,
wdCollapseEnd isn't working, so the paragraph mark and page break are inserted
prior to the field, not after. When I try inserting just plain text, it works. I've not been able to find a solution in my web searching. Thanks.
Code:
Sub CodeSnippet()
' Create a blank document, insert an IncludeText field code, and add a paragraph
' mark and page break after; loop the insertion action
Dim MyRange As Range
Dim i As Long
Documents.Add DocumentType:=wdNewBlankDocument
For i = 1 To 2
With ActiveDocument
Set MyRange = .Range(.Content.End - 1, .Content.End - 1)
' When I insert just text, the macro works...
'MyRange.Text = "TEST"
'But when I insert a field, it doesn't work...
.Fields.Add Range:=MyRange, Type:=wdFieldIncludeText, Text:="""C:\\testdoc.docx"""
MyRange.Collapse wdCollapseEnd
End With
If i < 2 Then 'Don't add a page break if it's the last one
MyRange.InsertParagraphAfter
MyRange.InsertBreak Type:=wdPageBreak
End If
Next i
End Sub