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
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
W Outlook 2010: Need to keep restarting outlook to download mail from a MAPI server Using Outlook 4
J Error message when trying to download attachments to e-mails Using Outlook 1
W Cant download from office PKC Using Outlook 1
D Received emails show only red X's no images - Email set for HTML. Prevent download of images is OFF Using Outlook 6
A Copy email from external server, rather than totall download it Using Outlook 1
J cannot download pop3 email from Comcast after latest Microsoft update Using Outlook 5
G E2k7 | After changing OAB on database properties, how much time it will take for Outlook 2007 to download new OAB Using Outlook 2
V How to restrict users to not change Download Headers only Using Outlook 2
G Configure outlook to download hyperlinked images in email automatically to PST Using Outlook 2
K download oab operation failed 0x801901b8 Exchange Server Administration 1
B MICROSOFT OUTLOOK HOTMAIL CONNECTOR WILL NOT DOWNLOAD Using Outlook 2
N Outlook 2010 Clients can't reply to emails or download offline address book? Hosted Exchange 2007 Using Outlook 5
K OAB permissions – Users cannot download the OAB but domain admins can Exchange Server Administration 29
T I am looking for a Microsoft Outlook Email Stationery download? Using Outlook 5
E Outlook Download with produce license key Using Outlook 1
A Outlook will not download certain messages from my friends Using Outlook 10
H change os,download duplicate mail,give all of mails header in one process Using Outlook 2
M Outlook 2007 (cached mode), Exchange 2010 - Cant download Offline Address Book Exchange Server Administration 6
J I'm getting a lot of sync issues. I've got Outlook 2007 and using Outlook Connector to download my Using Outlook 2
M download emails and share with multiple users on Outlook 2003 while holding read and flagged status Using Outlook 2
R (GPO?) Setting that controls automatic picture download Outlook 2002 Using Outlook 3
L cannot download Offline address book Exchange Server Administration 13
M Remove download or save target as option with attachments in OWA 2007 Using Outlook 3
L trying to download a google app that alows me to sync outlook with the droid x. Can you assist me in the process? Using Outlook 1
A One user cannot access availability or download address book Using Outlook 2

Similar threads

Top