有没有办法更改Word文档中修订的作者?

There is a way to change the author of revisions in a Word document?

我想知道是否有办法更改文档中的修订作者,我找到了更改评论作者的方法,但修订部分没有。我试图在 Revisions.object 文档中找到 property/method 来更改它,但我一无所获。

正如我所说,我已经尝试这样做了:

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 中选定的用户名。 RevisionsAuthor 属性 的语言参考指出:

Returns the name of the user who made the specified tracked change. Read-only String.

所以没有直接的方法使用 VBA 和对象模型来改变它。

可以通过编辑底层 Word Open XML 来更改它,如以下代码所示。但是,我注意到这似乎混淆了 Word - 在 运行 宏之后,文档中没有识别出任何修订。只有在保存、关闭并重新打开后,Word 才会再次 "see" 修订。

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

请注意,这也可能会更改作者的评论姓名。更 "elegant" 的方法是利用 XML 解析器(例如 MSXML)并使用特定节点。或者甚至使用适用于 closed 文档并编辑 Word Open XML 的软件包。但这是使用直接 Word VBA.

的最简单方法

我采纳了 Cindy Meister 的建议并清除了他报告的单词混淆。所有修订和评论的作者、创建者和上次修改时间都将根据输入的名称进行更改。

Sub ChangeCommentAndRevisionAuthor()
    Dim j, jmax As Long
    Dim Author(99) As String
    Dim WXML, NewAuthor, FindAuthor, ReplaceAuthor As String
    Dim BNew, StatusTrackRevision As Boolean
    Dim Rev As Revision
    Dim Cmt As Comment
    
    ' set some variables and put trackrevision to false
    StatusTrackRevision = ActiveDocument.TrackRevisions
    ActiveDocument.TrackRevisions = False
    jmax = -1
    ' checks and input new author name
    If ActiveDocument.Range.Revisions.count = 0 Then
        MsgBox "No revisions in your document!", vbInformation, "Change comment and revision author"
        Exit Sub
    End If
    NewAuthor = InputBox("New author name?", "Change comment and revision author")
    If NewAuthor = "" Then
        MsgBox "The author name can’t be empty.", vbInformation, "Change comment and revision author"
        Exit Sub
    End If
    ' Loop through all revisions and get all authors of revisions (maximum 100)
    With ActiveDocument.Range
        If .Revisions.count > 1000 Then
            If MsgBox("The number of revisions it large. " & .Revisions.count & vbCr & " Do you want to continue?", vbOKCancel + vbQuestion + vbDefaultButton2, "Change comment and revision author") > 1 Then Exit Sub
        End If
        For Each Rev In .Revisions
            BNew = True
            For j = 0 To jmax
                If Author(j) = Rev.Author Then
                    BNew = False
                    Exit For
                End If
            Next
            If BNew Then
                jmax = jmax + 1
                If jmax > UBound(Author) Then jmax = UBound(Author)
                Author(jmax) = Rev.Author
            End If
        Next
        ' change all comments
        For Each Cmt In ActiveDocument.Comments
            Cmt.Author = NewAuthor
            Cmt.Initial = NewAuthor
        Next
    End With
    ' read XML and change all authors to the new author
    WXML = ActiveDocument.Content.WordOpenXML
    For j = 0 To jmax
        WXML = Replace(WXML, "w:author=" & Chr(34) & Author(j) & Chr(34), "w:author=" & Chr(34) & NewAuthor & Chr(34))
    Next
    ' change "last modified by" to new author
    WXML = Replace(WXML, "<cp:lastModifiedBy>" & ActiveDocument.BuiltInDocumentProperties(7) & "</cp:lastModifiedBy>", "<cp:lastModifiedBy>" & NewAuthor & "</cp:lastModifiedBy>")
    ' save modified XML
    ActiveDocument.Content.InsertXML WXML
    ' Change Creator of Document to new author
    ActiveDocument.BuiltInDocumentProperties(3) = NewAuthor
    ' restore original status of track revision
    ActiveDocument.TrackRevisions = StatusTrackRevision
    
End Sub