簡體   English   中英

VBA不一致的運行時錯誤“ 91”

[英]VBA Inconsistent Run-time error '91'

就像前言一樣,我不是一個程序員。 只是試圖為一個宏使用一次創可貼。

當前需要在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

您在錯誤行上的語法似乎正確。 您可以:
-通過插入debug.print行以顯示文件名表達式來驗證文件名
-手動打開Word,刪除代碼中的On error ,然后再次運行,以驗證Word實例。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM