【问题标题】:There is a way to change the author of revisions in a Word document?有没有办法在 Word 文档中更改修订的作者?
【发布时间】:2019-09-10 09:13:42
【问题描述】:

我想知道是否有办法更改文档中修订的作者,我找到了更改 cmets 的作者的方法,但对于修订部分没有。我试图在 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

我走对了,还是没有办法改变这一点?

【问题讨论】:

    标签: vba ms-word ms-office revision author


    【解决方案1】:

    修订版会自动使用 UI 中选定的用户名。 RevisionsAuthor 属性的语言参考说明:

    返回进行指定跟踪更改的用户的名称。只读字符串。

    所以没有直接的方法使用 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
    

    请注意,这也可能会更改 cmets 的作者姓名。更“优雅”的方法是利用 XML 解析器(例如 MSXML)并使用特定节点。或者甚至使用适用于 closed 文档并编辑 Word Open XML 的包。但这是使用直接 Word VBA 的最简单方法。

    【讨论】:

    • 非常感谢您的详尽回答! :) 我可以根据我的需要和工作调整你的代码。
    【解决方案2】:

    我接受了 Cindy Meister 的建议并清除了他报告的 Word 混乱。所有修订和评论作者、创建者和 lastmodied 都将更改为输入的名称。

    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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-07-14
      • 2010-10-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多