"参数类型不匹配"尝试从excel Userform发送电子邮件时出错

时间:2015-12-01 19:08:21

标签: excel excel-vba outlook vba

我的一般定义中有这个集合 -

Public Sub Mail_Outlook_With_Signature_Plain(strbody As String)
    Dim OutApp As Object
    Dim OutMail As Object

    Set OutApp = CreateObject("Outlook.Application")
    Set OutMail = OutApp.CreateItem(0)

    On Error Resume Next
    With OutMail
        'Send to Manager email entry when ready to go live
        .To = ""
        .CC = "" 
        .BCC = ""
        .Subject = "There was an error/best practice found in" & Me.txtJobNumber
        .Body = strbody
        .Display   
    End With
    On Error GoTo 0

    Set OutMail = Nothing
    Set OutApp = Nothing
End Sub

然后在命令按钮sub下面我有两行代码:

Content = "This is the body of the email"     
Call Mail_Outlook_With_Signature_Plain(Content)

错误突出显示单词内容,并告诉我编译错误:ByRef参数类型不匹配。

我试过"设置Content ="我仍然得到同样的错误。

我做错了什么?

1 个答案:

答案 0 :(得分:1)

您需要在使用内容之前声明内容。

Dim Content As String
Content = "This is the body of the email"
Call Mail_Outlook_With_Signature_Plain(Content)

如果您不使用“Option Explicit”,则会发生这种情况。在代码的最顶层添加它(在所有子和函数之上),将要求您声明变量。如果未声明变量,VB将猜测并且您将遇到这些问题。

Option Explicit

Private Sub CommandButton1_Click()
    Dim Content As String
    Content = "This is the body of the email"
    Call Mail_Outlook_With_Signature_Plain(Content)
End Sub

Public Sub Mail_Outlook_With_Signature_Plain(strbody As String)
    Dim OutApp As Object
    Dim OutMail As Object

    Set OutApp = CreateObject("Outlook.Application")
    Set OutMail = OutApp.CreateItem(0)

    On Error Resume Next
    With OutMail
        'Send to Manager email entry when ready to go live
        .To = ""
        .CC = ""
        .BCC = ""
        .Subject = "There was an error/best practice found in " & Trim(Str(Me.txtJobNumber))
        .body = strbody
        .display
    End With
    On Error GoTo 0

    Set OutMail = Nothing
    Set OutApp = Nothing

End Sub