编译错误:参数数量错误或属性赋值无效

时间:2018-05-18 12:31:14

标签: excel vba

我收到编译错误:下面一段代码中的参数数量错误或属性赋值无效

Sub SendEmail()

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

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

    olMail.To = "vaibhav.joshi@feaindia.org"
    olMail.Subject = "Test Email"
    olMail.Body = "Hello. Welcome on board"
    olMail.Send

End Sub

Sub SendMassEmail()

Row_Number = 1

Do
DoEvents

    Row_Number = Row_Number + 1

    Call SendEmail(Sheet1.Range("A" & Row_Number), "This is a test email", Sheet1.Range("L1"))
    Loop Until Row_Number = 18
End Sub

1 个答案:

答案 0 :(得分:3)

您需要调整SendMail子项以接受您正在为其提供的参数。

Sub SendEmail(eml as string, sbj as string, bdy as string)

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

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

    olMail.To = eml
    olMail.Subject = sbj
    olMail.Body = bdy
    olMail.Send

End Sub

Sub SendMassEmail()

    Row_Number = 2

    Do while Row_Number <= 18
        Call SendEmail(Sheet1.Range("A" & Row_Number), "This is a test email", Sheet1.Range("L1"))
        Row_Number = Row_Number + 1
        DoEvents
    Loop

End Sub