• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

Download pdf attachments only if email subject has one of words

Outlook version
Outlook 2016 64 bit
Email Account
Office 365 Exchange
#1
Hello Everyone,

i am newbie in writing macros. eventually wrote a bit of it scraping the internet for ideas and help. now i am stuck here.
what i want is that on receipt of every email, if the subject of the email contains one of the words "rollover" or "repayment" or "drawdown" anywhere in the whole subjectline, only then the attachments should be downloaded and saved on the local drive. but its not working, even if the subject contains those words, it doesnt download and save any attachments at all.
This is what i have got so far.

[/code]
Option Explicit
Private WithEvents inboxItems As Outlook.Items
Private Sub Application_Startup()
Dim outlookApp As Outlook.Application
Dim objectNS As Outlook.NameSpace
Dim olRecipient As Outlook.Recipient
Dim olfolder As Folder

Set outlookApp = Outlook.Application
Set objectNS = outlookApp.GetNamespace("MAPI")

Set olRecipient = objectNS.CreateRecipient("Mum-Oper-Lnd")
olRecipient.Resolve
Set olfolder = objectNS.GetSharedDefaultFolder(olRecipient, olFolderInbox)
'Set olfolder = olfolder.Folders("Advices")
Set inboxItems = olfolder.Items
End Sub
Private Sub inboxItems_ItemAdd(ByVal Item As Object)
On Error GoTo ErrorHandler
Dim Msg As Outlook.MailItem
Dim Filename As String
Dim attach As Attachment
Dim subject As String
If TypeName(Item) = "MailItem" Then
Set Msg = Item
subject = Msg.subject

If InStr(1, subject, "rollover") > 0 Then

For Each attach In Msg.Attachments
Dim filetype As String
filetype = LCase$(Right$(attach.Filename, 4))

Select Case filetype
Case ".pdf"

Filename = "P:\attachment\" & attach.Filename
attach.SaveAsFile Filename
End Select

Next
End If

ProgramExit:
Exit Sub
ErrorHandler:
MsgBox (Err.Number & " - " & Err.Description)
Resume ProgramExit
End Sub

[/code]

Thanks in advance for the help.
 

Michael Bauer

Senior Member
Outlook version
Outlook 2010 32 bit
Email Account
Exchange Server
#2
set a breakpoint on this line:
Private Sub inboxItems_ItemAdd(ByVal Item As Object)

Then move an item into the folder. The code execution will stop at the breakpoint. Walk through it step by step by pressing f8, and see where it doesn´t do what you`d expect.
 
Outlook version
Outlook 2016 64 bit
Email Account
Office 365 Exchange
#3
set a breakpoint on this line:
Private Sub inboxItems_ItemAdd(ByVal Item As Object)

Then move an item into the folder. The code execution will stop at the breakpoint. Walk through it step by step by pressing f8, and see where it doesn´t do what you`d expect.
Thanks Michael.

The code doesn't perform this function:
If InStr(1, subject, "rollover") > 0 Then

It doesn't recognize the inStr "Rollover" in the subject. as a result doesn't perform any of the next conditions.

Thanks again mate.
 
Outlook version
Outlook 2016 64 bit
Email Account
Office 365 Exchange
#4
set a breakpoint on this line:
Private Sub inboxItems_ItemAdd(ByVal Item As Object)

Then move an item into the folder. The code execution will stop at the breakpoint. Walk through it step by step by pressing f8, and see where it doesn´t do what you`d expect.
Also, i am quite new to this vba thing.
Could you help with how to set a breakpoint ?
 
Outlook version
Outlook 2016 64 bit
Email Account
Office 365 Exchange
#5
Also, i am quite new to this vba thing.
Could you help with how to set a breakpoint ?
'
i did put a "stop" to add a breakpoint and stepped through with F8. it worked.
even without the breakpoint now it started working miraculously. i dont know how or why.

Anyways i still have 2 issues.
1. are the words example "rollover" or "repayment" -- case sensitive ? if they are how do i make the code run and do its job regardless whichever case the subject is in.
2. my current code as posted first, has only one code search within the subject line i.e. "rollover". now how to i incorporate other 2 keywords as well ? do i have to just repeat the InStr for other keywords or is there any other simpler way ?

Thanks in advance.
 

Michael Bauer

Senior Member
Outlook version
Outlook 2010 32 bit
Email Account
Exchange Server
#6
Yes, by default the Instr function is case-sensitve. Use it this way
Code:
if instr(1,"xxx",vbtextcompare)
. You need to test it; I always forget whether vbTextCompare or vbBinaryCompare makes it case-insensitive.

In order to check for more words, use it this way
Code:
if instr(...)>0 or instr(...)>0 then...
 
Outlook version
Outlook 2016 64 bit
Email Account
Office 365 Exchange
#7
Yes, by default the Instr function is case-sensitve. Use it this way
Code:
if instr(1,"xxx",vbtextcompare)
. You need to test it; I always forget whether vbTextCompare or vbBinaryCompare makes it case-insensitive.

In order to check for more words, use it this way
Code:
if instr(...)>0 or instr(...)>0 then...
Hi Michael,

vbtextcompare works like a charm.
but if i add more instances of Instr then again the code fails to do anything. i have kept Msgbox function to get a feedback for all incoming emails. its only then use a breakpoint and step through that the code works.

I am lost now.
 

Michael Bauer

Senior Member
Outlook version
Outlook 2010 32 bit
Email Account
Exchange Server
#8
After any changes to the code you need to run the Application_Startup procedure, or restart Outlook. Did you do that?
 
Outlook version
Outlook 2016 64 bit
Email Account
Office 365 Exchange
#9
After any changes to the code you need to run the Application_Startup procedure, or restart Outlook. Did you do that?
Yes Michael, i did restart the outlook couple of times. Any idea why is it not working ?

Anyways i tried the above on my personal mailbox. but the actual mailbox i want it to perform this is a group mailbox wherein probably i feel i do not have proper rights to it for this to execute. is that also going to be a problem ?
 

Similar threads