I have an application which generates e-mails with invoices attached as pdf. The generated e-mails are displayed in Outlook so you can edit them manually before sending.
On all invoices I want to add an extra attachment with a kind of disclaimer. I created a macro and a button which does this. Works fine, but I want this done automaticly upon creation of the e-mail.
My questions:
Does anyone know if it is possible to run a VBA Marco as soon as an e-mail is opened in the editor?
I noticed there is an ItemLoad Event but when this is excecuted, no values has been assigned yet so I cannot check the subject to see if it is an e-mail with an invoice attached.
In ThisOutlookSession
Private WithEvents InspGenSubj As Outlook.Inspectors
Private Sub Application_Startup()
Set InspGenSubj = Inspectors
End Sub
Private Sub InspGenSubj_NewInspector(ByVal Inspector As Inspector)
Dim curritem As Object
Set curritem = Inspector.currentItem
If curritem.Class = olMail Then
If curritem.Sent = False Then
If curritem.Subject = "Generated Mail subject" Then
MsgBox "Code to add attachment to " & curritem.Subject
End If
End If
End If
End Sub
Without knowing how the mail was generated I saved unsent mail to Drafts, closed then reopened.
Related
I want to autofill the BCC field with a specific address on replies, forwards and new emails.
I have seen a similar function that performs "silently" - i.e., the BCC address is added once the 'Send' button has been pressed.
I want to be able to remove/change the address if necessary.
From the user's perspective: click reply/forward/new email, and the message window opens up with the BCC field filled.
My knowledge of VBA is somewhat limited, so I'd appreciate if you could be specific about where to place the code.
This code put in ThisOutlookSession module will fire when you click on the button New Email and when replying to an email. From there it's simple to insert whatever you need in the various fields. You need to restart Outlook or manually call Application_Startup() to have it activated the first time.
Option Explicit
Public WithEvents myInspectors As Outlook.Inspectors
Public WithEvents myExplorer As Outlook.Explorer
Private Sub Application_Startup()
Set myInspectors = Application.Inspectors
Set myExplorer = Application.ActiveExplorer
End Sub
Private Sub myInspectors_NewInspector(ByVal Inspector As Inspector)
If TypeName(Inspector.CurrentItem) = "MailItem" Then
'MsgBox "new mail"
Inspector.CurrentItem.BCC = "joe.doe#domain.com"
End If
End Sub
Private Sub myExplorer_InlineResponse(ByVal Item As Object)
'MsgBox "reply"
Item.BCC = "jane.dane#domain.com"
End Sub
Why am I facing the below problem?
I have written a code to open sent email after sending email. The VBA code doesn't open the latest sent email but the previous one.
Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
Dim myItem As MailItem
Dim myNamespace As NameSpace
Dim myFolder As Folder
Set myNamespace = Application.GetNamespace("MAPI")
Set myFolder = myNamespace.GetDefaultFolder(olFolderSentMail)
Set myItem = myFolder.Items(myFolder.Items.Count)
myItem.Display
End Sub
This is the answer why this won't work with the Application_ItemSend event:
Application_ItemSend is called before the email is sent. You can see that because it has a Cancel parameter. This means the email only gets sent if Cancel = False else the email is dropped.
So you just cannot display the email because it isn't sent at this time in the Application_ItemSend event. It gets sent after the Application_ItemSend is finished.
Note: If you put a break point within the Application_ItemSend you will see that the "New Email" window remains open/visible until the ItemSend event is finished. Therefore you cannot open that email within that event.
Workaround
You might try the following code. This creates an event for the default folder of sent items which is called when an item is added to this folder.
A restart of Outlook might be needed after adding the code (or at least running the Application_Startup procedure once).
Option Explicit
Public WithEvents myOlItems As Outlook.Items
Private Sub Application_Startup()
Set myOlItems = Application.GetNamespace("MAPI").GetDefaultFolder(olFolderSentMail).Items
End Sub
Private Sub myOlItems_ItemAdd(ByVal Item As Object)
Item.Display
End Sub
If the folder where your sent items are moved to is not the default Outlook sent mail folder then you need to find the right folder to set it in Application_Startup.
I'm trying to automatically achieve this workflow:
when user opens a message draft in Outlook (a generated EML file)
if the subject matches a string (immutable, known beforehand, I can't change it; it's something like xyžřy, note the non-ASCII characters):
then add an e-mail to BCC field (immutable, known beforehand, valid e-mail address; let's say it's baz#example.com)
I already know the last part - how to add a BCC to a message, and I use InStr for matching:
Sub addbcc()
Dim objRecip As Recipient
Set oMsg = Application.ActiveInspector.CurrentItem
With oMsg
If InStr(1, oMsg.Subject, "xyžřy") > 0 Then
Set objRecip = oMsg.Recipients.Add("baz#example.com")
objRecip.Type = olBCC
objRecip.Resolve
End If
End With
Set oMsg = Nothing
End Sub
However, the user still needs to remember to press a button to run this macro, which is not more convenient than typing the BCC manually. Is it possible to run the macro automatically when this e-mail is opened?
Is it possible to run the macro automatically when this e-mail is opened?
Work with NewInspector Event , Events occurs when new window is opened by user or through your code.
Example
Option Explicit
Private WithEvents Inspectors As Outlook.Inspectors
Private Sub Application_Startup()
Initialize_handler
End Sub
Public Sub Initialize_handler()
Set Inspectors = Application.Inspectors
End Sub
Private Sub Inspectors_NewInspector(ByVal Inspector As Outlook.Inspector)
If Inspector.currentItem.Class = olMail Then
If Inspector.currentItem.Parent = "Drafts" Then ' Drafts Folder
Debug.Print Inspector.currentItem.Subject ' Immediate Window
' Call Your Code
' Inspector.currentItem.BCC = "baz#example.com"
End If
End If
End Sub
CurrentItem Property
You could monitor the drafts folder with ItemAdd. See the idea here for the inbox. How do I trigger a macro to run after a new mail is received in Outlook?
You could add the bcc in ItemSend. Outlook 2010 - VBA - Set bcc in ItemSend
I have a rule in Outlook which sends a daily email into a particular folder. I then have a VBA script which upon noticing a new unread message in that folder goes in and saves the attachment to a folder on my hard drive and does a few other formatting type things (on the attachment).
I then just linked up the script to the rule in the Outlook rules wizard so it runs as a package.
The problem is as follows: the script is kicked off BEFORE the message is sorted into the appropriate folder. In reality it should run after the message is sorted (otherwise there is nothing for it to act upon). Any ideas on how to rectify?
The code currently begins as follows:
sub saveattachment()
Should it be this instead?
private sub saveattachment()
or
public sub saveattachment()
Would it be better to have the "rule" embedded in the macro instead and then just run it as a private sub anytime the daily email appears in my Inbox?
If you need to assign a VBA macro sub to the Outlook rule, the VBA sub should look like the following one:
Public Sub Test(mail as MailItem)
' your code goes there
End Sub
An instance of the MailItem class is passed as a parameter and stands for the email arrived to the Inbox.
But in case if you need to be sure that your code is triggered when a mail is moved to a particular folder you need to handle the ItemAdd event of the Items class which comes from that folder. Be aware, the event is not fired when more than 16 items are added to the folder simultaneously.
Public WithEvents myOlItems As Outlook.Items
Public Sub Initialize_handler()
Set myOlItems = Application.GetNamespace("MAPI").GetDefaultFolder(olFolderContacts).Items
End Sub
Private Sub myOlItems_ItemAdd(ByVal Item As Object)
Dim myOlMItem As Outlook.MailItem
Dim myOlAtts As Outlook.Attachments
Set myOlMItem = myOlApp.CreateItem(olMailItem)
myOlMItem.Save
Set myOlAtts = myOlMItem.Attachments
' Add new contact to attachments in mail message
myOlAtts.Add Item, olByValue
myOlMItem.To = "Sales Team"
myOlMItem.Subject = "New contact"
myOlMItem.Send
End Sub
Finally, you may find the Getting Started with VBA in Outlook 2010 article helpful.
Just wondering if this is possible and if it is can someone assist me with?
In this scenario what we do is emails comes into shared folder. We will then have those email sorted.
After the sorting we will start putting emails into an approved folder. What I will like to do is have a VBA macro in outlook that will be able to generate a custom reply to all the emails in the approved folder.
For example if we place 5 emails in the folder and run a script it should send emails out to those 5 senders.
The email will be something generic such as "You are approved, please logout a "time".
I'd suggest starting from the Getting Started with VBA in Outlook 2010 article in MSDN. It explains the basics of programming VBA macros.
The ItemAdd event is fired when one or more items are added to the Items collection (i.e. folder). Be aware, the event is not fired when a large number of items are added to the folder at once.
So, you can handle the ItemAdd event of the approved folder to create and send a reply. The Reply method of Outlook items creates a reply, pre-addressed to the original sender, from the original message. The Send method sends the e-mail message. For example:
Public WithEvents myOlItems As Outlook.Items
Public Sub Initialize_handler()
Set myOlItems = Application.GetNamespace("MAPI").GetDefaultFolder(olFolderContacts).Items
End Sub
Private Sub myOlItems_ItemAdd(ByVal Item As Object)
Dim myOlMItem As Outlook.MailItem
Dim myOlAtts As Outlook.Attachments
Set myOlMItem = myOlApp.CreateItem(olMailItem)
myOlMItem.Save
Set myOlAtts = myOlMItem.Attachments
' Add new contact to attachments in mail message
myOlAtts.Add Item, olByValue
myOlMItem.To = "Sales Team"
myOlMItem.Subject = "New contact"
myOlMItem.Send
End Sub
Outlook reply to multiple emails on demand
Paste the following code in "ThisOutlookSession"
Outlook will automatically send a reply when you move Emails to "approved" folder
Option Explicit
'// items in the target folder to events
Dim WithEvents TargetFolderItems As Items
Private Sub Application_Startup()
Dim olNamespace As Outlook.NameSpace
Set olNamespace = Application.GetNamespace("MAPI")
Set TargetFolderItems = olNamespace.GetDefaultFolder(olFolderInbox) _
'// Set your folder here
.Folders.Item("approved").Items
End Sub
'// ItemAdd event code
Sub TargetFolderItems_ItemAdd(ByVal Item As Object)
Dim olReply As MailItem
Set olReply = Item.Reply
olReply.HTMLBody = "You are approved " & vbCrLf & olReply.HTMLBody
olReply.Send
Set TargetFolderItems = Nothing
Set olReply = Nothing
End Sub