You can use a VBA macro to create a new message and preset any of the fields, including To/CC/BCC, the subject, expiration date, flags, voting options and more.
To use, use Alt+F11 to open the VBA editor and paste the following code into ThisOutlookSession. Remove the fields you don't want to set and edit the values in the fields you want to set automatically.
Add the macro to a toolbar or ribbon button or to the QAT.
Public Sub CreateNewMessage() Dim objMsg As MailItem Set objMsg = Application.CreateItem(olMailItem) With objMsg .To = "Alias@domain.com" .CC= "Alias2@domain.com" .BCC = "Alias3@domain.com" .Subject = "This is the subject" .Categories = "Test" .VotingOptions = "Yes;No;Maybe;" .BodyFormat = olFormatPlain ' send plain text message .Importance = olImportanceHigh .Sensitivity = olConfidential .Attachments.Add ("path-to-file.docx") ' Calculate a date using DateAdd or enter an explicit date .ExpiryTime = DateAdd("m", 6, Now) '6 months from now .DeferredDeliveryTime = #8/1/2012 6:00:00 PM# .Display End With Set objMsg = Nothing End Sub
Send a new message to From address of selected messages
You can easily tweak the macro above to loop through a selection of messages and send a new message to the senders.
To use, select one or more messages then run the macro. As written, it opens the messages so you can review them and send yourself. You can change .display to .Send if you want to send them automatically. (Use .display when testing.)
Public Sub CreateNewMessage() Dim objMsg As MailItem Dim Selection As Selection Dim obj As Object Set Selection = ActiveExplorer.Selection For Each obj In Selection Set objMsg = Application.CreateItem(olMailItem) With objMsg .To = obj.SenderEmailAddress .Subject = "This is the subject" .Categories = "Test" .Body = "My notes" & vbcrlf & vbcrlf & obj.Body .Display ' use .Send to send it automatically End With Set objMsg = Nothing Next End Sub
Create a new contact with some fields filled in
You can use the same method with other Outlook items. This example creates a new contact with a country and city and part of phone number.
Use TaskItem and olTaskItem for Tasks, AppointmentItem and olAppointmentItem for appointments. You'll need to replace the fields with the correct properties for the item type. You can get the property names from VBA Help or at MSDN.
Public Sub CreateNewContact() Dim objContact As ContactItem Set objContact = Application.CreateItem(olContactItem) With objContact .BusinessAddressCity = "Halifax" .BusinessAddressCountry = "Canada" .Business2TelephoneNumber = "902123" 'the area code and local prefix .Display End With Set objContact = Nothing End Sub
Create a new Appointment
This macro creates a new appointment with the Location field filled in. Other fields can be added to it and if you need an meeting, click Invite attendees on the ribbon.
Sub CreateApptLocation() Dim olAppt As AppointmentItem Set olAppt = Application.CreateItem(olAppointmentItem) With olAppt .Subject = "My Subject" .Location = "My Favorite place" .Categories = "Business" .Display End With End Sub
How to use a variable to assign like .to = %var%? Is this possible?
Thanks for this great post. Wondering if you can help - I receive emails with the subject and attachments as i want them (from our accounting software). I am wondering if i could have a macro set to a button that
a) opens a forward draft (so i can add the clients name)
b) adds certain text to the body of the draft (generally the same on each one)
Even better would be to open a NEW email and copy across the attachments and subject of the email i get from the system so that i dont have to delete traces of initial email.
Thank you very much for this article! It helped me to create new email object where I can copy extracted text from multiple emails into it.
I want to modify my .To and .Cc based on the following conditions:
.To : Ops MANAGER
.Cc: Managers(under him)
I have the following columns with me:
Ops Manager 1 Manager 1
Ops Manager 1 Manager 2
Ops Manager 2 Manager 3
Ops Manager 2 Manager 4
Ops Manager 3 Manager 5
.To : Ops Manager 1
.Cc : Manager 1, Manager 2
For things like that, i recommend using multiple macros - ones with the addresses as strings that calls the main macro and passes the values.
hi, didyou get the answer?
To do something like this, where it is dynamic and the To changes? You would need to use multiple macros - the macro needs to add the addresses before opening.
You can use a "stub macro" that sets the values and then calls the main one -
I'd like to create e-mails automatically sent out using the following criteria:
If end of the month is Monday or Thursday.
Is it possible somehow?
Would be great if it also worked when Outlook is not opened.
thanks in advance,
Sending with outlook closed would require powershell (and it would open outlook).
You're wanting to send if the 30th is a mon or thurs? (or 31 or 28/29) I would check to see if date + 1 = 1 then check if today was mon or thurs.
Something like DateSerial(Month(Date) + 1) = 1
then check the day name:
WeekdayName(Weekday(Now())) = "Monday" Or WeekdayName(Weekday(Now())) = "Thursday" Then
I have a macro that should point you in the right direction at
Although I am not a newb at programming here is a funny story and warning too. If you try all this send mail code, do not test it with the email account Outlook is set too or you will spend a week and many hours sifting through the internet trying to figure out why the mail is being sent to the outbox and not actually sending the email, there is no help on that. I accidentally figured it out myself when I sent some mail not via code to the wrong address, namely my own. It went to the outbox, I was like WTF, all that time I spent trying to figure it out. It would have been nice if there was some documentation provided saying "hey, if you send mail to yourself it will go to the outbox". I hope you had fun laughing :)
>> "hey, if you send mail to yourself it will go to the outbox".
If the code sends the message, it should send it and come back to the inbox :)
Some of the macro samples display the messages instead of actually sending them - this is so you can see what they look like without actually spamming someone (my sample addresses) or yourself.
Hi, I'm learning how to write Microsoft outlook VBA code from scratch. Where should I able begein to write code? Thanks!
Unlike the other office apps, Outlook doesn't have a macro recorder, so you need to write it yourself or work with macros you find online. You'll write / edit it in the VBA editor.
This would be to send a file to a sender after he/she sends or texts "send file 400".
sure. i don't have a sample for this specific scenario, but i have one that creates appointments that gives you an idea how to do it.