有没有一种方法可以更改Word文档中修订的作者?

3

我想知道是否有一种方法可以更改文档修订版本的作者,我已经找到了更改评论作者的方法,但没有找到修订部分的方法。我已经尝试在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

我是否在正确的方向上,或者说没有改变的可能性?

2个回答

3
我采纳了Cindy Meister的建议并解决了他所报告的Word混淆问题。所有修订和注释的作者、创建者和最后修改者都将根据输入的名称更改。
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

3
修订会自动使用UI中所选的用户名。关于“修订”的“作者”属性,语言参考说明如下:
“返回指定跟踪更改的用户姓名。只读字符串。”
因此,在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的最简单的方法。

非常感谢您详细的回答! :) 我可以根据我的需求调整您的代码,并且它按照我想要的方式工作。 - undefined
@Satanas,你能分享一下你的方法吗?最好的祝福。 - undefined

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接