简体   繁体   English

删除 Microsoft Word 文档中所有拼写错误的单词

[英]Delete all misspelled words in Microsoft Word document

I have numerous word documents with misspelt words that I'm hoping to batch delete.我有许多单词拼写错误的word文档,希望批量删除。 I've tried both of the solutions mentioned below, but they all seem to fail for me.我已经尝试了下面提到的两种解决方案,但它们似乎对我来说都失败了。

https://answers.microsoft.com/en-us/msoffice/forum/all/how-to-remove-all-misspelled-words-in-ms-word-at/608dbb5d-e719-4b5f-b44e-1b0542b66bd7 https://answers.microsoft.com/en-us/msoffice/forum/all/how-to-remove-all-misspelled-words-in-ms-word-at/608dbb5d-e719-4b5f-b44e-1b0542b66bd7

Sub DeleteSpellingErrors()
Dim rng As word.Range, i As Integer
If Selection.Range.Start = Selection.Range.End Then
    Set rng = ActiveDocument.Content
Else
    Set rng = Selection.Range
End If
If rng.SpellingErrors.Count > 0 Then
    For i = rng.SpellingErrors.Count To 1 Step -1
        rng.SpellingErrors(i).Delete
    Next
End If
End Sub

https://answers.microsoft.com/en-us/msoffice/forum/all/remove-all-misspelled-words-in-my-word-document/b686c318-c1fc-4d90-9e56-e922bb556abd https://answers.microsoft.com/en-us/msoffice/forum/all/remove-all-misspelled-words-in-my-word-document/b686c318-c1fc-4d90-9e56-e922bb556abd

Using these macro codes causes my microsoft word to freeze (I'm using a 10th gen intel i7) indefinitely.使用这些宏代码会导致我的 microsoft word 无限期冻结(我使用的是第 10 代 intel i7)。 Despite having waited for hours, there still hasn't been any progress.尽管已经等待了几个小时,但仍然没有任何进展。 It seems to me like these codes only work for shorter documents, but because my word docs have more than 200 pages, it seems to freeze.在我看来,这些代码只适用于较短的文档,但因为我的 word 文档有 200 多页,所以它似乎冻结了。 Does anyone have any other code suggestions?有没有人有任何其他代码建议? Better yet, does anyone have any suggestions that allow me to batch delete misspelt words across multiple word docs?更好的是,有没有人有任何建议可以让我在多个单词文档中批量删除拼写错误的单词? Currently, I am deleting misspelt words one document at a time.目前,我一次删除一个文档中拼写错误的单词。 Thanks for any help!谢谢你的帮助!

Your code runs fine on my PC with a document that has 350 spelling errors.您的代码在我的 PC 上运行良好,文档包含 350 个拼写错误。

If you have a 200+ page document it would be better to disable screen updating whilst your macro runs.如果您有超过 200 页的文档,最好在宏运行时禁用屏幕更新。 I would also add a 'doevents' statement to the for loop so that at least the CTRL Break will halt the program.我还将在 for 循环中添加一个“doevents”语句,以便至少 CTRL Break 将停止程序。 Initially you may also want to debug.print the count of errors to see how the macro is progressing.最初,您可能还想 debug.print 错误计数以查看宏的进展情况。

Option Explicit

Sub DeleteSpellingErrors()

    Dim rng As Word.Range
    Dim i As Long
    
    If Selection.Range.Start = Selection.Range.End Then
    
        Set rng = ActiveDocument.StoryRanges(wdMainTextStory)
        
    Else
    
        Set rng = Selection.Range
        
    End If
    
    If rng.SpellingErrors.Count > 0 Then
    
        Application.ScreenUpdating = False
        Debug.Print "Total errors = ", rng.SpellingErrors.Count ' for debugging only
        
        For i = rng.SpellingErrors.Count To 1 Step -1
    
            DoEvents
            rng.SpellingErrors.Item(i).Delete
        
            Debug.Print i, rng.SpellingErrors.Count ' for debug only.  Note Count doesn't change
            
        Next
        
        Application.ScreenUpdating = True
        Application.ScreenRefresh
        
    End If
    
End Sub

Try if this code snippet is a bit faster:试试这个代码片段是否更快一点:

    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

Most probably you will have to re-run the procedure two or three times as I see that SpellingErrors.Count is not exact.很可能您将不得不重新运行该过程两到三次,因为我看到 SpellingErrors.Count 不准确。

This re-run can be avoided with this other coding:使用以下其他编码可以避免重新运行:

Sub DeleteSpellingErrors()
    Dim cnt, i As Long
    Dim cur, Last As Range
    Dim doc As Document
    Set doc = ActiveDocument
    cnt = doc.Range.SpellingErrors.Count
    Set cur = doc.GoTo(What:=wdGoToSpellingError, Which:=wdGoToFirst)
    Set Last = doc.GoTo(What:=wdGoToSpellingError, Which:=wdGoToLast)
    i = 1
    Do While cur <> Last
        cur.Select
        cur.Delete
        Debug.Print cnt & " " & i
        Set cur = doc.GoTo(What:=wdGoToSpellingError, Which:=wdGoToNext)
        DoEvents
        i = i + 1
    Loop
End Sub

For testing purposes the document consisted of 107 pages with more than 3000 spelling errors and it took few minutes (about 3 or 4) of execution.出于测试目的,该文档由 107 页组成,其中包含 3000 多个拼写错误,并且执行需要几分钟(大约 3 或 4 分钟)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM