如何跨多个Word文档执行宏?

How to execute a macros across multiple Word documents?

我有一个宏代码(由 Xavier Junqué 提供,请参阅下面的代码片段 1),它允许我 . I'm hoping to execute this code across all word docs in a single folder. There are hundreds of word documents. Can anyone tell me how? I've tried to use Graham Mayor's (GM) batch process add-in,我在其他论坛上看到有人推荐它,但加载项似乎在代码中需要一个“函数”行(例如 Function MyUserDefinedProcess(oDoc As Document) As Boolean)。我尝试将此功能行添加到我的代码中,替换子行(参见代码片段 2),但它不起作用。有没有人有任何其他用于批量执行宏的宏解决方案,或者可以告诉我我在 GM 加载项上做错了什么?非常感谢!

#Code Snippet 1: the code I'm trying to execute across multiple files...
    Sub DeleteSpellingErrors()
    Dim cnt As Long
    Dim cur As Range
    Dim doc As Document
    Set doc = ActiveDocument
    cnt = doc.Range.SpellingErrors.Count
    Set cur = doc.GoTo(What:=wdGoToSpellingError, Which:=wdGoToFirst)
    For i = 1 To cnt
        cur.Select
        cur.Delete
        Debug.Print cnt & " " & i
        Set cur = doc.GoTo(What:=wdGoToSpellingError, Which:=wdGoToNext)
        DoEvents
    Next
End Sub

#Code Snippet 2: Same as above code but with function line instead of sub...
    Function DeleteSpellingErrors(ByRef oDoc As Word.Document) As Boolean
    Dim cnt As Long
    Dim cur As Range
    Dim doc As Document
    Set doc = ActiveDocument
    cnt = doc.Range.SpellingErrors.Count
    Set cur = doc.GoTo(What:=wdGoToSpellingError, Which:=wdGoToFirst)
    For i = 1 To cnt
        cur.Select
        cur.Delete
        Debug.Print cnt & " " & i
        Set cur = doc.GoTo(What:=wdGoToSpellingError, Which:=wdGoToNext)
        DoEvents
    Next
End Function

2 月 5 日 @Xavier:根据你的建议,这就是我所做的......

  1. 我从“...STARTUP”文件夹中剪切“BatchProcessDocuments Ver 4.7.dotm”并将其粘贴到“...Word”文件夹中。
  2. 我打开一个新的空白 word 文档 > 开发人员 > 宏 > 输入 abc 并单击“创建宏”
  3. Visual Basic 编辑器出现。我可以看到“abc”window,但看不到 DeleteSpellingErrors() 宏。我可以看到这个“abc”宏在模块 2 下,并且已经有一个模块 1。如果我打开模块 1,我会找到如下所示的删除拼写错误宏代码(我之前已经更新了我的代码以匹配您建议的代码,这就是为什么它已经在模块 1)
  4. 下的原因
 Function DeleteSpellingErrors(doc As Document) As Boolean
     Dim cnt, i As Long
     Dim cur, Last As Range
     If doc Is Nothing Then
         Set doc = Application.ActiveDocument
     End If
     Do
         cnt = doc.Range.SpellingErrors.Count
         If cnt <= 0 Then Exit Do
         Set Last = doc.GoTo(What:=wdGoToSpellingError, Which:=wdGoToLast)
         Set cur = doc.GoTo(What:=wdGoToSpellingError, Which:=wdGoToFirst)
         For i = 1 To cnt
             cur.Select
             cur.Delete
             'Debug.Print cnt & " " & i
             Set cur = doc.GoTo(What:=wdGoToSpellingError, Which:=wdGoToNext)
             DoEvents
             i = i + 1
         Next
     Loop
     DeleteSpellingErrors = True
 End Function
  1. 我用上面的代码替换了模块 1 和 2 的内容。我保存两个宏并关闭 VBA 编辑器,关闭 word 文档,然后将“BatchProcessDocuments Ver 4.7.dotm”粘贴回启动文件夹。
  2. 我打开“BatchProcessDocuments Ver 4.7.dotm”文件 > 批处理选项卡 >“处理文件夹”> 用户定义的处理,然后输入“DeleteSpellingErrors”。请参见下面的屏幕截图。 screenshot
  3. 同样,它会立即处理批处理宏并说它已完成,而实际上并未对基础文件执行任何操作。我在此文件夹中有两个 docm 文件用于测试目的。我试过 docx 文件也没有效果。

@Xavier 请参阅下面我在评论中提到的错误消息图片,一如既往地感谢你。 Error image 1 Error image 2

检查此代码(添加例外等):

Private Sub DelErrorsInFiles()
    Dim oDoc As Document
    Dim fileName As String
    
    Const folder As String = "C:\Test\" '"\" at the end is important
    
    fileName = Dir(folder)
    Do While fileName <> ""
        Set oDoc = Documents.Open(fileName:=folder & fileName)
        DeleteSpellingErrors oDoc
        oDoc.Save
        oDoc.Close
        DoEvents
        Set oDoc = Nothing
        fileName = Dir
    Loop
End Sub

Function DeleteSpellingErrors(ByRef oDoc As Word.Document) As Boolean
    Application.ScreenUpdating = False
    Debug.Print oDoc.Name
    Debug.Print oDoc.Range.SpellingErrors.Count
    Dim i As Long
    Do While oDoc.Range.SpellingErrors.Count > 0
        oDoc.Range.SpellingErrors(1).Select
        Debug.Print CStr(i) & " " & Selection.Text
        Selection.Delete
        i = i + 1
        If i Mod 10 = 0 Then
            oDoc.UndoClear
            DoEvents
        End If
    Loop
    Application.ScreenUpdating = True
    Application.ScreenRefresh
End Function

以下代码在 107 页的文档和 3000 多个拼写错误中为我工作。 Graham Mayor add-in.

的一个电话只用了 3 分钟的处理时间就完成了工作
Function DeleteSpellingErrors(doc As Document) As Boolean
    Dim cnt, i As Long
    Dim cur, Last As Range
    Do
        cnt = doc.Range.SpellingErrors.Count
        If cnt <= 0 Then Exit Do
        Set Last = doc.GoTo(What:=wdGoToSpellingError, Which:=wdGoToLast)
        Set cur = doc.GoTo(What:=wdGoToSpellingError, Which:=wdGoToFirst)
        For i = 1 To cnt
            cur.Select
            cur.Delete
            'Debug.Print cnt & " " & i
            Set cur = doc.GoTo(What:=wdGoToSpellingError, Which:=wdGoToNext)
            DoEvents
            i = i + 1
        Next
    Loop
    DeleteSpellingErrors = True
End Function

所以,可以执行批处理宏。

' ********************************************* **

很可能没有宏 DeleteSpellingErrors() 的最后更新。为了正确更新,请执行以下操作:

  1. 转到包含“BatchProcessDocuments Ver 4.7.dotm”的文件夹,这是文件夹:

C:\用户\ ... \AppData\Roaming\Microsoft\Word\STARTUP

  1. 将“BatchProcessDocuments Ver 4.7.dotm”移动到上层文件夹:

C:\用户\ ... \AppData\Roaming\Microsoft\Word

  1. 打开一个空白的 Word 文档

  2. 创建一个名为“abc”的宏

  3. 现在您应该看到宏“abc”和宏 DeleteSpellingErrors()

  4. 用以下内容替换这两个宏

     Function DeleteSpellingErrors(doc As Document) As Boolean
         Dim cnt, i As Long
         Dim cur, Last As Range
         If doc Is Nothing Then
             Set doc = Application.ThisDocument
         End If
         Do
             cnt = doc.Range.SpellingErrors.Count
             If cnt <= 0 Then Exit Do
             Set Last = doc.GoTo(What:=wdGoToSpellingError, Which:=wdGoToLast)
             Set cur = doc.GoTo(What:=wdGoToSpellingError, Which:=wdGoToFirst)
             For i = 1 To cnt
                 cur.Select
                 cur.Delete
                 'Debug.Print cnt & " " & i
                 Set cur = doc.GoTo(What:=wdGoToSpellingError, Which:=wdGoToNext)
                 DoEvents
                 i = i + 1
             Next
         Loop
         DeleteSpellingErrors = True
     End Function
    
  5. 保存宏 DeleteSpellingErrors()

  6. 关闭 VB window.

  7. 关闭单词

  8. 将“BatchProcessDocuments Ver 4.7.dotm”移回其原始文件夹。

  9. 打开“BatchProcessDocuments Ver 4.7.dotm”

  10. 进行批处理:现在它应该可以正常工作了。

我不确定发生了什么。无论如何,我已经制作了一个小型 VB.NET 应用程序,用于 运行 Macros to Word Documents inside a folder。我已尝试尽可能多地进行调试,但很高兴听到您的意见。您可以在我的站点下载页面下载它 here

// **************************************** //

我发布了一个新版本。现在您可以输入宏并将它们与应用程序一起保存在 START 文件夹内的 Word doc1.dotm 文件中。您看不到宏 DeleteSpellingErrors 的原因是因为 GM 将宏保存在受保护的 .dotm 文件中。新版本希望您不保存保护,因此您可以访问 doc1.dotm 中的所有宏。它可以是具有相同扩展名 v.gr 的任何名称。 myMacros.dotm

该应用程序有一个帮助菜单,您可以在其中按照使用说明进行操作。