Consulting

Results 1 to 10 of 10

Thread: Moving comments into the text with highlighting

  1. #1
    VBAX Regular
    Joined
    Mar 2018
    Location
    Leesburg
    Posts
    40
    Location

    Moving comments into the text with highlighting

    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

  2. #2
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    3,424
    Location
    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.
    Cheers
    Paul Edstein
    [MS MVP - Word]

  3. #3
    VBAX Regular
    Joined
    Mar 2018
    Location
    Leesburg
    Posts
    40
    Location
    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.

  4. #4
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    3,424
    Location
    Quote Originally Posted by dbowlds View Post
    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
    Cheers
    Paul Edstein
    [MS MVP - Word]

  5. #5
    VBAX Regular
    Joined
    Mar 2018
    Location
    Leesburg
    Posts
    40
    Location
    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

  6. #6
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    3,424
    Location
    Change:
    Set Rng = Cmnt.Reference
    to:
    Set Rng = Cmnt.Scope
    Cheers
    Paul Edstein
    [MS MVP - Word]

  7. #7
    VBAX Regular
    Joined
    Mar 2018
    Location
    Leesburg
    Posts
    40
    Location
    Paul, this works perfectly! Thank you very much!

  8. #8
    VBAX Regular
    Joined
    Mar 2018
    Location
    Leesburg
    Posts
    40
    Location
    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?

  9. #9
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    3,424
    Location
    You might change:
    .Text = Cmnt.Range.Text
    to:
    .Text = Cmnt.Range.Text & "|" & Cmnt.Author & "|"
    Cheers
    Paul Edstein
    [MS MVP - Word]

  10. #10
    VBAX Regular
    Joined
    Mar 2018
    Location
    Leesburg
    Posts
    40
    Location
    Paul, this worked perfectly!
    Thank you very much!
    Have a great day.
    Doug

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •