编写VBA宏,检查拼写错误的单词前后是否有其他单词,并给出拼写建议
创始人
2024-12-05 19:00:26
0
  1. 首先,需要使用VBA中的拼写检查功能,确定错误的单词是否存在拼写错误。可以使用以下代码来检查单元格中的拼写错误:
Function CorrectSpelling(ByVal str As String) As String
    On Error GoTo SpellCheckError
    Application.ScreenUpdating = False
    If Application.CheckSpelling(str) Then
        CorrectSpelling = str
        Exit Function
    End If
SpellCheckError:
    Dim Suggestions As Variant
    Suggestions = Application.GetSpellingSuggestions(str)
    If Not IsEmpty(Suggestions) Then
        CorrectSpelling = Suggestions(1)
    Else
        CorrectSpelling = str
    End If
    Application.ScreenUpdating = True
End Function
  1. 接下来,需要遍历文档中的所有单词,检查每个单词及其前后位置的单词是否存在拼写错误。可以使用以下代码来遍历文档中的单词:
Sub CheckSpellingBeforeAndAfter()
    Dim wdApp As Word.Application
    Dim wdDoc As Word.Document
    Dim wdRange As Word.Range
    Dim wdWords As Word.Words
    Dim wdWord As Word.Range
    Dim wdPrevWord As Word.Range
    Dim wdNextWord As Word.Range
    
    Set wdApp = CreateObject("Word.Application")
    wdApp.Visible = True
    Set wdDoc = wdApp.ActiveDocument
    Set wdRange = wdDoc.Content
    
    Set wdWords = wdRange.Words
    For Each wdWord In wdWords
        If Not wdWord.SpellingErrors.Count = 0 Then
            Set wdPrevWord = wdWord.End - 1
            Set wdNextWord = wdWord.End + 1
            If Not wdPrevWord.Characters.First = wdRange.Characters.First Then
                If Not wdPrevWord.SpellingErrors.Count = 0 And Not wdPrevWord.InRange(wdWord) Then
                    MsgBox "Misspelled word found: " & wdPrevWord.Text & " " & wdWord.Text & " " & wdNextWord.Text & " Suggested correction: " & CorrectSpelling(wdPrevWord.Text & " " & wdWord.Text)
                End If
            End If
            If Not wdNextWord.Characters.Last = wdRange.Characters.Last Then
                If Not wdNextWord.SpellingErrors.Count = 0 And Not wdNextWord.InRange(wdWord) Then
                    MsgBox "Misspelled word found

相关内容

热门资讯

wepower有外 挂!wep... wepower有外 挂!wepoke真的有挂嘛,wepoke辅助插件,我来教教你(有挂解说);人气非...
微扑克系统发牌规律!微扑克辅助... 自定义微扑克系统规律,只需要输入自己想要的开挂功能,一键便可以生成出微扑克专用辅助器,不管你是想分享...
必备科技!开心联盟棋牌俱乐部怎... 必备科技!开心联盟棋牌俱乐部怎么才能赢(辅助)好像是真的有挂(2022已更新)(哔哩哔哩)1、必备科...
wepoke插件!wepoke... wepoke插件!wepoke德州扑克系统规律,wepoke有挂,力荐教程(有挂细节)1、不需要AI...
微扑克德州专用辅助器!微扑克辅... 微扑克德州专用辅助器!微扑克辅助软件查得出来,微扑克有挂,细节方法(有挂方法)1、许多玩家不知道微扑...
最新技巧!衢州都莱辅助器是真是... 最新技巧!衢州都莱辅助器是真是假(透明挂)一贯存在有挂(2024已更新)(哔哩哔哩)1、衢州都莱辅助...
wepokeai代打!wepo... wepokeai代打!wepoke挂真的,wEpOke果真真的有挂,曝光教程(有挂攻略);wpk透视...
微扑克辅助挂!微扑克被系统制裁... 微扑克辅助挂!微扑克被系统制裁,WPK确实是有挂,教你攻略(有挂透视),您好,微扑克这款游戏可以开挂...
教程攻略!经典联盟有外 挂吗(... 教程攻略!经典联盟有外 挂吗(透明挂)确实是有挂(2026已更新)(哔哩哔哩);在进入经典联盟有外 ...
wepoke ai辅助!wep... wepoke ai辅助!wepoke软件机器人(透明挂)wepoker(的确真的有挂)是一款可以让一...