View Single Post
 
Old 10-06-2018, 05:15 PM
gmaxey gmaxey is offline Windows 7 32bit Office 2016
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,598
gmaxey is just really nicegmaxey is just really nicegmaxey is just really nicegmaxey is just really nicegmaxey is just really nice
Default

Code:
Sub CodeSnippet_ConstrainHighlighting() ' 10/06, 1018
    Dim i As Long
    Dim MyRange As Range ' A range for the body of the doc
    ' Create a new document
    'Documents.Add DocumentType:=wdNewBlankDocument
    ' Show field codes
    ActiveWindow.View.ShowFieldCodes = True
    'Add text to the doc, format it, add field, and loop
    For i = 1 To 2
        With ActiveDocument
            ' Set the range as the very end of the document
            Set MyRange = .Range(.Content.End - 1, .Content.End - 1)
        End With
            
        ' Add text and format apply
        With MyRange
            ' Insert the file name into the document
            .Text = "Test Heading Text Here"
            ' Apply H1 style
            .Style = ActiveDocument.Styles("Heading 1")
            ' Remove numbering
            .ListFormat.RemoveNumbers NumberType:=wdNumberParagraph
            
            '>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
            ' Add highlight
            
           
            
            .HighlightColorIndex = wdYellow
            
            '<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
            .Collapse 0
            .HighlightColorIndex = wdAuto
            '.End = ActiveDocument.Range.End
    
            ' Add two lines
            .InsertParagraphAfter
            .InsertParagraphAfter
    
            ' Set range to end of doc
            With ActiveDocument
                Set MyRange = .Range(.Content.End - 1, .Content.End - 1)
            End With
            
            ' Insert the IncludeText field and file path
            .Fields.Add Range:=MyRange, Type:=wdFieldIncludeText, Text:="""C:\\testdoc.docx"""
    
            ' Collapse the range to the end of the document
            .End = ActiveDocument.Range.End
            .Collapse wdCollapseEnd
            ' Insert a line and a page break, but not if it's the last item in the doc
            If i < 2 Then
                .InsertParagraphAfter
                .InsertBreak Type:=wdPageBreak
            End If
        End With
    Next i
End Sub
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote