输入不匹配字

时间:2018-05-21 07:43:24

标签: ms-word word-vba

Private Sub RunSpellCheckOnSelection()

    MsgBox "Running check..."

    Dim wbDictionaryCollection As Excel.Workbook
    Dim wsDictionary           As Excel.Worksheet
    Dim rngWords               As Excel.Range

    Set wbDictionaryCollection = appExcel.Workbooks.Open(Application.ActiveDocument.Path & "\Dictionary.xlsx")
    Set wsDictionary = wbDictionaryCollection.Worksheets(strLanguage)
    Set rngWords = wsDictionary.Range("A1").CurrentRegion

    Dim PUNCTUATION As String
    PUNCTUATION = ".,:;'!?/\@#$%^&*(){}[]-_=+|<>`~" & Chr(13)

    Dim blnWordFound As Boolean

    Dim nDictionaryWords As Long
    Dim iDictionaryWord As String

    Dim nWords As Long
    Dim iWord  As Long

    Dim nPunctuation As Long
    Dim iPunctuation As Long

    nDictionaryWords = rngWords.Rows.Count
    nWords = Selection.words.Count
    nPunctuation = Len(PUNCTUATION)

    ' For every word in selection:
    For iWord = 1 To nWords

            ' Check if the word is in the dictionary:
            blnWordFound = False

            For iDictionaryWord = 1 To nDictionaryWords
                    ''''''''
                    If Selection.words(iWord).Text = Mid$(rngWords.Cells(iDictionaryWord, 1), 1, Len(rngWords.Cells(iDictionaryWord, 1) - 2)) Then

此行为我提供了类型不匹配错误,我不会意识到这里的错误..

我编辑并添加了剩下的代码!我在Mid函数

上获得了一个类型错误错误

1 个答案:

答案 0 :(得分:0)

在该语句中有两个单独的语法错误,并且每个语法错误都会重复。

首先,Range对象的Cells属性返回Cell对象,而不是字符串。这导致了类型不匹配错误。要纠正它,请在两个地方使用rngWords.Cells(...)。Range.Text。

第一个错误是隐藏第二个错误:范围的Cells属性采用单个数字Long作为其参数,但是您提供了两个数字。如果单击Debug&gt;编译项目时,它将突出显示单词Cells并解释“编译错误:错误的参数数量或无效的属性赋值”。我不知道你对“,1”的期望是什么,但它不应该存在。