【问题标题】:Excel - Change old comments to new Threaded commentsExcel - 将旧评论更改为新的线程评论
【发布时间】:2023-03-12 16:57:02
【问题描述】:

我厌倦了 cmets 到处乱跑,导致滚动条缩小,所以我想构建一个宏来将所有旧注释转换为新的线程注释。我遇到的问题是我需要保留原始作者,但是当我尝试更改我的 Threaded Comment 的作者时,我得到了一个错误,请参见下面的代码。

如何更改 Threaded cmets 的作者?谷歌没有给我指路:)

Public Sub Convert_Notes_to_Comments()
    
    Dim rng As Range, cell As Range
    Dim CommentText As String, cleanedText As String, commentAuthor, newComment As CommentThreaded
    Set rng = Selection
    
    For Each cell In rng
        If Not cell.Comment Is Nothing Then
            CommentText = cell.Comment.Text
            commentAuthor = cell.Comment.Author
            cleanedText = Replace(CommentText, commentAuthor & ":", "")
            cell.Comment.Delete
            Set newComment = cell.AddCommentThreaded(cleanedText)
            cell.CommentThreaded.Author = commentAuthor   'I GET AN ERROR HERE
        End If

    Next cell
End Sub

【问题讨论】:

  • Author 属性是只读的。想法 - 也许在 AddCommentThreaded 之前将您的 UserName 更改为 commentAuthor
  • Application.username 不幸地没有工作。我将尝试使用 Environ("username")
  • 您要更改特定范围(选择)的 cmets,还是全部(在特定工作表或所有工作簿中)?
  • 恐怕CommentThreaded.Author 是只读的。评论完成后会自动放置。它不能随心所欲地改变......你可以使用一个技巧。不要替换任何东西并简单地使用AddCommentThreaded(CommentText),它将保留前用户的前用户。你使用 Office 365 吗?

标签: excel vba comments


【解决方案1】:

我觉得自己很笨。 Excel 内置了将旧的 cmets 转换为新的线程化 cmets 的功能,保留了作者。这很好,因为我没有找到任何其他有效的解决方案,因为线程 cmets 中的 Author 字段是只读的。

【讨论】:

    【解决方案2】:

    首先,CommentThreaded.Authorread-Only 属性。 Excel 会自动添加新用户。如果您使用 Excel 365(就像我想的那样),则所选用户不是 Windows 用户帐户,而是 Microsoft 登录帐户...

    请尝试下一个代码。它会做你尝试做的事情。除此之外,它保留以前的用户名,如果它与实际(使用的)不同:

    Sub changeOldCommentsInThC()
      Dim ws As Worksheet, rngComm As Range, commentAuthor As String, cleanedText As String
      Dim cel As Range, CommentText As String, newComment As CommentThreaded
      
          Set ws = ActiveSheet 'use here the sheet you need
          On Error Resume Next 'if no comment existing it will raise an error
             Set rngComm = ws.UsedRange.SpecialCells(xlCellTypeComments) 'all comments on the sheet
          On Error GoTo 0
          If Not rngComm Is Nothing Then               'if there are (old) comments:
                For Each cel In rngComm.cells          'iterate between range cells
                    CommentText = cel.Comment.text     'memorize the comment text
                    commentAuthor = cel.Comment.Author 'memorize the comment author
                    cel.Comment.Delete                 'delete the comment
                    cleanedText = Replace(CommentText, commentAuthor & ":" & vbLf, "") 'remove the former user
                    Set newComment = cel.AddCommentThreaded(cleanedText)                'create the Threaded comment
                    If commentAuthor <> newComment.Author.Name Then 'if not the author of both comments is the same:
                        cel.CommentThreaded.Delete     'delete the newly created threaded comment
                        Set newComment = cel.AddCommentThreaded("Former user: " & CommentText) 'recreate it keeping the former user name
                   End If
                Next
          End If
    End Sub
    

    请进行测试并发送一些反馈。它可以很容易地适应更改工作簿中所有工作表的所有旧 cmets...

    【讨论】:

    • @Dumitru Daniel 您是否有时间测试上述建议?如果经过测试,它没有澄清问题吗?只是好奇... :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-27
    • 2018-04-14
    • 2016-03-24
    相关资源
    最近更新 更多