简体   繁体   English

获取电子邮件正文中的特定行 vba Outlook

[英]Get a specific Line in a body of email vba outlook

Every time the mail is received, I want to print out a specific word in a body of email:每次收到邮件时,我想打印出电子邮件正文中的特定单词:

---------------------------------Body of email------------------------------------- ---------------------------------电子邮件正文-------------- -----------------------

Sender Name: John发件人姓名:约翰

Sender's Address: john@sample-mail.com发件人地址:john@sample-mail.com

Subject: Mail Subject from John <----- the line that I want to get主题:来自约翰的邮件主题<----- 我想得到的行

--------------------------------------End------------------------------------------ - - - - - - - - - - - - - - - - - - - 结尾 - - - - - - -------------------------------

My Code:我的代码:

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


Expected output:预期输出:

Subject: Mail Subject from John

The output I get gives nothing at all我得到的输出什么都不给

You could use breakpoints to debug the code.您可以使用断点来调试代码。

Instead of代替

BodySubj = Split(olMail.Body, vbNewLine)

Use采用

BodySubj = Split(olMail.Body, Chr(10) & Chr(13))

Instead of relying on message body lines you can use regular expressions to extract the exact substrings, read more about that:您可以使用正则表达式来提取确切的子字符串,而不是依赖消息正文行,请阅读更多相关信息:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM