none
VB: macro in Outlook to copy the contain of a sent email with the timestamp RRS feed

  • Question

  • Hi all,

    I'd like to create a macro in Outlook but I have to admit I don't really know VB...

    Many times the day, I have to send an email and paste the contain with timestamp in a log file.

    The steps are:
    1. send the email
    2. go to the "sent items" folder
    3. select the last mail sent
    4. click forward
    5. edit it from the beginning of the timestamp to the end
    6. copy
    7. close the close
    The simplest way for me to do it would be to have "send and copy" button beside the "send" button which would do it automatically.

    I am sorry to ask the whole code but I don't even know how to select the "sent folder".

    Thanks in advance for your help on it.

    Max
    Friday, May 23, 2008 1:50 PM

All replies

  • Hello Max,

     

    I write down the following macro for your reference. You can paste it into the ThisOutlookSession in Outlook VBE (Alt + F11)

     

    Code Snippet

    Dim WithEvents colSentItems As Items

    Private Sub Application_Startup()
       Dim NS As Outlook.NameSpace
       Set NS = Application.GetNamespace("MAPI")
       Set colSentItems = NS.GetDefaultFolder(olFolderSentMail).Items
       Set NS = Nothing
    End Sub

    Private Sub colSentItems_ItemAdd(ByVal Item As Object)
        If Item.Class = olMail Then
            Dim mItem As MailItem
            Dim fItem As MailItem
            Set mItem = Item
            Set fItem = mItem.Forward
            MsgBox fItem.Body
        End If
    End Sub

     

     

    In this example, I register Application_Startup() event so that this piece of macro (ItemAdd) automatically takes effect when Outlook is started up.

     

    colSentItems_ItemAdd will be triggered when a new item is added into "Sent Item" folder, e.g. when an email or an appointment is sent.

     

    The line "If Item.Class = olMail Then" is used to distinguish the mail items from other outlook items, such as appointment, contact, etc.

     

    To get the forward email, I called mItem.Forward. After then, we can retrieve the body of the forward email item by calling fItem.Body. In this example, I simply pops up the message body. You may consider storing the body string to your own log file.

     

    Also note that, when you test my macro, please first mouse-click the Application_Startup and press F5 to run this function. This is necessary for the first test because the outlook has already been started.

     

    Let me know if you have any other concerns or questions.

     

    Regards,

    Jialiang Ge

     

    Tuesday, May 27, 2008 4:13 AM
    Moderator
  • Hello Max,

     

    I write down the following macro for your reference. You can paste it into the ThisOutlookSession in Outlook VBE (Alt + F11)

     

     

    Code Snippet

    Dim WithEvents colSentItems As Items

    Private Sub Application_Startup()
       Dim NS As Outlook.NameSpace
       Set NS = Application.GetNamespace("MAPI")
       Set colSentItems = NS.GetDefaultFolder(olFolderSentMail).Items
       Set NS = Nothing
    End Sub

    Private Sub colSentItems_ItemAdd(ByVal Item As Object)
        If Item.Class = olMail Then
            Dim mItem As MailItem
            Dim fItem As MailItem
            Set mItem = Item
            Set fItem = mItem.Forward
            MsgBox fItem.Body
        End If
    End Sub

     

     

     

     

    In this example, I register Application_Startup() event so that this piece of macro (ItemAdd) automatically takes effect when Outlook is started up.

     

    colSentItems_ItemAdd will be triggered when a new item is added into "Sent Item" folder, e.g. when an email or an appointment is sent.

     

    The line "If Item.Class = olMail Then" is used to distinguish the mail items from other outlook items, such as appointment, contact, etc.

     

    To get the forward email, I called mItem.Forward. After then, we can retrieve the body of the forward email item by calling fItem.Body. In this example, I simply pops up the message body. You may consider storing the body string to your own log file.

     

    Also note that, when you test my macro, please first mouse-click the Application_Startup and press F5 to run this function. This is necessary for the first test because the outlook has already been started.

     

    Let me know if you have any other concerns or questions.

     

    Regards,

    Jialiang Ge

     


    Dear Moderator,
    I need your help on this urgent matter, I would like to create a macro for Outlook to run from specific folder (e.g "Inbox", deleted items, "myimportantemails" etc) and then click on the macro and grab "Email address and Full name if available".
    This will get me going until I could learn more about VB Macros for Outlook/
    Thanks in advance!
    PIPO
    PDyer
    Friday, November 6, 2009 3:14 AM