How do I write an IF statement that searches for a specific word in an email's subject line

只谈情不闲聊 提交于 2019-12-11 10:26:29

问题


I've got a rule + script setup in outlook. The rule looks for specific words in the email subject and then runs the script (defined in Modules) below. But it seems to be only working for my personal inbox and not a group inbox. The below is the code that works.

The lines in comments are me trying to work it out.

Public Sub saveAttachtoDisk(item As Outlook.MailItem)
Dim objAtt As Outlook.Attachment
Dim saveFolder As String
saveFolder = "d:\temp\"

Dim objNS As Outlook.NameSpace

Set Items = objNS.GetDefaultFolder(olFolderInbox).Items

'Set objNS = olApp.GetNamespace("MAPI")
'Set myRecipient = objNS.CreateRecipient("XXXXXXX")
'myRecipient.Resolve
'set Items = objNS.GetSharedDefaultFolder(myRecipient, olFolderInbox).Items
'Dim itm As Outlook.MailItem

' If TypeName(item) = "MailItem" Then
'    Set itm = item

     For Each objAtt In itm.Attachments
          objAtt.SaveAsFile saveFolder & "\" & objAtt.DisplayName
          Set objAtt = Nothing
     Next
End Sub

回答1:


This is how to go and read the email subject to launch further code :

Public Sub saveAttachtoDisk()
Dim olApp As Outlook.Application, _
    oNS As Outlook.NameSpace, _
    oFld As Outlook.Folder, _
    oMails As Outlook.Items, _
    oMail As Outlook.MailItem, _
    oAtt As Outlook.Attachment, _
    SaveFolder As String

SaveFolder = "d:\temp\"

On Error Resume Next
Set olApp = GetObject(, "Outlook.Application")
If Err.Number > 0 Then Set olApp = CreateObject("Outlook.Application")
On Error GoTo 0

Set oNS = olApp.GetNamespace("MAPI")
Set oFld = oNS.GetDefaultFolder(olFolderInbox)
Set oMails = oFld.Items

For Each oMail In oMails
    If InStr(1, oMail.Subject, "Txt_to_Find") Then
        '----Your code comes here
        For Each oAtt In oMail.Attachments
            oAtt.SaveAsFile SaveFolder & "\" & oAtt.DisplayName
            Set oAtt = Nothing
        Next oAtt
    Else
    End If
Next oMail
End Sub


来源:https://stackoverflow.com/questions/33513822/how-do-i-write-an-if-statement-that-searches-for-a-specific-word-in-an-emails-s

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!