对于电子邮件宏上没有下一个错误

Posted

技术标签:

【中文标题】对于电子邮件宏上没有下一个错误【英文标题】:For without Next Error on Email Macro 【发布时间】:2013-11-06 18:39:22 【问题描述】:

您好,我这里的代码返回一个错误“For without Next”,它在底部一直突出显示结束子。

为什么会这样?我只是想在我的电子邮件正文中插入某些单元格的值。

有人可以帮忙吗?

Sub Notify()
    Dim name, email, evnt, status As Range
    Dim SigString As String
    Dim Signature As String

        SigString = Environ("appdata") & _
                "\Microsoft\Signatures\Will.txt"

    If Dir(SigString) <> "" Then
        Signature = GetBoiler(SigString)
    Else
        Signature = ""
    End If

    For Each name In Range(("B2"), Range("B2").End(xlDown))
    For Each email In Range(("C2"), Range("C2").End(xlDown))
    For Each evnt In Range(("E2"), Range("E2").End(xlDown))
    For Each status In Range(("F2"), Range("F2").End(xlDown))

    On Error Resume Next

        With CreateObject("Outlook.Application").CreateItem(0)
            .To = email.Value
            .CC = email.Offset(0, 1).Value
            .Subject = "Information You Requested"
            .Body = "Dear " & name.Value & "," & vbNewLine & vbNewLine & _
                "As you may know the 2014 race started on November 4th and will be open until November 15th in order for you to select your candidate." & vbNewLine & vbNewLine & _
                "Your 2014 election is now On Hold since you have a previous " & evnt.Value & "event with a " & status.Value & "status in Workday. " & vbNewLine & vbNewLine & _
                "In order for you to be able to finalize your Elections you need to finalize the event above as soon as possible." & vbNewLine & vbNewLine & _
                "If you have any questions about this task please call us at 1-877-777-7777 option 8" & vbNewLine & vbNewLine & _
                "Regards" & vbNewLine & vbNewLine & _
                "Department"
            .Display
            '.Attachments.Add "C:\test.txt"
            '.Send
        End With
    Next
End Sub

Function GetBoiler(ByVal sFile As String) As String
    'Dick Kusleika
    Dim fso As Object
    Dim ts As Object
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set ts = fso.GetFile(sFile).OpenAsTextStream(1, -2)
    GetBoiler = ts.readall
    ts.Close
End Function

【问题讨论】:

使用 this 进行代码缩进 - 它会显示级别并使匹配 if/endif 和其他类似结构更容易。它适用于 Office 2007 和 2010,但网站没有显示这些 【参考方案1】:

您有 4 个For Each,但只有一个Next

您需要另外 3 个Next。看这个。

Sub Notify()
    '
    '~~> Rest of the code
    '
    For Each Name In Range(("B2"), Range("B2").End(xlDown))
        For Each Email In Range(("C2"), Range("C2").End(xlDown))
            For Each evnt In Range(("E2"), Range("E2").End(xlDown))
                For Each Status In Range(("F2"), Range("F2").End(xlDown))
                    '
                    '~~> Rest of the code
                    '
                Next
            Next
        Next
    Next
End Sub

【讨论】:

嘿,谢谢!!!现在我没有收到错误,但宏只是不断循环遍历名称并且它不会停止...... 它会停止 :) 只是你循环了很多次。我也不鼓励您使用xlDown 来查找lastrow。你可能想看看THIS 我会在开头插入该代码吗?我的射程是多少? Range(("B2"), Range("B2").End(xlDown)) 会变成类似Range("B2:B" &amp; Lrow) 如果您有大量行,那么您可能要考虑使用数组? 抱歉延迟回复,但我仍然无法正常工作。我将不得不继续尝试调整代码,直到我让它工作......

以上是关于对于电子邮件宏上没有下一个错误的主要内容,如果未能解决你的问题,请参考以下文章

在自动填充空白值的宏上获取运行时错误 6 [重复]

没有 PayPal 帐户的电子邮件的 PayPal 错误

宏上的编译错误以拆分和保存工作表

处理 Parse 中的某些错误

从服务传递到组件的角度错误消息

在 Drupal 中,如何在没有管理员批准和用户设置密码的情况下激活电子邮件帐户?