【发布时间】:2021-09-30 18:54:10
【问题描述】:
每次收到邮件时,我都想打印出邮件正文中的特定单词:
----------------------电子邮件正文---------- --------------------------
发件人姓名:约翰
发件人地址:john@sample-mail.com
主题:来自 John 的邮件主题
--------------------------结束-------- ----------------------------------
我的代码:
Private WithEvents olItems As Outlook.Items
Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
End Sub
Private Sub Application_Startup()
Dim olApp As Outlook.Application
Dim olNS As Outlook.NameSpace
Set olApp = Outlook.Application
Set olNS = olApp.GetNamespace("MAPI")
Set olItems = olNS.GetDefaultFolder(olFolderInbox).Folders("Watchlisted").Items
End Sub
Private Sub olItems_ItemAdd(ByVal Item As Object)
Dim BodySubjLine As String, BodySubj As Variant
Dim olMail As Outlook.MailItem
Dim olAtt As Outlook.Attachment
Set olMail = Item
BodySubj = Split(olMail.Body, vbNewLine)
BodySubjLine = Trim(BodySubj(2))
Debug.Print BodySubjLine
Set olMail = Nothing
End Sub
预期输出:
Subject: Mail Subject from John
我得到的输出什么也没有
【问题讨论】:
-
Debug.Print UBound(BodySubj)带给你什么?你没有看到任何错误?尝试遍历BodySubj并打印每个元素及其索引,这样您就可以确定您想要哪个元素。您可能希望使用vbCrLf进行拆分,而不是vbLf -
Debug.Print UBound(BodySubj)虽然什么也没做,但循环确实给了我一个想法。谢谢 :) -
它什么都不做 - 输出应该在 VB 编辑器的立即窗口中。