2017-09-28 46 views
0

當談到VBA代碼時,我是一個noob。我們的想法是掃描特定主題行的傳入電子郵件,從電子郵件的第一行提取電子郵件地址,並回復從第一行提取的電子郵件地址。從字符串中提取電子郵件地址:無效的過程調用或參數

問題出在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 

回答

0

最有可能的郵件正文不包含任何'<'或'>'。在這種情況下,Instr將返回0,和你結束了一個命令left(sText, -1)將exacly拋出你描述

對於一個首發的錯誤,更改代碼

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

之後,你必須做出你的心如何在這種情況下做的(你應該還你找到的情況下的「<」,但沒有「>」)

0

也許<>sText表示爲&lt;&gt;

0

通常很好的做法是聲明所有的變量,即使它們只是字符串。

我會爲所有你使用的字符串做這個。我也會改變你的日常SendReply如下:

私人小組SendReply(BYVAL Tostr作爲字符串,BYVAL Subjectstr作爲字符串,BYVAL Bodystr作爲字符串,BYVAL senderstr作爲字符串)

從內存中,如果你不做到上述,代碼不知道變量應該是什麼數據類型。

相關問題