PDA

View Full Version : [SOLVED:] Moving comments into the text with highlighting



dbowlds
05-11-2018, 03:38 AM
Hello, Mr. Greg Maxey provided the below code on another forum (msofficeforums). The purpose of the code was to relocate document comments directly into the text, placing them immediately after the text in question (the referenced text). This code works perfectly for its intended purpose. I would love to use the code but have found in practice that the comments become lost in the text and it is difficult to tell exactly what text is being referenced. If tracked changes are on, the inserted comments show up as inserted text. However, what I would love this code to do is:

1. Turn off track changes (in case it is on), I assume via:
ActiveDocument.TrackRevisions = False 'I don't want the inserted comment text to appear changes
ActiveDocument.TrackFormatting = False 'I don't want below highlights to appear as changes

2. Highlight (in Yellow) the referenced text (so it is then clear what text is in question or otherwise being referred to). I tried to select oRng and use oRng.Shading.BackgroundPatternColor = wdColorYellow but it would not do anything. I don't think I know how to properly select the oRng.

3. Highlight (in a different color) the newly inserted text (comment text).

Can anyone please help me with this? Such help would be most appreciated.
Doug

Sub ScratchMacro()
'A basic Word macro coded by Greg Maxey
Dim lngIndex As Long
Dim oRng As Range
For lngIndex = ActiveDocument.Range.Comments.Count To 1 Step -1
Set oRng = ActiveDocument.Range.Comments(lngIndex).Reference
oRng.InsertAfter " " & ActiveDocument.Range.Comments(lngIndex).Range.Text
ActiveDocument.Range.Comments(lngIndex).Delete
Next lngIndex
lbl_Exit:
Exit Sub
End Sub

macropod
05-12-2018, 12:05 AM
Try:

Sub Demo()
Application.ScreenUpdating = False
Dim Cmnt As Comment, Rng As Range, bTrk As Boolean
With ActiveDocument
bTrk = .TrackRevisions
.TrackRevisions = False
For Each Cmnt In .Comments
Set Rng = Cmnt.Reference
With Rng
.Collapse wdCollapseEnd
.Text = Cmnt.Range.Text
.HighlightColorIndex = wdYellow
End With
Next
If .Comments.Count > 0 Then .DeleteAllComments
.TrackRevisions = bTrk
End With
Application.ScreenUpdating = False
End Sub
PS: When posting code, please use the code tags, indicated by the # button on the posting menu. Without them, your code loses much of whatever structure it had.

dbowlds
05-12-2018, 11:48 AM
Hi Paul, thank you for your response. (Also, thank you for the reminder about tagging the code properly. I will do better next time, I promise.)
Your code does a great job of copying the comments into the text and then highlighting the comments and then deleting the comments. However, one thing I would still like to occur is for the "referenced" text, the text the comments are in reference to, be also highlighted a different color. Even though the yellow comment text is inserted immediately following the referenced text, the reader cannot be sure of the specific passage being commented about. For example, does the comment apply to the entire preceding sentence? Or the preceding paragraph? So, is there any way to also highlight (in a different color), the text being referenced by the comment? I think VBA considers that to be ActiveDocument.Range.Comments(Index).Reference but am not sure.

macropod
05-12-2018, 04:35 PM
However, one thing I would still like to occur is for the "referenced" text, the text the comments are in reference to, be also highlighted a different color
That's as simple as inserting:
.HighlightColorIndex = wdTurquoise
before:
.Collapse wdCollapseEnd

dbowlds
05-13-2018, 11:10 AM
Hello Paul, thanks again for the response. For some reason this still doesn't work for me. Below is what the code looks like. The comments that has been moved into the document become yellow highlighted. However, the referenced text in the document remains unhighlighted. Any ideas?

Private Sub cmdMoveComments_Click()
Application.ScreenUpdating = False
Dim Cmnt As Comment, Rng As range, bTrk As Boolean
With ActiveDocument
bTrk = .TrackRevisions
.TrackRevisions = False
For Each Cmnt In .Comments
Set Rng = Cmnt.Reference
With Rng
.HighlightColorIndex = wdGreen
.Collapse wdCollapseEnd
.text = Cmnt.range.text
.HighlightColorIndex = wdYellow
End With
Next
If .Comments.Count > 0 Then .DeleteAllComments
.TrackRevisions = bTrk
End With
Application.ScreenUpdating = True
End Sub

macropod
05-13-2018, 03:03 PM
Change:
Set Rng = Cmnt.Reference
to:
Set Rng = Cmnt.Scope

dbowlds
05-14-2018, 03:34 AM
Paul, this works perfectly! Thank you very much!

dbowlds
05-14-2018, 05:58 AM
Paul, even though I closed this thread as solved, could I impose upon you again? Hopefully, this is an easy add. It occurred to me that if the document has multiple reviewers then we'd lose track of who made which comments. How could the code be modified to also include the comment author's name inserted after the comment text?

macropod
05-14-2018, 04:00 PM
You might change:
.Text = Cmnt.Range.Text
to:
.Text = Cmnt.Range.Text & "|" & Cmnt.Author & "|"

dbowlds
05-15-2018, 03:46 AM
Paul, this worked perfectly!
Thank you very much!
Have a great day.
Doug