2015-12-02 36 views
0

我得到一個錯誤的下面的代碼,每次我嘗試運行它與約2500萬words.it數據集說:「運行時錯誤'91':對象變量或隨着塊變量沒有設置「任何人都可以請協助。該計劃的目標是找到一個開始和結束,並刪除中間的一切。運行時錯誤與我的代碼VBA Word

Sub SomeSub() 
    Application.ScreenUpdating = False 
    Application.DisplayAlerts = False 

    Dim StartWord As String, EndWord As String 
    Dim Find1stRange As Range, FindEndRange As Range 
    Dim DelRange As Range, DelStartRange As Range, DelEndRange As Range 

    'Setting up the Ranges 
    Set Find1stRange = ActiveDocument.Range 
    Set FindEndRange = ActiveDocument.Range 
    Set DelRange = ActiveDocument.Range 

    'Set your Start and End Find words here to cleanup the script 
    StartWord = "From: [email protected]" 
    EndWord = "for malware by Websense. www.websense.com " 

    'Starting the Find First Word 
    With Find1stRange.Find 
     .Text = StartWord 
     .Replacement.Text = "" 
     .Forward = True 
     .Wrap = wdFindAsk 
     .Format = False 
     .MatchCase = False 
     .MatchWholeWord = False 
     .MatchWildcards = False 
     .MatchSoundsLike = False 
     .MatchAllWordForms = False 
    Application.ScreenUpdating = False 
     'Execute the Find 
     Do While .Execute 
      'If Found then do extra script 
      If .Found = True Then 
       'Setting the Found range to the DelStartRange 
       Set DelStartRange = Find1stRange 
       'Having these Selections during testing is benificial to test your script 
       DelStartRange.Select 

       'Setting the FindEndRange up for the remainder of the document form the end of the StartWord 
       FindEndRange.Start = DelStartRange.End 
       FindEndRange.End = ActiveDocument.Content.End 

       'Having these Selections during testing is benificial to test your script 
       FindEndRange.Select 

    Application.ScreenUpdating = False 
       'Setting the Find to look for the End Word 
       With FindEndRange.Find 
        .Text = EndWord 
        .Execute 

        'If Found then do extra script 
        If .Found = True Then 
         'Setting the Found range to the DelEndRange 
         Set DelEndRange = FindEndRange 

         'Having these Selections during testing is benificial to test your script 
         DelEndRange.Select 

        End If 

       End With 

       'Selecting the delete range 
       DelRange.Start = DelStartRange.Start 
       DelRange.End = DelEndRange.End 
       'Having these Selections during testing is benificial to test your script 
       DelRange.Select 

       'Remove comment to actually delete 
       DelRange.Delete 



      End If  'Ending the If Find1stRange .Found = True 
     Loop  'Ending the Do While .Execute Loop 
    End With 'Ending the Find1stRange.Find With Statement 

    End Sub 
+0

我錯誤發生了什麼? –

+0

你好,抱歉,遲到的回覆。但是我在DelRange.Select中獲得它 – jaydub

回答

1

您得到運行時錯誤可能是因爲找不到DelEndRange

那麼在我的測試中,這是我發現的問題。

因此請檢查EndWord文本是否在文檔中。