在Outlook中使用主题关键词接收电子邮件后运行脚本。

huangapple go评论69阅读模式
英文:

Run script after receiving email in Outlook with keywords in subject

问题

我想它只会在新邮件的主题中有特定的几个词时触发。主题每次都会不同,但其中的几个词将保持不变。

英文:

I have the following which calls a Python script after receiving any email:

Option Explicit

'This "event" runs whenever there is a new email
Private Sub Application_NewMailEx(ByVal EntryIDCollection As String)
'Declare as generic Object since we don't know what type of NewMail Item we got yet
Dim entry As Object

'Me refers to ThisOutlookSession, the GetNamespace is just something we always have to do in outlook
'GetItemFromID gets the mail object which we need to have to check if its an email
'or if we want to do something with it like move it to another folder
Set entry = Me.GetNamespace("MAPI").GetItemFromID(EntryIDCollection)

'Exit our event handler if this isn't an email - meetings and notifications also may pop up here
If Not TypeOf entry Is MailItem Then Exit Sub

'Call the sub that will handle the new email
passing in the email VBA object in case that is important
OnNewEmail entry
End Sub


'This Sub will be called whenever the Application_NewMailEx event is triggered with a MailItem
Private Sub OnNewEmail(ByVal email As MailItem)

Shell "C:\Program Files\Python310\python.exe C:\Users\lradico\Desktop\WWP\waites_slack.py"
End Sub

I want it to only trigger if the new email has a few specific words in the subject. The subject will be different every time, but a few words in it will remain the same.

答案1

得分: 1

If InStr(1, email.subject, yourString) > 0 Then
'如果邮件主题中包含yourString,则执行以下代码(如果有多个字符串,可以嵌套If语句或使用And)
End If

英文:
If InStr(1, email.subject, yourString) > 0 Then
    'code to execute if mail has yourString in subject (for more than 1 String just nest Ifs or use And
End If

答案2

得分: 0

以下是翻译好的部分:

首先,无需单独处理 Application 类的 NewMailNewMailEx 事件。只有在代码中检测到特定关键字的项目时,才需要运行 Python 脚本。为了执行这项任务,您需要处理 NewMailEx 事件,在其中可以检查 Subject 属性,然后根据需要运行脚本:

Option Explicit

Private Sub Application_NewMailEx(ByVal EntryIDCollection As String)
  Dim entry As Object
  Set entry = Me.GetNamespace("MAPI").GetItemFromID(EntryIDCollection)
  
  If Not TypeOf entry Is MailItem Then Exit Sub

  ' 文本比较,从位置 1 开始
  If InStr(1, entry.subject, keyword) > 0 Then
    Shell "C:\Program Files\Python310\python.exe C:\Users\lradico\Desktop\WWP\waites_slack.py"
  End If
  
End Sub

最简单的解决方案是使用 InStr 函数,该函数返回一个整数,指定一个字符串在另一个字符串中首次出现的位置。

此外,您可以考虑使用正则表达式来对主题字符串应用更复杂的模式,请参阅 Outlook VBA 中使用正则表达式从电子邮件正文获取文本 以获取更多信息和代码示例。

英文:

First of all, there is no need to handle the NewMail and NewMailEx events of the Application class separately. You need to run the python script only if items with specific keywords are detected in the code. For that task you need to handle the NewMailEx event where you could check the Subject property and then if required run the script:

Option Explicit

Private Sub Application_NewMailEx(ByVal EntryIDCollection As String)
  Dim entry As Object
  Set entry = Me.GetNamespace("MAPI").GetItemFromID(EntryIDCollection)
  
  If Not TypeOf entry Is MailItem Then Exit Sub

  ' A textual comparison starting at position 1
  If InStr(1, entry.subject, keyword) > 0 Then
    Shell "C:\Program Files\Python310\python.exe C:\Users\lradico\Desktop\WWP\waites_slack.py"
  End If
  
End Sub

The easiest solution is to use the InStr function which returns an integer specifying the position of the first occurrence of one string within another.

Also you may consider using regular expressions to apply more complex patterns to the subject string, see RegEx in Outlook VBA to get text from Email Body for more information and code samples.

huangapple
  • 本文由 发表于 2023年5月6日 21:37:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/76189188.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定