Outlook VBA to Save Attachments from OUTGOING messages to a folder - vba

I would like to create an archive of OUTGOING attachments in a folder (a Windows folder, outside Outlook). I have used scripts to save attachments from INCOMING messages by using some of the solutions provided on this site, but I don't see a way to set this up for outgoing mail. I also tried to set up a rule to apply a script to all outgoing messages, but I don't see an option to "run a script" on messages I send (like I can for incoming messages).
I can probably use a script that parses an outlook folder, but it would be much more effective to have it run in real-time as messages are sent.

Process the Application.ItemSend event - the item will be passed as a parameter to your event handler. You can then process the message attachment the same way your process the incoming messages.

If you really want to use the Outbox instead of the ItemSend event (which is probably a better solution), try this (found here and modified to use Outbox)
Public WithEvents myOlItems As Outlook.Items
Public Sub Initialize_handler()
Set myOlItems = Application.GetNamespace("MAPI").GetDefaultFolder(olFolderOutbox).Items
End Sub
Private Sub myOlItems_ItemAdd(ByVal Item As Object)
'your code to save attachments here
End Sub

Related

Saving Outlook Attachments Automatically Based On Attachment File Name Only

I need to save an Outlook file attachment with a static file name to a specific network location automatically when that email arrives. This file will be saved in a network location for upload using a monthly SSI server package. I hope to do this automatically without any interaction but not opposed to manually running a macro. I am unsure of references that are needed in VBA to get this to execute. I am also unfamiliar with the "ThisOutlookSession" configuration that I've seen in similar threads
I have attempted to use the existing script that I've seen here with no luck. ( I can get them to run without error but do not get any results ) I want to search all incoming email and only have it take action if the email has an attachment and that attachment has a specific unchanging file name. I have the developer tab enabled in Outlook and can access VBA through it. Looking for a solid simple solution. Constants are the file name and extension as well as the network folder. Variables would be the sender and date of delivery. Office 365 running Windows 10 in a professional environment. Any help or direction would be greatly appreciated.
You can handle incoming emails in Outlook in the NewMailEx event handler. The event fires once for every received item that is processed by Microsoft Outlook. The item can be one of several different item types, for example, MailItem, MeetingItem, or SharingItem. The EntryIDsCollection string contains the Entry ID that corresponds to that item. Use the Entry ID string to call the NameSpace.GetItemFromID method and process the item. The NewMailEx event fires when a new message arrives in the Inbox and before client rule processing occurs.
Also, as a possible workaround, you may consider handling the ItemAdd event on the Inbox folder. This event is fired when one or more items are added to the specified collection. This event does not run when a large number of items are added to the folder at once. For example:
Public WithEvents myOlItems As Outlook.Items
Public Sub Application_Startup()
Set myOlItems = Application.GetNamespace("MAPI").GetDefaultFolder(olFolderContacts).Items
End Sub
Private Sub myOlItems_ItemAdd(ByVal Item As Object)
MsgBox Item.Attachments.Count
End Sub

Outlook VBA Type mismatch (error 13) when initializing handler for Sent Mail folder

This is my first thread on the forum so I hope i do everything as is needed.
Since a few years we use a vba outlook macro at our firm to send our sent mails to a folder. The folder is specified when pressing the SEND button. When we created the macro, we used to save the file directly after pressing send, but then only a draft file was saved to the specified folder. Herefore we initiated a handler to check when the mail is added to the Sent Mail folder so we could save this mail to the specified folder. Since yesterday, only some of my colleagues started getting a Type mismatch error while running this macro. Today another couple of colleagues is having this issue as well. Most of us can still use the macro without having any errors.
I narrowed the problem down to the initialize_handler itself by creating a much easier macro. So now when sending any email, I initialize a handler for the items in my Sent Mails folder. When an item is added to the Sent Mails folder, a messagebox should appear telling me an item is added.
The type mismatch error is created on "Set myOlItems = ...."
Public WithEvents myOlItems As Outlook.Items
Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
Call Initialize_handler
End Sub
Public Sub Initialize_handler()
Set myOlItems = Application.GetNamespace("MAPI").GetDefaultFolder(olFolderSentMail).Items
End Sub
Private Sub myOlItems_ItemAdd(ByVal ObjectSent As Object)
MsgBox "Item is added to Sent Mails"
End Sub
I hope someone has a solution to this. I wasn't able to find out what could trigger the error?
Thanks!
I'd recommend starting from breaking the chain of property and methods calls by declaring each property or method call on a separate line of code. So, we will be able to find out which property or method calls fails.

How to execute VBA after Outlook Application_Startup event handler and after all folders sync?

I'm trying to run a macro that moves emails received before today to a cabinet folder whenever Outlooks starts. The problem is that the Application_Startup event handler happens before Outlook is completely loaded and folders have synced. Consequently, all the emails that came in last night aren't moved to the cabinet when I open Outlook in the morning.
To fix this I created a custom class to instantiate an Outlook.syncObject which syncs all folders and provides an event handler when the syncing is complete. I create an object from this class within the Application_Startup event handler. However, this sync doesn't seem to actually retrieve any emails and also seems to complete before Outlook has even loaded.
It seems like being able to execute code after Outlook has done all of it's startup processes would be a common feature request. Thanks for any help.
This sample code simply shows me how many unread emails are in my inbox. If I close Outlook, send myself an email, then open Outlook, I need Outlook to load and a full sync to occur before generating the messagebox with the number of unread emails in my inbox.
Oulook Application_Startup event handler:
Dim mySyncInstance As New mySync
Private Sub Application_Startup()
mySyncInstance.Initialize_handler
End Sub
Custom mySync Class Code:
Dim WithEvents mySync As Outlook.syncObject
Sub Initialize_handler()
Set mySync = Application.Session.SyncObjects.item(1)
mySync.Start
End Sub
Private Sub mySync_SyncEnd()
MsgBox Application.GetNamespace("MAPI").GetDefaultFolder(olFolderInbox).items.Restrict("[UnRead] = True").Count & _
" Emails are unread in the main inbox."
End Sub
I'm trying to run a macro that moves emails received before today to a cabinet folder whenever Outlooks starts.
You may consider handling the NewMailEx event of the Application class which is fired when a new item is received in the Inbox. So, you can get the mail item and decide whether you need to move it in a subfolder or not. This event fires once for every received item that is processed by Microsoft Outlook. The item can be one of several different item types, for example, MailItem, MeetingItem, or SharingItem. The EntryIDsCollection string contains the Entry ID that corresponds to that item.
Also you can create a rule and assign a VBA macro sub. The incoming instance of the mail item object will be passed as a parameter. For example:
public sub test(mail as MailItem)
' do whatever you need
end sub
As an alternative way you can handle the ItemAdd event of the Items class which is fired when one or more items are added to the specified collection. This event does not run when a large number of items are added to the folder at once (more than 16).
Why not use Items.ItemAdd event on the Inbox folder?

Finding Outlook event triggered by third party email send

I have a third party piece of software that sends email through Outlook. It allows us to specify a "reply to" email address, but not a "from" email address. I'm trying to write something in VBA that notices when a message comes from that third party software and uses the "reply to" address as the "from" address before it sends the email. I'm having trouble getting any events to trigger when I send email with the third party software.
If I use
Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
MsgBox "Test"
End Sub
it triggers the message box when I send an email through Outlook, but not when the third party software does. Is there another event I should be looking at instead? I'm looking at Application events or MailItem events (http://msdn.microsoft.com/en-us/library/office/dn320237(v=office.15).aspx) and nothing but the Send events seem at all appropriate. I tried the sample code for the item Send event
Public WithEvents myItem As Outlook.MailItem
Sub SendMyMail()
Set myItem = Outlook.CreateItem(olMailItem)
myItem.To = "Dan Wilson"
myItem.Subject = "Data files information"
myItem.Send
End Sub
Private Sub myItem_Send(Cancel As Boolean)
myItem.ExpiryTime = #2/2/2003 4:00:00 PM#
End Sub
but it doesn't trigger even when I send an email through Outlook. I did try restarting Outlook before testing that out.
This question looks promising: How do I trigger a macro to run after a new mail is received in Outlook?
but it involves setting an event listener to the Inbox. I've heard that there's a way to change settings somehow in a way that prevents the third party messages from being automatically sent, and when that happens they get stuck in Outlook's Drafts folder. How would I go about putting an event listener on the Drafts folder instead of the Inbox?
Application.ItemSend event is only fired if a message is sent through the Outlook Object Model. If the other app uses Extended MAPI, no event fires.

Issue with Outlook Event for New Mail

I would like to do some operation when a new mail comes to mailbox. For that I am using Item_Add() Event
Private Sub myOlItems_ItemAdd(ByVal Item As Object)
... Do Something
Exit Sub
Can anyone tell me How to set the Handler for "inbox" folder under "gilbertojperera#gmail.com"?
Problem:
The issue I am facing here is that this even get fired only when a new mail comes in "Inbox" folder under "Outlook Data File". But I used to receive mails in the other "Inbox" folder marked by second red arrow (third last folder)
Due to this problem My Item_Add() event is not being triggered & resulting to my outlook macro Fail.
Please do help if you have some valuable inputs.
It sounds like you only initialized the handler for the Inbox DefaultFolder (olFolderInbox). You also must add a handler to the Inbox folder for gilbertojperera#gmail.com.