【发布时间】:2015-07-29 05:15:20
【问题描述】:
我正在尝试使用 VBA 在 Outlook 中获取电子邮件中的电子邮件标题和正文。我正在使用Application_NewMail() 事件处理程序来处理新邮件已经到达,但我不知道如何从那里获取标题和正文。
这是我在 Application_NewMail() 事件处理程序中的代码:
Private WithEvents myOlItems As Outlook.Items
Private Sub Application_NewMail()
Dim olApp As Outlook.Application
Dim oNS As NameSpace
Dim oFolder As MAPIFolder
Dim oNewMail As MailItem
Set olApp = Outlook.Application
Set oNS = GetNamespace("MAPI")
Set oFolder = oNS.GetDefaultFolder(olFolderInbox)
Set oNewMail = oFolder.Items.GetFirst
'This is the string that hold the mail body.
Dim mailBody As String
Dim mailArg() As String
MsgBox "New Mail!"
End Sub
一旦我收到电子邮件,此功能就会正常触发。我成功让消息框弹出。但我希望能够读取邮件正文和标题以插入数据库。
它的实际数据库方面我知道该怎么做,但我不确定如何从电子邮件中获取标题和正文。我尝试过这样的事情:
Set olItem = ActiveExplorer.Selection.Item(1)
mailBody = oNewMail.Body
mailArg = Split(mailBody, vbLf)
'Check to see what is inside the body. We need to say Tank X: Y
MsgBox "This is line one " & mailArg(0) & "This is line two " & mailArg(1)
我收到错误:Object variable or With block variable not set
任何帮助将不胜感激。
【问题讨论】: