删除Word文件的所有注释

问题描述 投票:1回答:1

所以我试图删除Word文件中的所有注释,每个Excel文件的VBA代码。

我试过使用

'Dim ObjWord as Word.Application
ObjWord.ActiveDocument.DeleteAllComments

并称

Sub RemoveAllComments(Doc As Document)
    Dim n As Long
    Dim oComments As Comments
    Set oComments = Doc.Comments
    For n = oComments.Count To 1 Step -1
    oComments(n).Delete
    Next
    Set oComments = Nothing
End Sub

但第一个给了我运行时'错误4605命令不可用',第二个代码段抛出'错误438对象不支持此属性或方法'。

我还有其他办法吗?

编辑1:

Dim ObjWord As Word.Application
Set ObjWord = LoadWord()
ObjWord.Visible = True

是在Function中调用LoadWord()。

Function LoadWord() As Word.Application 
    Set LoadWord = GetObject(, "Word.Application")
    If MsgBox("Word's already in use klick ok to dismiss all changes", vbOKCancel) = vbCancel Then
        Set LoadWord = Nothing
    End If
    Exit Function
End Function
excel vba ms-word comments
1个回答
0
投票

错误4605与。

ObjWord.ActiveDocument.DeleteAllComments

表示ActiveDocument(如果有的话)不包含注释。由于你没有发布任何与ActiveDocument(或Doc,在你的第二个子中)如何实例化相关的代码,或者你是否已经确定相关文档甚至包含任何注释,所以无法在这方面给出进一步的建议。

此外,这个。

Sub RemoveAllComments(Doc As Document)

将是无效的。至少需要这样做。

Sub RemoveAllComments(Doc As ObjWord.Document)

对于第一个例子,这种情况可以通过以下方法来补救:

On Error Resume Next
ObjWord.ActiveDocument.DeleteAllComments
On Error Goto 0
© www.soinside.com 2019 - 2024. All rights reserved.