運行時錯誤,用於電子郵件自動化的 VBA EXCEL (Runtime Error, VBA EXCEL for email automation)


問題描述

運行時錯誤,用於電子郵件自動化的 VBA EXCEL (Runtime Error, VBA EXCEL for email automation)

我一直在使用 vba 為 excel 進行電子郵件自動化,我的代碼僅適用於第一封電子郵件,並且在下一封電子郵件中出現運行時錯誤,

我嘗試將對象設置為無濟於事,但無濟於事..

我不知道內存洩漏在哪裡

有人能指出我嗎

錯誤在對象備註處。我提供了錯誤的屏幕截圖。

Module Name = Automail

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

Class Name = Remarks

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

錯誤:

在此處輸入圖片描述


參考解法

方法 1:

Cleaned up the code a bit, I can't test it since I do not know what the remarks class is. There are a few other thing that look strange, what kind of module (class/UF/module) is automail? The Button_Click look suspiciously like a UserForm, in this case I recommend reading: 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

(by ArepeelL8n)

參考文件

  1. Runtime Error, VBA EXCEL for email automation (CC BY‑SA 2.5/3.0/4.0)

#outlook #excel #vba #runtime-error






相關問題

Outlook 2007/2010 中的 Vspace (Vspace in Outlook 2007/2010)

Outlook 2010 自動完成流(緩存的聯繫人) (Outlook 2010 autocomplete stream ( cached contacts))

如何格式化電子郵件中的字符串以便 Outlook 打印換行符? (How do I format a String in an email so Outlook will print the line breaks?)

通過 Office 加載項將內容添加到 Outlook 電子郵件正文 (Adding content to an Outlook email body via an Office Add-In)

Outlook Addin 中的 Recipient.Name 和 ExchangeUser.Name 有什麼區別? (What is the Difference Between Recipient.Name and ExchangeUser.Name in Outlook Addin?)

運行時錯誤,用於電子郵件自動化的 VBA EXCEL (Runtime Error, VBA EXCEL for email automation)

將 Crystal Reports 轉換為 HTML 並作為正文發送到我的郵件中 (Crystal Reports into HTML and sending in my mail as body)

通過 VBA 將格式化的電子郵件正文複製到新的 Word 文檔中並保留文本格式 (Copy formatted Email body into new Word document via VBA and keep the text formatting)

Outlook 在啟動時忽略加載項的 HKEY_LOCAL_MACHINE 條目 (Outlook ignoring HKEY_LOCAL_MACHINE entry for add-in on startup)

如何知道是否收到或發送了電子郵件? (How to know if an e-mail was received or sent?)

如何從 Excel 表格創建電子郵件? (How to create emails from Excel table?)

如何在 Outlook 正文中左對齊從 Excel 範圍創建的 HTML 表格? (How to Left-Align HTML Table, created from an Excel Range, in an Outlook Body?)







留言討論