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
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
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