【问题标题】:VBA Inconsistent Run-time error '91'VBA 不一致的运行时错误'91'
【发布时间】:2017-11-21 14:05:57
【问题描述】:

就像前言一样,我不是一个程序员。只是想为一次性使用创可贴的宏。

目前需要在 excel 中循环约 3700 行,并在约 100 个单词的文档中找到某些单词。

基本上第 1 行需要找到单词“驴”(单元格 A1)并搜索单元格 A4 中列出的文档,然后生成该单词被找到的次数,如果大于 2 次,则将单元格标记为“是”。

我的问题是,有时它会运行完成,有时在打开下一个文档时我收到错误

运行时错误“91”:对象变量或未设置块变量

通常重新启动 excel 或更改正在搜索的文档的文件路径会修复它一两次运行。这让我相信它与记忆有关,但我不确定。

任何想法是什么问题?谢谢!

这是代码原样,是的,它很草率。

Sub FindName()
    Dim wrdApp As Object
    Dim wrdDoc As Object
    Dim maxRowCount As Integer
    Dim TP As String
    Dim FindWord As String
    Dim result As String
    Dim RowCount As Integer
    Dim i As Long
    Dim iCount As Integer

    TP = "003"

        i = 115
        maxRowCount = 140

     On Error Resume Next
            Set wrdApp = GetObject(, "Word.Application")
            If Err.Number > 0 Then Set wrdApp = CreateObject("Word.Application")
            On Error GoTo 0
            wrdApp.Visible = True

    Set wrdDoc = wrdApp.Documents.Open("C:\TP\X-" & TP & ".docx")

    For i = i To maxRowCount



      If Cells(i, 4).Text = TP Then

         FindWord = Cells(i, 1).Text

         '// Defines selection for Word's find function
         wrdDoc.ActiveWindow.Selection.GoTo What:=wdGoToSection, Which:=wdGoToFirst
         wrdDoc.SelectAllEditableRanges
         iCount = 0

         '// Word Find Method Setup Block
         With wrdDoc.ActiveWindow.Selection.Find
             .Text = FindWord
             .Replacement.Text = ""
             .Forward = True
             .Wrap = 1 ' wdFindContinue (Word constant not defined in Excel)
             .Format = False
             .MatchCase = True
             .MatchWholeWord = True
             .MatchWildcards = False
             .MatchSoundsLike = False
             .MatchAllWordForms = False
            Do While wrdDoc.ActiveWindow.Selection.Find.Execute
                iCount = iCount + 1
                wrdDoc.ActiveWindow.Selection.MoveRight
               ' MsgBox iCount
            Loop
         End With


         '// Unnecessary storing, I know
         result = iCount
         Cells(i, 6).Value = result
         If result > 1 Then
                Cells(i, 7).Value = "YES"
         Else
                Cells(i, 7).Value = "NO"
         End If



      Else

      TP = Cells(i, 4).Text
      FindWord = Cells(i, 1).Value
        '// Close and don't save application
        wrdApp.Quit SaveChanges:=0 ' wdDoNotSaveChanges (Word constant not defined in Excel)

        Set wrdApp = Nothing
        Set wrdDoc = Nothing

            On Error Resume Next
            Set wrdApp = GetObject(, "Word.Application")
            If Err.Number > 0 Then Set wrdApp = CreateObject("Word.Application")
            On Error GoTo 0


        Set wrdDoc = wrdApp.Documents.Open("C:\TP\X-" & TP & ".docx")



        '// Defines selection for Word's find function

         wrdDoc.ActiveWindow.Selection.GoTo What:=wdGoToSection, Which:=wdGoToFirst
         wrdDoc.SelectAllEditableRanges
         iCount = 0
         '// Word Find Method Setup Block
         With wrdDoc.ActiveWindow.Selection.Find
             .Text = FindWord
             .Replacement.Text = ""
             .Forward = True
             .Wrap = 1 ' wdFindContinue (Word constant not defined in Excel)
             .Format = False
             .MatchCase = True
             .MatchWholeWord = True
             .MatchWildcards = False
             .MatchSoundsLike = False
             .MatchAllWordForms = False
            Do While wrdDoc.ActiveWindow.Selection.Find.Execute
                iCount = iCount + 1
                wrdDoc.ActiveWindow.Selection.MoveRight
            Loop
         End With


         '// Unnecessary storing, I know
         result = iCount
         Cells(i, 6).Value = result
         If result > 1 Then
                Cells(i, 7).Value = "YES"
         Else
                Cells(i, 7).Value = "NO"
         End If
      End If
    Next i


End Sub

【问题讨论】:

  • 你的错误发生在哪一行?
  • 在 Else 部分设置 wrdDoc = wrdApp.Documents.Open("C:\TP\X-" & TP & ".docx")。我已经让它打印出它正在打开的文档和目录/文档名称是完美的。
  • 你的语法没问题。尝试在故障行之前插入:debug.print "C:\TP\X-" & TP & ".docx" 并使用ctrl+G查看结果
  • @PatrickHonorez 检查调试时文档路径和名称正确。
  • @BenWest 然后尝试手动打开 Word,然后再次运行并删除所有 on error

标签: vba excel


【解决方案1】:

错误行中的语法似乎正确。您可以:
- 通过插入debug.print 行来验证文件名以显示文件名表达式
- 通过手动打开 Word、删除代码中的 On error 并再次运行来验证 Word 实例化。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多