我想知道是否有办法更改文档中的修订作者,我找到了更改评论作者的方法,但没有找到修订部分的方法。我试图在 Revisions.object 文档中找到一个属性/方法来更改它,但我一无所获。
正如我所说,我已经尝试过这样做:
Sub ChangeCommentCreator()
Dim I As Long
Dim Novo As String
Dim Corto As String
If Selection.Comments.Count = 0 Then
MsgBox "No comments in your selection!", vbInformation, "Alerta"
Exit Sub
End If
Novo = InputBox("New author name?", "Alerta")
Corto = InputBox("New author initials?", "Alerta")
If Novo = "" Or Corto = "" Then
MsgBox "The author name/initials can’t be empty.", vbInformation, "Alerta"
Exit Sub
End If
With Selection
For I = 1 To .Comments.Count
.Comments(I).Author = Novo
.Comments(I).Initial = Corto
Next I
End With
End Sub
我走的路是对的还是没有办法改变?
最佳答案
修订版会自动使用 UI 中选定的用户名。 Revisions
的 Author
属性的语言引用说明:
Returns the name of the user who made the specified tracked change. Read-only String.
所以没有直接的方法使用 VBA 和对象模型来改变它。
可以通过编辑底层的 Word Open XML 来更改它,如以下代码所示。但是,我注意到这似乎混淆了 Word - 在运行宏后,文档中没有识别出任何修订。只有在保存、关闭并重新打开后,Word 才能再次“看到”修订。
Sub ChangeAuthorName()
Dim sWOOXML As String
Dim findAuthor As String
Dim replaceAuthor As String
findAuthor = "w:author=" & Chr(34) & "Cindy Meister" & Chr(34)
replaceAuthor = "w:author=" & Chr(34) & "unknown" & Chr(34)
sWOOXML = ActiveDocument.content.WordOpenXML
sWOOXML = Replace(sWOOXML, findAuthor, replaceAuthor)
ActiveDocument.content.InsertXML sWOOXML
End Sub
请注意,这也可能会更改作者的评论姓名。更“优雅”的方法是利用 XML 解析器(例如 MSXML)并使用特定节点。或者甚至使用可处理已关闭 文档并编辑 Word Open XML 的包。但这是使用直接 Word VBA 的最简单方法。
关于vba - 有没有办法更改Word文档中修订的作者?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57867579/