运行-Time Error 91 对象变量或未设置块变量

Run-Time Error 91 Object variable or With block variable not set

我收到“运行-Time Error 91 Object variable or With block variable not set”错误。该代码工作了一两次,之后就停止工作了。

任何人都可以帮助解决我做错的事情吗?

我在下面指示的行中收到以下代码的错误:

Private Sub CommandButton1_Click()
    Dim Sht As Excel.Worksheet
    Set Sht = ThisWorkbook.ActiveSheet
 
    Recip = Worksheets("STIF Report").AutoFilter.Range.Offset(1).SpecialCells(xlCellTypeVisible).Cells(1, 6) ----!
   
    Custody = Worksheets("STIF Report").AutoFilter.Range.Offset(1).SpecialCells(xlCellTypeVisible).Cells(1, 5) ----!
   
    Dim rng As Range
    Set rng = Sht.Range("B43:D85")
        rng.Copy
   
    Dim OutApp As Object
    Set OutApp = CreateObject("Outlook.Application")
 
    Dim OutMail As Object
    Set OutMail = OutApp.CreateItem(0)
 
    Dim vInspector As Object
    Set vInspector = OutMail.GetInspector
   
    Dim wEditor As Object
    Set wEditor = vInspector.WordEditor
    
    With OutMail
        .TO = Recip
        .CC = ""
        .Subject = "STIF Vehicle Confirmation" & " - " & Custody ----!
        .display
        
         wEditor.Paragraphs(1).Range.Text = "Hello All," & Chr(11) & Chr(11) & "I hope this email finds you all doing well." & Chr(11) & Chr(11) & _
         "Can you please confirm if the below STIF vehicle details are accurate for the accounts below? If the vehicle has changed, can you please confirm the new STIF vehicle name and CUSIP?" & vbCrLf
        
         wEditor.Paragraphs(2).Range.Paste
    End With
 
    Set OutMail = Nothing
    Set OutApp = Nothing
End Sub

我遇到的问题似乎是因为同一个 sheet 上有多个 table,宏很难理解我指的是哪个 table。 sheet 上有两个 table(一个在另一个之上),我需要在过滤完我想要显示的信息后将第二个 table 粘贴到电子邮件正文中。为此,我必须命名 table;在这种情况下,'Email,' 并使用 .ListObjects 属性 调用它,在这种情况下是 .ListObjects("Email") 以便宏了解我希望它关注哪个 table上。

您可以在下面看到更新后的代码:

Private Sub CommandButton1_Click()
    Dim Sht As Excel.Worksheet
    Set Sht = ThisWorkbook.ActiveSheet
    Recip = Worksheets("STIF Report").ListObjects("Email").AutoFilter.Range.Offset(1).SpecialCells(xlCellTypeVisible).Cells(1, 6)
  
    Custody = Worksheets("STIF Report").ListObjects("Email").AutoFilter.Range.Offset(2).SpecialCells(xlCellTypeVisible).Cells(1, 5)
  
    Dim rng As Range
    Set rng = Sht.Range("B43:D85")
        rng.Copy
  
    Dim OutApp As Object
    Set OutApp = CreateObject("Outlook.Application")
    Dim OutMail As Object
    Set OutMail = OutApp.CreateItem(0)
    Dim vInspector As Object
    Set vInspector = OutMail.GetInspector
  
    Dim wEditor As Object
    Set wEditor = vInspector.WordEditor
   
    With OutMail
        .TO = Recip
        .CC = ""
        .Subject = "STIF Vehicle Confirmation" & " - " & Custody
        .display
       
         wEditor.Paragraphs(1).Range.Text = "Hello All," & Chr(11) & Chr(11) & "I hope this email finds you all doing well." & Chr(11) & Chr(11) & _
         "Can you please confirm if the below STIF vehicle details are accurate for the accounts below? If the vehicle has changed, can you please confirm the new STIF vehicle name and CUSIP?" & vbCrLf
       
         wEditor.Paragraphs(2).Range.Paste
    End With
End Sub