Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 10-06-2018, 11:21 AM
Peterson Peterson is offline Highlighting applied to range ending in a paragraph continues to apply to text added after Windows 10 Highlighting applied to range ending in a paragraph continues to apply to text added after Office 2016
Competent Performer
Highlighting applied to range ending in a paragraph continues to apply to text added after
 
Join Date: Jan 2017
Posts: 141
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
  #2  
Old 10-06-2018, 05:15 PM
gmaxey gmaxey is offline Highlighting applied to range ending in a paragraph continues to apply to text added after Windows 7 32bit Highlighting applied to range ending in a paragraph continues to apply to text added after Office 2016
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,429
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
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
  #3  
Old 10-08-2018, 02:50 PM
Peterson Peterson is offline Highlighting applied to range ending in a paragraph continues to apply to text added after Windows 10 Highlighting applied to range ending in a paragraph continues to apply to text added after Office 2016
Competent Performer
Highlighting applied to range ending in a paragraph continues to apply to text added after
 
Join Date: Jan 2017
Posts: 141
Peterson is on a distinguished road
Default

Terrific -- thanks for your help, Greg!
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Paragraph mark added on the top of every second page aptbs00 Word 6 09-14-2018 03:53 PM
Highlighting applied to range ending in a paragraph continues to apply to text added after Conditional Formatting - Duplicates - Apply to specified range music_al Excel Programming 1 01-25-2017 05:34 AM
Highlighting applied to range ending in a paragraph continues to apply to text added after Word changing my body text to heading text when I apply paragraph numbering ddiemetric Word 1 01-17-2017 01:10 PM
Paragraph space before - can styles apply it intelligently? timpani Word 7 10-23-2012 04:08 PM
Paragraph ending in following page tadlomc Word 1 01-18-2012 05:31 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 04:25 PM.


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