![]() |
|
|||||||
|
|
|
Thread Tools | Display Modes |
|
#1
|
|||
|
|||
|
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
|
|
#2
|
|||
|
|||
|
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
|
|
#3
|
|||
|
|||
|
Terrific -- thanks for your help, Greg!
|
|
|
|
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 |
Conditional Formatting - Duplicates - Apply to specified range
|
music_al | Excel Programming | 1 | 01-25-2017 05:34 AM |
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 |