如何编写IF语句来搜索电子邮件主题行中的特定单词

时间:2015-11-04 04:39:37

标签: vba email outlook outlook-vba

我在outlook中设置了规则+脚本。该规则查找电子邮件主题中的特定单词,然后运行下面的脚本(在模块中定义)。但它似乎只适用于我的个人收件箱而不是群组收件箱。以下是有效的代码。

评论中的行是我试图解决的问题。

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 个答案:

答案 0 :(得分:1)

这是如何阅读电子邮件主题以启动更多代码:

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