![]() |
|
|
|
#1
|
|||
|
|||
|
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
|
|
#2
|
||||
|
||||
|
After inserting a field move the end of the range to the end of the document then collapse the range e.g.
Code:
.Fields.Add Range:=MyRange, Type:=wdFieldIncludeText, Text:="""C:\\testdoc.docx""" MyRange.End = ActiveDocument.Range.End MyRange.Collapse wdCollapseEnd
__________________
Graham Mayor - MS MVP (Word) (2002-2019) Visit my web site for more programming tips and ready made processes www.gmayor.com |
|
#3
|
|||
|
|||
|
Terrific -- thank you very much!
|
|
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Loop to Collapse Paragraphs within Range Not Working | poetofpiano | Word VBA | 1 | 03-11-2018 06:29 PM |
Name a Range in a Word Document and then copy that range to the end of the doc w button click
|
DanNatCorning | Word VBA | 1 | 04-29-2016 10:47 PM |
Inserting text field into word document
|
BeataG | Word VBA | 2 | 01-03-2014 12:56 AM |
| Date field inserting merge date in blank field | Lesley | Mail Merge | 5 | 09-30-2013 01:49 AM |
Range.Collapse?
|
tinfanide | Word VBA | 5 | 01-17-2013 06:02 PM |