Outlook VBA

ritagail

New Member
Joined
Jul 28, 2009
Messages
24
I have never written VBA code from 'scratch' so needless to say I am a novice at the most. I have manipulated VBA code in Excel after I have recorded it but Outlook does not have the record function. So, I NEED HELP! lol. :rolleyes:

I need a piece of code that would go to the subject line of any open email and insert todays (the current date) at the end of the subject.

Is this possible?

Any help greatly appreciated!
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
You could probably do it with https://www.autohotkey.com/

The VBA method would be: newItem.Subject = newItem.Subject & " " & Format(Now, "yyyy-mm-dd").

But I'm not sure how you'd apply it automatically, as I don't really use VBA in Outlook, so I'm not familiar with its methods.
 
Upvote 0
here is something I wrote years ago to prefix the date to a subject of email that were currently selected along with a routine to undo the changes, it was easier to prefix with the date for two reasons, firstly it sorted better in chronological order and also easier to remove as we used a fixed length date and time string

Code:
'>>from here
' Author    Jim Ward
' Creation  31st March 2004
'
' Description
'
' The following 2 routines allow the user to select a number of emails
' For each member of the selection prefix the subject with the received date and time
' in the format yyyymmdd_hhmm_<subject content>
'
' This allows for the messages to be filed outwith OUTLOOK as message files, when OUTLOOK
' saves these it uses the <SUBJECT> field for the filename. In introducing the date, we can
' now use the directory sort to get the messages in chronological order, without this the
' messages will be in the order of when they were saved to the drive, NOT the received date.
'
'
' The second routine takes a date stamped SUBJECT email and removes the date stamp.
'
' Modification History
' ====================
' Jim Ward
' 27th May 2004
' Added in logic to stop untagged subjects being unintentionally stripped of characters(1-14)
' checking that characters (1-8) are numeric and positions 9 and 14 are underscore
'
'
'
Sub PrefixSubjectWithDate()
'
' Declare variables
'
Dim myOlSel As Outlook.Selection
Dim olExp, olCurrentFolder
Dim X As Integer
'
' setup what we require
'
Set olExp = Outlook.ActiveExplorer
Set olCurrentFolder = olExp.CurrentFolder
Set myOlSel = olExp.Selection
'
' Process each message selected
' Modify the subject In the message, And save the changes
'
For X = 1 To myOlSel.count
    thissubject = myOlSel.Item(X).Subject
    thisdate = myOlSel.Item(X).ReceivedTime
    newdate = Format(thisdate, "yyyymmdd_hhmm_")
    myOlSel.Item(X).Subject = newdate & thissubject
    myOlSel.Item(X).Save
Next X

End Sub
Sub UndoPrefixSubjectWithDate()
Dim myOlSel As Outlook.Selection
Dim olExp, olCurrentFolder
Dim X As Integer

Set olExp = Outlook.ActiveExplorer
Set olCurrentFolder = olExp.CurrentFolder
Set myOlSel = olExp.Selection
'
' modify the subject In the message, And save the changes
'
' Check that we have some resemblance to our date stamp string to stop users
' unwittingly removing untagged messages.
'
' We should have an underscore in positions 9 and 14 and
' numeric data in 1-8 and 10-13
'
For X = 1 To myOlSel.count
    thissubject = myOlSel.Item(X).Subject

    dash1 = InStr(1, thissubject, "_")
    dash2 = 0
    If dash1 = 9 Then
        dash2 = InStr(dash1 + 1, thissubject, "_")
        mytest = Mid(thissubject, dash1 + 1, 4)
        If dash2 = 14 And IsNumeric(Left(thissubject, 8)) = True And IsNumeric(Mid(thissubject, dash + 1, 4)) = True Then
            newSubject = Right(thissubject, Len(thissubject) - 14)
            myOlSel.Item(X).Subject = newSubject
            myOlSel.Item(X).Save
        End If
    End If
Next X

End Sub
'>>to here
 
Upvote 0

Forum statistics

Threads
1,214,924
Messages
6,122,294
Members
449,077
Latest member
Rkmenon

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top