从字符串中提取电子邮件地址:无效的过程调用或参数
问题描述:
当谈到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
答
最有可能的邮件正文不包含任何'<'或'>'。在这种情况下,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))
....
之后,你必须做出你的心如何在这种情况下做的(你应该还你找到的情况下的“<”,但没有“>”)
答
也许<
和>
在sText
表示为<
和>
。
答
通常很好的做法是声明所有的变量,即使它们只是字符串。
我会为所有你使用的字符串做这个。我也会改变你的日常SendReply如下:
私人小组SendReply(BYVAL Tostr作为字符串,BYVAL Subjectstr作为字符串,BYVAL Bodystr作为字符串,BYVAL senderstr作为字符串)
从内存中,如果你不做到上述,代码不知道变量应该是什么数据类型。