+ Reply to Thread
Results 1 to 4 of 4

changing date in email subject line with vba

Hybrid View

  1. #1
    Registered User
    Join Date
    12-02-2012
    Location
    Australia
    MS-Off Ver
    Excel 2007
    Posts
    51

    changing date in email subject line with vba

    Hi people.

    Guys, i need help with adding a date to the subject line in outlook. basically when I click a button in excel I would like a populated email come up. But what I need to do is have the subject heading date change so it has the previous months date. ie November 2012. Could someone please show me how to do this?

     Sub runMail()
    
    Call OutlookMailSender
    
    End Sub
    
    Sub OutlookMailSender(Optional attachment)
    
    Dim objOutlookMsg As Outlook.MailItem
    Dim objOutlookRecip As Outlook.recipient
    Dim objOutlookAttach As Outlook.attachment
    Dim bodytext As String
    Dim recipient As String
    Dim mBefore As Date
    
    
    bodytext = "Hi " & vbNewLine & vbNewLine & vbNewLine & _
    "This email was sent by Excel automation"
    
    recipient = "[email protected]"
        
    
    Set objOutlook = CreateObject("Outlook.Application")
    
    
    Set objOutlookMsg = objOutlook.CreateItem(olMailItem)
    
    With objOutlookMsg
    
    Set objOutlookRecip = .Recipients.Add(recipient)
    objOutlookRecip.Type = olTo
    
    
    Set objOutlookRecip = .Recipients.Add("[email protected]")
    objOutlookRecip.Type = olCC
    
    
    Set objOutlookRecip = .Recipients.Add("[email protected]")
    objOutlookRecip.Type = olBCC
    
    
    .Subject = "HELLO"
    .Body = bodytext
    .Importance = olImportanceHigh
    
    
    If Not IsMissing(attachment) Then
    Set objOutlookAttach = .Attachments.Add(attachment)
    End If
    
    For Each objOutlookRecip In .Recipients
    
    If Not objOutlookRecip.Resolve Then MsgBox "Could not resolve the email for " & objOutlookRecip
    Next
    
    If True Then
    .Display
    Else
    
    .Save
    .Send
    End If
    
    End With
    
    Set objOutlook = Nothing
    
    
    End Sub
    Last edited by ConfusedaboutVBA; 12-09-2012 at 06:35 AM.

  2. #2
    Forum Expert martindwilson's Avatar
    Join Date
    06-23-2007
    Location
    London,England
    MS-Off Ver
    office 97 ,2007
    Posts
    19,320

    Re: changing date in email subject line with vba

    Your post does not comply with Rule 3 of our Forum RULES. Use code tags around code. Posting code without them makes your code hard to read and difficult to be copied for testing. Highlight your code and click the # at the top of your post window. For more information about these and other tags, found here
    "Unless otherwise stated all my comments are directed at OP"

    Mojito connoisseur and now happily retired
    where does code go ?
    look here
    how to insert code

    how to enter array formula

    why use -- in sumproduct
    recommended reading
    wiki Mojito

    how to say no convincingly

    most important thing you need
    Martin Wilson: SPV
    and RSMBC

  3. #3
    Registered User
    Join Date
    12-02-2012
    Location
    Australia
    MS-Off Ver
    Excel 2007
    Posts
    51

    Re: changing date in email subject line with vba

    Thank gosh you were here to notify me of this crisis.

    Code has now been fixed, crisis aborted.

    Keep up the good work champ!

  4. #4
    Forum Expert martindwilson's Avatar
    Join Date
    06-23-2007
    Location
    London,England
    MS-Off Ver
    office 97 ,2007
    Posts
    19,320

    Re: changing date in email subject line with vba

    ha ha not my rule mate but if a mod had seen it they would probably forbidden any replies until you did.
    .Subject = "HELLO"
    looks to be the bit
    so maybe
    Sub runMail()
    
    Call OutlookMailSender
    
    End Sub
    
    Sub OutlookMailSender(Optional attachment)
    
    Dim objOutlookMsg As Outlook.MailItem
    Dim objOutlookRecip As Outlook.recipient
    Dim objOutlookAttach As Outlook.attachment
    Dim bodytext As String
    Dim recipient As String
    Dim mBefore As Date
    lastmonth = Format(DateAdd("m", -1, Now()), "yyyy-mmmm")'change format to your prefered layout
    
    bodytext = "Hi " & vbNewLine & vbNewLine & vbNewLine & _
    "This email was sent by Excel automation"
    
    recipient = "[email protected]"
        
    
    Set objOutlook = CreateObject("Outlook.Application")
    
    
    Set objOutlookMsg = objOutlook.CreateItem(olMailItem)
    
    With objOutlookMsg
    
    Set objOutlookRecip = .Recipients.Add(recipient)
    objOutlookRecip.Type = olTo
    
    
    Set objOutlookRecip = .Recipients.Add("[email protected]")
    objOutlookRecip.Type = olCC
    
    
    Set objOutlookRecip = .Recipients.Add("[email protected]")
    objOutlookRecip.Type = olBCC
    
    
    .Subject = "HELLO" & lastmonth 'or just lastmonth without the hello
    .Body = bodytext
    .Importance = olImportanceHigh
    
    
    If Not IsMissing(attachment) Then
    Set objOutlookAttach = .Attachments.Add(attachment)
    End If
    
    For Each objOutlookRecip In .Recipients
    
    If Not objOutlookRecip.Resolve Then MsgBox "Could not resolve the email for " & objOutlookRecip
    Next
    
    If True Then
    .Display
    Else
    
    .Save
    .Send
    End If
    
    End With
    
    Set objOutlook = Nothing
    
    
    End Sub

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1