问题
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