尝试创建个性化的批量电子邮件,并不断获取“ mail_body_message”的错误代码“ ByRef参数类型不匹配”

时间:2019-12-19 14:27:43

标签: excel vba outlook

您好,我在第25行(mail_body_message)上看到错误代码

  

“ ByRef参数类型不匹配”

Oracle Database Concepts Glossary

Sub SendEmail(what_address As String, subject_line As String, mail_body As String)

Dim olApp As Outlook.Application
Set olApp = CreateObject("Outlook.Application")


    Dim olMail As Outlook.MailItem
    Set olMail = olApp.CreateItem(olMailItem)

    olMail.To = what_address
    olMail.Subject = subject_line
    olMail.Body = mail_body
    olMail.Send


End Sub
Sub SendMassEmail()

   row_number = 1

   Do
   DoEvents
    row_number = row_number + 1
    Dim mail_body As String
    Dim full_name As String
    Dim promo_code As String

    mail_body_message = Sheet1.Range("J2")
    full_name = Sheet1.Range("B" & row_number) & " " & Sheet1.Range("C" & row_number)
    promo_code = Sheet1.Range("D" & row_number)
    mail_body_message = Replace(mail_body_message, "replace_name_here", full_name)
    mail_body_message = Replace(mail_body_message, "promo_code_replace", promo_code)
        MsgBox mail_body_message
    Call SendEmail(Sheet1.Range("A" & row_number), "This is a test e-mail", mail_body_message)
   Loop Until row_number = 6

   MsgBox "complete"
End Sub

1 个答案:

答案 0 :(得分:0)

如@BigBen所述,在代码顶部添加Option Explicit。这将迫使您声明变量。

提示提示:仅声明它们并不能解决您的问题。您需要将它们声明为Right Type

Sub SendEmail()中,参数mail_body被声明为String。因此,使用简单的语言,您需要在传递变量之前将其声明为String。这两个示例将使您更清楚。

样品A

Option Explicit

Sub Sample()
    Dim mail_body_message

    mail_body_message = "Blah"

    SendEmail mail_body_message
End Sub

Sub SendEmail(mail_body As String)
    MsgBox mail_body
End Sub

enter image description here

示例B

Option Explicit

Sub Sample()
    Dim mail_body_message As String '<~~ Declaring it as the RIGHT TYPE

    mail_body_message = "Blah"

    SendEmail mail_body_message
End Sub

Sub SendEmail(mail_body As String)
    MsgBox mail_body
End Sub

enter image description here

您可能还想阅读有关此错误ByRef argument type mismatch

的信息