Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 10-04-2018, 11:34 PM
Peterson Peterson is offline How to collapse range after inserting field at end of document Windows 10 How to collapse range after inserting field at end of document Office 2016
Competent Performer
How to collapse range after inserting field at end of document
 
Join Date: Jan 2017
Posts: 141
Peterson is on a distinguished road
Default How to collapse range after inserting field at end of document

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
Reply With Quote
  #2  
Old 10-05-2018, 12:31 AM
gmayor's Avatar
gmayor gmayor is offline How to collapse range after inserting field at end of document Windows 10 How to collapse range after inserting field at end of document Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,101
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

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
Reply With Quote
  #3  
Old 10-05-2018, 12:42 AM
Peterson Peterson is offline How to collapse range after inserting field at end of document Windows 10 How to collapse range after inserting field at end of document Office 2016
Competent Performer
How to collapse range after inserting field at end of document
 
Join Date: Jan 2017
Posts: 141
Peterson is on a distinguished road
Default

Terrific -- thank you very much!
Reply With Quote
Reply



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
How to collapse range after inserting field at end of document 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
How to collapse range after inserting field at end of document 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
How to collapse range after inserting field at end of document Range.Collapse? tinfanide Word VBA 5 01-17-2013 06:02 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 03:11 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft