OutlookForum member X82 needs to change the subject of replies.
Instead of the subject reading "RE: Info", I must change it to a pre-set sentence like Authorization 0054213. The number will change but the first part is always the same. Is there a way of making outlook change the subject line?
A quick but "dirty" method is to use an InputBox to select a number, which then inserts the correct subject. (The code for this is at Change subject)
It's also possible to do this using a ComboxBox, but its more complicated and requires a UserForm.
Before using the macro, you need to set Outlook's macro security to Low or to warn. Then open the VBA editor using Alt+F11. More information on using the VBA editor is at How to use the VBA Editor
For a variation of the code that contains a proper Cancel command, see cancel-subjects.txt. Follow the instructions below, but add a second command button to the Userform and change the caption to Cancel.
Step 1: Create the Userform.
- Right click on Project1 and select Insert > UserForm
- Open the control Toolbox and select a ComboBox and add it to the UserForm.
- Add a Command button.
- Right click on the Command button and choose Properties.
- Type OK (or Change Subject) in the Caption field.
- Right click on the UserForm and choose View Code.
- Paste the code below into the code window.
- Change the subject titles as desired. This list is for your reference only, not the actual text that will be added to the subject. The subject text is changed in the VBA macro code.
Private Sub UserForm_Initialize() With ComboBox1 .AddItem "Subject 1" .AddItem "Subject 2" .AddItem "Subject 3" .AddItem "Subject 4" .AddItem "Subject 5" .AddItem "no change" End With End Sub Private Sub CommandButton1_Click() lstNo = ComboBox1.ListIndex Unload Me End Sub
Step 2: Add VBA macro for reply
Next you need to add the macro that creates the reply then asks you to select the new subject.
- Right click on Project1 and choose Insert > Module.
- Paste the code below into the Module.
- Change the subject lines to use the desired text. If you want to include the original subject, use oMail.Subject = "Subject " & objMail.Subject format.
To test, select a mail item and run the macro. The macro works with a selected or open message, thanks to the use of the GetCurrentItem function.
Public lstNo As Long Public Sub ChangeSubjectOnReply() Dim objItem As Object Dim oMail As Outlook.MailItem Set objItem = GetCurrentItem() Set oMail = Application.ActiveExplorer.Selection(1).Reply oMail.Display UserForm1.Show ' MsgBox "user chose " & lstNo & " from combo" Select Case lstNo Case -1 oMail.Subject = objItem.Subject Case 0 oMail.Subject = "Subject 1" Case 1 oMail.Subject = "Subject 2" Case 2 oMail.Subject = "Subject 3" Case 3 oMail.Subject = "Subject 4" Case 4 oMail.Subject = "Subject 5" End Select End Sub Function GetCurrentItem() As Object Dim objApp As Outlook.Application Set objApp = Application On Error Resume Next Select Case TypeName(objApp.ActiveWindow) Case "Explorer" Set GetCurrentItem = objApp.ActiveExplorer.Selection.Item(1) Case "Inspector" Set GetCurrentItem = objApp.ActiveInspector.CurrentItem End Select Set objApp = Nothing End Function
Video Tutorial
Import a ready to use form
Because this is more complicated than just pasting a macro, I have ready to use code available for download. Import it into the VBA editor and it's ready to run. ChangeSubject VBA
More Information
The second part of X82's question was how to get a code from the message body and add it to the subject. To do this you need to find the code in the body. You can do this by parsing the body and looking for a string. Basic instructions are at Parse text fields using VBA. For best results, it needs to be in the body in the same location, eg, as "Authorization: 123456789". Once you find the code and assign it to a string value, enter it into the subject using this format:
oMail.Subject = "Authorization " & strCode
VBA UserForm sample: Select from a list of templates
Hi Diane,
This script is similar with what I would need and It would be great if you could help me out to adapt it to my need.
I will have an open email that I intend to forward (manually click send) to a certain email address, always the same one, but the subject line differs for each email. I want to enter a certain number in the combo box, and a certain subject to be auto-populated. Could you please help me with codding for this one?
Would be much appreciated!
Thank you!
How to use dynamic subject list?
Hi, I have used this code to setup a macro to add subject from a drop down. As per the example it works by selecting a contract and running the macro which works fine, however I would like to also be able to select a contact group to populate the To. It does not work when I do try to do this, is there a way I can make it work for a contract group?
Thanks
Hi Diane,
If I will place the value I have from the combobox on the email body, how am I going to format it (font size, font style, bold)?
Thank you
Diane,
I'm trying to use the combobox to allow user to select between a list of carriers,
like "Carrier 1", "Carrier 2" and "Carrier 3", and based in the selection, the macro
populates the "To" field in the email with the respective emails for the carrier.
Is that possible?
Sure. In the combo box, you put the carrier names and in the Select case, you'd use oMail.To = "alias@address".
Dear Diane,
Thanks a lot for the effort.
This is the one I am looking for a long time.
I would like to add some more things for my project. Please tell me how to set up hotkey(ALT+T) to run a macro in OUTLOOK 2013.
Looking forward for your reply
Thanks
Jerald Nicholas
Hi Diane,
Thanks for this tutorial, I have this working with some minor modifications. I wanted to ask, is there any way to launch the macro when creating a new email in outlook? Instead of going through the developer tab ever time, I'd like to macro to run and prompt me for the subject line whenever I create a new email. Will be great if you can advise on this.
Thank you,
Ash
As long as the macro is one you run (not an automatic macro that runs when something happens) you can create a button on the ribbon for it. See https://www.slipstick.com/developer/how-to-use-outlooks-vba-editor/#button if you aren't sure how to do it.
Hi Diane,
Thanks for this tutorial, I got this working pretty well with some modifications. I wanted to ask, is there any way to trigger this macro anytime a new email is created in outlook. Meaning I'd want the form where I select the subject lines to appear when I click "New Mail Message", instead of going through the developer tab ever time.
Thanks,
Ash
You need to use the newinspector - see https://www.slipstick.com/developer/code-samples/default-subject-messages/ for an example. I think (did not test) you'd add UserForm1.Show and select case lines to the newinspector macro.
This is working here -
Declare this in a new module (if you have other modules already in use, put it at the top of one)
Public lstNo As Long
Use the macros at the page i linked to and replace the inspector active macro with this
Private Sub m_Inspector_Activate()
Dim strSubject as String
Select Case lstNo
Case -1
strSubject = objItem.Subject
Case 0
strSubject = "Subject 1"
Case 1
strSubject = "Subject 2"
Case 2
strSubject = "Subject 3"
Case 3
strSubject = "Subject 4"
Case 4
strSubject = "Subject 5"
End Select
If TypeName(m_Inspector.currentItem) = "MailItem" And _
m_Inspector.currentItem.Subject = "" Then
m_Inspector.currentItem.Subject = strSubject
End If
Set m_Inspector = Nothing
End Sub