I am doing changes to a 'word track changes applied' document using VBA.
The red colored paragraph ending mark is a inserted paragraph ending mark.(Make 'track changes ON' > put cursor at the end pf first paragraph > Press Enter > Insert the new paragraph content > format with a different style)
I need to add a field for the insertions with text "Insertion" + inserted text. (The output document in this process goes through some other processes(not in VBA), so in order to let that other processes "That is an insertion", we are adding that field)
Public Sub main()
Dim objRange As Word.Range
Set objRange = Word.ActiveDocument.Range
TrackInsertions objRange
End Sub
Public Sub TrackInsertions(WordRange As Word.Range)
Dim objRevision As Word.Revision
Dim objContentControl As Word.ContentControl
Dim objRange As Word.Range
With WordRange
For Each objRevision In .Revisions
If AllowTrackChangesForInsertion(objRevision) = True Then
On Error Resume Next
With objRevision
Set objRange = .Range
.Range.Font.Underline = wdUnderlineSingle
.Range.Font.ColorIndex = wdRed
Set objField = objRange.Fields.Add(Range:=objRange, Type:=wdFieldComments, Text:="Insertion " + objRange.Text, PreserveFormatting:=False)
.Accept
End With
Err.Clear
End If
Next objRevision
End With
End Sub
Private Function AllowTrackChangesForInsertion(ByRef Revision As Word.Revision) As Boolean
With Revision
Select Case .Type
Case wdRevisionInsert, wdRevisionMovedFrom, wdRevisionMovedTo, wdRevisionParagraphNumber, wdRevisionStyle
AllowTrackChangesForInsertion = IsTextChangeExist(.Range)
Case Else
AllowTrackChangesForInsertion = False
End Select
End With
End Function
Private Function IsTextChangeExist(ByRef Range As Word.Range) As Boolean
'False if the range contain inlineshapes, word fields and tables
Select Case True
Case Range.InlineShapes.Count > 0
IsTextChangeExist = False
Case Range.Fields.Count > 0
IsTextChangeExist = False
Case Range.Tables.Count > 0
IsTextChangeExist = False
Case Else
IsTextChangeExist = True
End Select
End Function
The issue is, when do the above change, the second paragraph with inserted text (I am not counting the paragraph ending marks as paragraphs here) and the first paragraph has turned into one paragraph. As within this code part, actual paragraph count get reduced, the final output (after run through other application) also contains the reduced paragraphs count, which is the issue.
When we read through the revisions, red colored paragraph ending mark + second paragraph goes as one revision. Even that revision has multiple paragraphs, it goes as one revision. If we have applied seperate paragraph styles to the inserted paragraphs, after run through this code, the revision got one style, the immediate paragraph's style. This all occurs because of that Inserted paragraph ending mark.
I tried moving through the word paragraphs, because what I want is to avoid changing the paragraphs count in the document. (tried bottom to up, up to bottom both )But that didn't solve my issue.
Also I have tried to split the revision into two revisions, when
If objParagraph.End < objRevision.Range.End Then
.....
End If
But I am unable to apply range to a new revision.
Now I want to split the revision into parts if we identified a paragraph ending mark within the content, and apply separate fields to them, if possible. So the paragraph count nor the paragraph styles won't change after adding fields.
Or, Is there a way to accept all paragraph ending marks(only) that are marked as inserted within a word document?
Could anybody please help me to proceed with the code, Please tell me if you got other ideas.
Thank you in advance.