无法弄清楚对象所需的错误

时间:2016-01-27 13:25:36

标签: vba excel-vba excel

代码的目的是将电子邮件发送到地址列表。要确定起点,将出现一个用户表单,询问说明列和行(计划随着我的改进,我将添加其他选项)。

我有代码工作,但是,我做了一些调整,并没有继续得到一个对象所需的错误,我已经尝试搞清楚几个小时没有运气。请问你看看我的代码并建议我可能出错的地方?

NB。我也尝试声明所有变量,但它没有解决问题。

错误发生在循环上,直到username.Value =“”

Sub cmdGo_Click()

Application.DisplayAlerts = False

i = cmbRow

If i = "" Then
    Exit Sub
End If

username = cmbColumn

If username = "" Then
    Exit Sub
End If

Select Case username

    Case "A", "a"
        username = Cells(i, "a").Value
    Case "B", "b"
        username = Cells(i, "b").Value
    Case "C", "c"
        username = Cells(i, "c").Value
    Case "D", "d"
        username = Cells(i, "d").Value
    Case "E", "e"
        username = Cells(i, "e").Value

End Select

Do

Dim OutApp As Object
    Dim OutMail As Object
    Set OutApp = CreateObject("Outlook.Application")
    Set OutMail = OutApp.CreateItem(0)
    On Error Resume Next
    With OutMail

        .To = username
        .CC = ""
        .BCC = ""
        .Importance = 1
        .Subject = "Hello"
        .HTMLBody = "Message"

'display shows each email before sending
          .Display
'send sends email automatically
'         .Send

    End With
    On Error GoTo 0
    Set OutMail = Nothing
    Set OutApp = Nothing

Application.DisplayAlerts = True

i = i + 1

Loop Until username.Value = ""

End Sub

1 个答案:

答案 0 :(得分:3)

代码中的语法和理解错误很少。我在下面列出了它们并重新编写了一些代码,以帮助它按照您的意愿运行。

  1. 最好在每个模块的顶部使用Option Explicit明确声明所需类型的变量
  2. 将用户名的设置放在循环中,以便每次使用基于i = i+1计数器的新电子邮件重置。
  3. 其余评论在代码中:

    Option Explicit
    
    Sub cmdGo_Click()
    
    Application.DisplayAlerts = False
    
    If cmbRow = "" or cmbColumn = "" Then 
        Exit Sub
    End If
    
    Dim i As Long
    i = cmbRow
    
    Dim UserNameCol As String 'created a new variable just to get column letter so can be used later in the loop and removed the `Select Case` block.
    UserNameCol = cmbColumn
    
    'set outlook outside loop since you only need to call it once, doing it in loop creates unneccesary processing
    Dim OutApp As Object
    Dim OutMail As Object
    Set OutApp = CreateObject("Outlook.Application")
    
    Do Until Len(Cells(i, UserNameCol).Value) = 0 'will stop when blank cell appears
    
        Dim UserName As String
        UserName = Cells(i, UserNameCol).Value 'always will user whatever column choosen
    
        Set OutMail = OutApp.CreateItem(0) 'this goes here because a new email is needed each time
        On Error Resume Next
        With OutMail
    
            .To = UserName
            '.CC = "" 'you can remove this lines because you are not putting anything in the field
            '.BCC = "" 'you can remove this lines because you are not putting anything in the field
            .Importance = 1
            .Subject = "Hello"
            .HTMLBody = "Message"
    
            'display shows each email before sending
            .Display
            'send sends email automatically
            '.Send
    
        End With
        On Error GoTo 0
    
        i = i + 1
    
    Loop 
    
    'destroy outlook when finished processing all mails
    Set OutMail = Nothing
    Set OutApp = Nothing
    
    Application.DisplayAlerts = True
    
    End Sub