Download pdf attachments only if email subject has one of words

Status
Not open for further replies.

Gaurav

Member
Outlook version
Outlook 2016 64 bit
Email Account
Office 365 Exchange
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
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.
 

Gaurav

Member
Outlook version
Outlook 2016 64 bit
Email Account
Office 365 Exchange
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.
 

Gaurav

Member
Outlook version
Outlook 2016 64 bit
Email Account
Office 365 Exchange
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 ?
 

Gaurav

Member
Outlook version
Outlook 2016 64 bit
Email Account
Office 365 Exchange
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
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...
 

Gaurav

Member
Outlook version
Outlook 2016 64 bit
Email Account
Office 365 Exchange
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
After any changes to the code you need to run the Application_Startup procedure, or restart Outlook. Did you do that?
 

Gaurav

Member
Outlook version
Outlook 2016 64 bit
Email Account
Office 365 Exchange
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 ?
 
Status
Not open for further replies.
Similar threads
Thread starter Title Forum Replies Date
K Outlook Office 365 VBA download attachment Outlook VBA and Custom Forms 2
GregS Outlook 2016 iPhone won't download Outlook 2016 Mail. Using Outlook 0
O Office 365 Outlook - mark to download Using Outlook 0
K Outlook 2010 duplicate download emails 1 inbox 1 PST no updates Using Outlook 3
R Would creating a new profile cause Outlook to download all the old mails from the server? Using Outlook 1
M Macro for attachments download adjustment Outlook VBA and Custom Forms 3
S Outlook 2013 doesn't download html messages Using Outlook 0
M Google Apps Mail *SENT* from iPhone doesn't download to Outlook via POP3 Using Outlook 1
P Email Download Outlook VBA and Custom Forms 12
C bcm 2010 download does not recognize outlook Using Outlook 3
RBLampert Outlook downloads error out if one particular account is the first to download Using Outlook 0
B Automatic picture download and changing email addresses Using Outlook 3
rezazy Do not download old email Using Outlook 1
D How to force full message download for all Gmail messages in all folders with IMAP in Outlook 2013 Using Outlook 1
V IMAP - download headers only Using Outlook 4
D VBA script to auto download attachments and rename file according to subject line Outlook VBA and Custom Forms 23
Z Download & Move Attachments Using Outlook 1
G Outlook 2013 won't download images Using Outlook 6
L Download/export multiple POP accounts Using Outlook 1
A Outlook doesnt download emails after reinstalling Using Outlook 4
T Unable to download e-mail sub account in OL 2010. No probs with Mailwasher Using Outlook 6
R Outlook download will not complete. Using Outlook 1
R Outlook Connector will not download ALL messages from hotmail accounts Using Outlook.com accounts in Outlook 4
S Outlook doesn't download all hotmail emails on Vista Using Outlook 2
K Download emails to HD Using Outlook 1
N Configure Outlook 2010 to download sent emails Using Outlook 4
Forum Admin Download BCM 2010 BCM (Business Contact Manager) 0
D Is It Possible To Download Contact Mngr Via a Website? BCM (Business Contact Manager) 1
P Download BCM Database BCM (Business Contact Manager) 1
R Auto Download Document Outlook VBA and Custom Forms 2
P How Do I Download Business Contact Manager BCM (Business Contact Manager) 1
P Download for KB957324 BCM (Business Contact Manager) 13
I Button PDF in Outlook Contact custom form Outlook VBA and Custom Forms 1
X Outlook automation pull from PDF Using Outlook 5
L Tired of Outlook disabling Adobe PDF Creator add-in Using Outlook 1
D Saving Selected Emails as PDF and saving Attachments Outlook VBA and Custom Forms 6
I Outlook 2016 64bit - on receipt convert emails into PDF and save Outlook VBA and Custom Forms 2
C replace subject line generated by converting a word document to PDF and sending it to an email Using Outlook 8
K add pdf to every reply or forward Outlook VBA and Custom Forms 1
Diane Poremsky Save Outlook Email as a PDF Using Outlook 0
C Auto Add a PDF to an outgoing Email, based on a rule Using Outlook 2
B Auto open PDF Outlook VBA and Custom Forms 0
P Printing PDF attachments of Outlook message attachments Outlook VBA and Custom Forms 2
R how to open a pdf file with a known password Outlook VBA and Custom Forms 1
V Emails, pdf.s ocr - oh my Using Outlook 1
E PDF Import Using Outlook 1
M VBA code to save email attachments (PDF) as email subject line Outlook VBA and Custom Forms 1
D Save pst and attachments as a PDF File Outlook VBA and Custom Forms 3
J Auto print PDF attachments as they arrive with certain words in subject Outlook VBA and Custom Forms 3
E Outlook VBA to print attached Pdf to a fax printer and assign fax number Using Outlook 0

Similar threads

Top