从字符串中提取电子邮件地址:无效的过程调用或参数

时间:2017-09-28 10:04:07

标签: vba outlook outlook-vba

对于VBA代码,我是一个菜鸟。我们的想法是扫描特定主题行的传入电子邮件,从电子邮件的第一行提取电子邮件地址,并回复从第一行提取的电子邮件地址。

问题在于emailC行,它告诉我这是一个无效的过程调用或参数。

Private Sub Application_NewMailEx(ByVal EntryIDCollection As String)

    Dim mymail As Outlook.MailItem
    Dim ns As Outlook.NameSpace
    Set ns = Application.GetNamespace("MAPI")
    Set mymail = ns.GetItemFromID(EntryIDCollection)

    Substr = Trim(mymail.Subject)
    If InStr(1, Substr, "TEST SUBJECT") > 0 Then

        sText = mymail.Body

        vText = Split(sText, Chr(13), -1, vbTextCompare)
        'Find the next empty line of the worksheet
        emailC = Trim(Left(sText, InStr(sText, "<") - 1)) 'Split(vText(0), " ", -1, vbTextCompare)
        Resultstr = Trim(Mid(sText, InStr(sText, ">") + 1))
        senderstr = mymail.SenderEmailAddress

        Call SendReply(emailC, mymail.Subject, Resultstr, senderstr)

     End If

End Sub

Private Sub SendReply(Tostr, Subjectstr, Bodystr, senderstr)
    Dim mymail2 As Outlook.MailItem
    Set mymail2 = Application.CreateItem(olMailItem)

    nam = mymail2.Session.CurrentUser.Name

    With mymail2
        .To = senderstr
        .Subject = "RE: " & Subjectstr
        .ReplyRecipients.Add emailC
        .Body = Replace(Bodystr, Tostr, "", 1, -1, vbTextCompare)

    End With

    mymail2.Send

End Sub

3 个答案:

答案 0 :(得分:0)

邮件正文很可能不包含任何'&lt;'或''gt;'。在这种情况下,Instr将返回0,并且您最终会得到一个命令left(sText, -1),它会严重抛出您描述的错误

对于初学者,请更改代码

dim p as integer
p = InStr(sText, "<") 
if p = 0 then
    debug.Print "no '<' found, text = :" & sText
else
    emailC = Trim(Left(sText, p - 1)) 
    ....

之后你必须决定在这种情况下该怎么做(你也应该找到'&lt;'但没有'&gt;'的情况)

答案 1 :(得分:0)

<>可能在sText中表示为&lt;&gt;

答案 2 :(得分:0)

通常很好的做法是声明所有变量,即使它们只是字符串。

我会为你正在使用的所有String执行此操作。我还会按如下方式更改您的SendReply例程:

Private Sub SendReply(ByVal Tostr as String,ByVal Subjectstr as String,ByVal Bodystr as String,ByVal senderstr as String)

从内存中,如果你不这样做,代码就不知道变量应该是什么数据类型。

相关问题