Consulting

Results 1 to 5 of 5

Thread: Outlook macro with today's date in subject and paste clipboard in body

  1. #1
    VBAX Newbie
    Joined
    Mar 2017
    Posts
    3
    Location

    Outlook macro with today's date in subject and paste clipboard in body

    Hi Everyone,

    Very new to VBA. I write similar emails everyday to the same email address. I needed some help to automate it using VBA macros.

    Basically, I want:

    To: xyz at xyz com

    Subject: 'TEXT' & todays date & the sent email counter(if possible)

    Body: Paste Clipboard item

    Send
    Can somebody please help me with this?
    Thanks in advance.

  2. #2
    Where is the clipboard item copied from?
    What does the sent mail counter count? All the mails you have ever sent? The messages you have sent today? Some other count?
    What does TEXT represent in the subject?
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  3. #3
    VBAX Newbie
    Joined
    Mar 2017
    Posts
    3
    Location
    Quote Originally Posted by gmayor View Post
    Where is the clipboard item copied from?
    What does the sent mail counter count? All the mails you have ever sent? The messages you have sent today? Some other count?
    What does TEXT represent in the subject?
    Clipboard item is copied from a text file.
    Sent mail counter is for the messages I have sent today.
    TEXT just represents the same sentence I put in subjects of all the emails.

    Thank you for your reply.

  4. #4
    OK try the following

    Option Explicit
    
    Sub CreateMessageFromClipboard()
    Dim olEmail As Outlook.MailItem
    Dim olItem As MailItem
    Dim olInsp As Outlook.Inspector
    Dim wdDoc As Object
    Dim oRng As Object
    Dim olitems As Items
    Dim lngCount As Long: lngCount = 0
    Const strSubject As String = "Fixed Text" ' This is the fixed text for the subject
    Const strRecipient As String = "someone@somewhere.com"
    
    
        Set olitems = Session.GetDefaultFolder(olFolderSentMail).Items
        olitems.Sort "[Received]", True
        For Each olItem In olitems
            If Format(olItem.SentOn, "yyyymmdd") = Format(Date, "yyyymmdd") Then
                lngCount = lngCount + 1
            Else
                Exit For
            End If
            DoEvents
        Next olItem
        On Error Resume Next
        Set olEmail = CreateItem(olMailItem)
        With olEmail
            .To = strRecipient
            .Subject = strSubject & Chr(32) & Format(Date, "dd/mm/yyyy") & " (" & lngCount & ")"
            .BodyFormat = olFormatHTML
            .Display
            Set olInsp = .GetInspector
            Set wdDoc = olInsp.WordEditor
            Set oRng = wdDoc.Range(0, 0)
            oRng.PasteSpecial DataType:=2
        End With
    lbl_Exit:
        Set olEmail = Nothing
        Set olInsp = Nothing
        Set olitems = Nothing
        Set wdDoc = Nothing
        Set oRng = Nothing
        Exit Sub
    End Sub
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  5. #5
    VBAX Newbie
    Joined
    Mar 2017
    Posts
    3
    Location
    Quote Originally Posted by gmayor View Post
    OK try the following

    Option Explicit
    
    Sub CreateMessageFromClipboard()
    Dim olEmail As Outlook.MailItem
    Dim olItem As MailItem
    Dim olInsp As Outlook.Inspector
    Dim wdDoc As Object
    Dim oRng As Object
    Dim olitems As Items
    Dim lngCount As Long: lngCount = 0
    Const strSubject As String = "
    
    
        Set olitems = Session.GetDefaultFolder(olFolderSentMail).Items
        olitems.Sort "[Received]", True
        For Each olItem In olitems
            If Format(olItem.SentOn, "yyyymmdd") = Format(Date, "yyyymmdd") Then
                lngCount = lngCount + 1
            Else
                Exit For
            End If
            DoEvents
        Next olItem
        On Error Resume Next
        Set olEmail = CreateItem(olMailItem)
        With olEmail
            .To = strRecipient
            .Subject = strSubject & Chr(32) & Format(Date, "dd/mm/yyyy") & " (" & lngCount & ")"
            .BodyFormat = olFormatHTML
            .Display
            Set olInsp = .GetInspector
            Set wdDoc = olInsp.WordEditor
            Set oRng = wdDoc.Range(0, 0)
            oRng.PasteSpecial DataType:=2
        End With
    lbl_Exit:
        Set olEmail = Nothing
        Set olInsp = Nothing
        Set olitems = Nothing
        Set wdDoc = Nothing
        Set oRng = Nothing
        Exit Sub
    End Sub
    Thank you so much for your help. Sorry couldn't back to you, had to go overseas. I just tried the code and it works like a charm. Thank you so so much. You are a legend!!!!

Tags for this Thread

Posting Permissions

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