View Single Post
 
Old 10-06-2018, 11:21 AM
Peterson Peterson is offline Windows 10 Office 2016
Competent Performer
 
Join Date: Jan 2017
Posts: 143
Peterson is on a distinguished road
Default Highlighting applied to range ending in a paragraph continues to apply to text added after

I'm working on a macro that inserts text into a document (as a range), formats it, and highlights it; after, a couple lines are added and then a field.

The trouble is, highlighting applied to the inserted text range is also being applied to everything I add after, that is, the subsequent field is also ending up highlighted.

It appears that the range includes the paragraph mark at the end, but when I redefine the range to exclude one character at the end, the problem persists. When I exclude two characters at the end, the highlighting does not bleed into anything I add after; however, the last character is not highlighted.

This is puzzling to me because when I manually apply highlighting to selected text that includes a paragraph mark, the highlighting Word applies excludes the mark, and the following paragraph I add is not highlighted.

Thanks
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
            
            .End = ActiveDocument.Range.End - 2
            
            Debug.Print MyRange
            
            .HighlightColorIndex = wdYellow
            
            '<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
            
            .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
Reply With Quote