【问题标题】:Runtime Error, VBA EXCEL for email automation运行时错误,用于电子邮件自动化的 VBA EXCEL
【发布时间】:2019-09-03 03:23:49
【问题描述】:

我一直在使用 vba 为 excel 进行电子邮件自动化,我的代码仅适用于第一封电子邮件,并且在下一封邮件中出现运行时错误,

我尝试将对象设置为无济于事..

我不知道内存泄漏在哪里

有人能指点一下吗

错误出现在对象备注处。我已经提供了错误的屏幕截图..

模块名称 = 自动邮件

Public PublicRow As Integer
Dim mark As New Remarks

Sub Button_Click()

Dim LastR As Long
Dim CRow As Long
Dim sSendTo As String
Dim sSendCC As String
Dim sSendBCC As String
Dim sSubject As String
Dim txt As String
Dim OutApp As Object
Dim OutMail As Object


Set OutApp = CreateObject("Outlook.Application")
Set mark.item = Nothing

OutApp.Session.Logon

LastR = Cells(Rows.Count, 2).End(xlUp).Row

For CRow = 3 To LastR
    If Cells(CRow, 6) <> "Email sent" Then
        If Cells(CRow, 3) <= Date Then

            Set OutMail = OutApp.CreateItem(0)
            Set mark.item = OutMail

            sSendTo = Cells(CRow, 5)
            sSendCC = ""
            sSendBCC = ""
            sSubject = "Project Due Date"
            PublicRow = CRow

            With OutMail
                .To = sSendTo
                If sSendCC > "" Then .CC = sSendCC
                If sSendBCC > "" Then .BCC = sSendBCC
                .Subject = sSubject

                txt = "Dear " & Cells(CRow, 4) & ", "
                txt = txt & vbCrLf & vbCrLf
                txt = txt & "The due date has been reached for the 
 project:"
                txt = txt & vbCrLf & vbCrLf
                txt = txt & "    " & Cells(CRow, 2)
                txt = txt & vbCrLf & vbCrLf
                txt = txt & "Please take the appropriate actions."
                txt = txt & vbCrLf & vbCrLf
                txt = txt & "Regards,"
                txt = txt & vbCrLf
                txt = txt & "Danial"

                .Body = txt
                .Display (True)
            End With

            Set OutMail = Nothing

        End If
    End If
Next CRow

Set mark.item = Nothing
Set OutApp = Nothing

End Sub

类名 = 备注

Option Explicit

Public WithEvents item As Outlook.MailItem

Private Sub item_Close(Cancel As Boolean)

Dim boolSent As Boolean

boolSent = item.Sent

If Err.Number = 0 Then
    Cells(PublicRow, 6) = "Email not sent"
    Cells(PublicRow, 7) = "X"
Else
    Cells(PublicRow, 6) = "Email sent"
    Cells(PublicRow, 7) = Now()
End If

End Sub

错误:

【问题讨论】:

  • Dim mark As New Remarks改成Dim mark As Remarks看看有什么变化。我猜您将 mark 设置为空,但由于您使用 New 关键字,它会立即再次实例化。见这里:stackoverflow.com/a/42656772/10223558
  • 如果不需要,您也可以省略整个 CC 和 BCC 内容,使代码更具可读性。像 outApp 一样将 mark 移动到 Method 内部,然后它会在 Method 结束时超出范围,您不必= Nothing 它。
  • 嗨 L8n,通过从声明中删除 New。宏不将备注声明为对象并返回不存在错误。但是在错误之后,重新编写“New”。 The code runs fine.. i noticed that the code runs fine when the pop out windows of selecting outlook profile is gone.. So i think i have to do with the popup windows itself that block the email from being detected by my Remarks class

标签: excel vba outlook runtime-error


【解决方案1】:

清理了一下代码,我无法测试它,因为我不知道备注类是什么。 还有一些看起来很奇怪的东西,automail 是一个什么样的模块(class/UF/module)? Button_Click 看起来有点像 UserForm,在这种情况下我建议阅读:https://rubberduckvba.wordpress.com/2017/10/25/userform1-show/

Public PublicRow As Integer

Sub Button_Click()


    Dim LastR As Long
    Dim CRow As Long
    Dim sSendTo As String
    Dim sSubject As String
    Dim bodyTxt As String
    Dim OutApp As Object
    Dim OutMail As Object

    Set OutApp = CreateObject("Outlook.Application")

    Dim mark As Remarks
    Set mark = New Remarks
    'This is the "usual" way to instantiate an object, see here: https://stackoverflow.com/a/42656772/10223558

    Set mark.item = Nothing ' why set it to nothing here, usually this would happen in the class itself?

    OutApp.Session.Logon

    LastR = Cells(Rows.Count, 2).End(xlUp).Row

    For CRow = 3 To LastR
        If Cells(CRow, 6) <> "Email sent" And Cells(CRow, 3) <= Date Then
            Set OutMail = OutApp.CreateItem(olMailItem) 'use constant name instead of integer, makes it more legible.

            sSendTo = Cells(CRow, 5)
            sSubject = "Project Due Date"
            PublicRow = CRow

            bodyTxt = buildBody(Cells(CRow, 4),  Cells(CRow, 2)

            With OutMail
                .To = sSendTo
                .Subject = sSubject
                .Body = bodyTxt 
                .Display (True)
            End With
            Set mark.item = OutMail
            'shouldn't there be some code to send the mail here?
        End If

    Next CRow
End Sub

Private Function buildBody(ByVal receiverName as String, ByVal projectName as String) as String
    Dim txt As String
    txt = "Dear " & receiverName  & ", "
    Txt = txt & vbCrLf & vbCrLf
    txt = txt & "The due date has been reached for the project:"
    txt = txt & vbCrLf & vbCrLf
    txt = txt & "    " & projectName 
    txt = txt & vbCrLf & vbCrLf
    txt = txt & "Please take the appropriate actions."
    txt = txt & vbCrLf & vbCrLf
    txt = txt & "Regards,"
    txt = txt & vbCrLf
    txt = txt & "Danial"
    buildBody = txt
End Function

【讨论】:

  • 子、模块和类名只是我重命名为的类。我使用代码中的 .display 函数发送邮件我将标记设置为空,因为我认为实例来自以前的应用程序会影响下一个实例。我对此非常了解,只是尝试过。我会尝试代码,并会告诉你它是如何进行的
  • 更新这里,测试你的代码。同样的错误发生了。第一封电子邮件工作正常,但下一封返回错误。这就是我的代码的工作方式。我的 excel 包括具有不同电子邮件的列,以及具有不同截止日期的列。到达截止日期后,单击表单按钮将向所有相应的电子邮件发送有关截止日期的警告。当我使用已过期的 2 行到期日期测试代码时,就会发生错误。当我单击表单按钮时,outlooks 会显示电子邮件窗口,其中包含要发送的第一封电子邮件的正文、电子邮件和主题,并为下一封返回错误。
  • 备注类,顾名思义..只是对每一行进行备注..作为检查电子邮件是否发送的条件,或者确实只是在显示后关闭
  • 非常感谢您清理我的代码 :).. 我很感激 :).. 我是 vba 新手,所以我可能会犯错误 XD
  • 嗨 L8n,我认为问题是每次运行代码时都没有打开 Outlook。所以我在代码中添加了 Shell ("OUTLOOK") 以在创建电子邮件之前先运行它并且它有效!.. 但现在的问题是 Outlook 应用程序现在打开并每次我希望它在后台运行时弹出.. 你知道如何做到这一点吗?
猜你喜欢
  • 1970-01-01
  • 2013-10-23
  • 2014-10-10
  • 2022-07-06
  • 1970-01-01
  • 1970-01-01
  • 2015-05-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多