I have been using a macro called "ReinsertComments" that was posted by someone in the comments section over at CyberText (see macro below). It reinserts all the comments in a Word document. I would like to find a way, if at all possible, to reinsert only one user's comments, but not all other users' comments.
- Is it possible to specify per author which comments will be reinserted?
- Could you run an If/Then statement to specify a user name for the comments to be reinserted? Perhaps something along the lines of
If myComment.Author = "Jane" Then
- If a version of that is possible, where should I insert the If/Then and End If portion in the macro below?
Thanks to all! :)
Sub CommentsReinsert()
Dim myComment As Comment
Dim myComText As String
Dim comStart
Dim comEnd
Dim i
On Error GoTo Done
Application.ScreenUpdating = False
If ActiveDocument.TrackRevisions = True Then
With ActiveDocument
.TrackRevisions = False
End With
End If
For i = ActiveDocument.Comments.Count To 1 Step -1
Set myComment = ActiveDocument.Comments(i)
myComText = myComment.Range.Text
comStart = myComment.Scope.Start
comEnd = myComment.Scope.End
myComment.Reference.Select
myComment.Delete
ActiveDocument.Range(comStart, comEnd).Select
ActiveDocument.Comments.Add _
Range:=Selection.Range, Text:=myComText
Next i
ActiveWindow.ActivePane.Close
Application.ScreenUpdating = True
Done:
End Sub
CodePudding user response:
You could use code like:
Sub ReinsertComments()
Application.ScreenUpdating = False
Dim bRev As Boolean, i As Long, Rng As Range, Cmt As Comment
With ActiveDocument
bRev = .TrackRevisions
.TrackRevisions = False
For i = .Comments.Count To 1 Step -1
With .Comments(i)
Select Case .Author
Case "Jane"
Set Rng = .Range
Set Cmt = ActiveDocument.Comments.Add(Range:=.Scope, Text:="")
Cmt.Range.FormattedText = Rng.FormattedText ': Cmt.Author = "Anon."
Rng.Comments(1).Delete
End Select
End With
Next
.TrackRevisions = bRev
End With
Set Cmt = Nothing: Set Rng = Nothing
Application.ScreenUpdating = True
End Sub
Unlike the code you posted, the above preserves the original comment's formatting. Note the commented-out code for changing the inserted comment's author name.
An alternative approach would be to temporarily change the UserName & Initials in the Word Options, to that of the commentator, then simply change the Comment.Author property to whatever other name you want - without all the deletion/reinsertion circumlocution.
I imagine people would be concerned about the dates if there are replies to comments and the above macro was deleting then reinserting them with a different date...
CodePudding user response:
As I said in my previous reply, you could use the alternative approach of:
Sub ChangeCommentAuthor()
Application.ScreenUpdating = False
Dim bRev As Boolean, i As Long, UsrNm As String, UsrIn As String, bUsr As Boolean
UsrNm = Application.UserName: UsrIn = Application.UserInitials: bUsr = Options.UseLocalUserInfo
'Nominate the UserName & Initials to change from
Application.UserName = "Figgie 10": Application.UserInitials = "F": Options.UseLocalUserInfo = True
With ActiveDocument
bRev = .TrackRevisions
.TrackRevisions = False
For i = .Comments.Count To 1 Step -1
With .Comments(i)
'Nominate the UserName & Initials to change to
If .Author = Application.UserName Then .Author = "John Doe": .Initial = "JD"
End With
Next
.TrackRevisions = bRev
End With
Application.UserName = UsrNm: Application.UserInitials = UsrIn: Options.UseLocalUserInfo = bUsr
Application.ScreenUpdating = True
End Sub