Consulting

Results 1 to 5 of 5

Thread: Creating VBA-Macro for new custom message

  1. #1

    Creating VBA-Macro for new custom message

    Currently I am trying to understand how to build a custom macro-button into our Outlook 2010 / Outlook 2013 / Outlook 2016 enviroment.

    What I want to achive:
    When I get an email I want to be able to mark it and then click the Macro-icon of this script, and it should do the following:
    Create an new email
    Set the recipient in the email to a pre-defined e-mail adress
    Set an pre-defined subject
    Attach the marked email as an attachment (cannot be an forwarded email)
    Send the email automatically

    A bonus feature would be if I could mark multiple emails also when that occurs, and do the same thing with all the marked emails as attachments.
    Sending them all in the same email is OK in that case.

    Anyone got a clue on how I can achive this?

  2. #2
    The following macro will do all of that. Ensure that you change the three lines at the top for the fixed values before running the macro.
    Option Explicit
    Sub SendOnMessageasAttachment()
    Dim olItem As Outlook.MailItem
    Dim olOutMail As Outlook.MailItem
    Dim olInsp As Outlook.Inspector
    Dim wdDoc As Object
    Dim oRng As Object
    
    Const sAddr As String = "someone@somewhere.com"    ' the recipient
    Const sSubj As String = "This is the message subject"
    Const sText As String = "This is the covering message text"
    
        If Application.ActiveExplorer.Selection.Count = 0 Then
            MsgBox "No Items selected!", vbCritical, "Error"
            Exit Sub
        End If
    
        Set olOutMail = CreateItem(olMailItem)
        With olOutMail
            .To = sAddr
            .subject = sSubj
            For Each olItem In Application.ActiveExplorer.Selection
                .Attachments.Add olItem
            Next olItem
            Set olInsp = .GetInspector
            Set wdDoc = olInsp.WordEditor
            Set oRng = wdDoc.Range(0, 0)
            oRng.Text = sText
            .Display 'This line essential
            '.Send 'Remove the apostrophe from the start of the line after testing.
        End With
        Set olInsp = Nothing
        Set wdDoc = Nothing
        Set oRng = Nothing
    lbl_Exit:
        Set olItem = Nothing
        Set olOutMail = Nothing
        Exit Sub
    End Sub
    Last edited by gmayor; 04-27-2016 at 10:32 PM.
    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
    Nice! Faster than ever - and 100% correct!! I'm impressed!

    Just a question, if I would like the Subject in the email looking something like this:
    This is the message subject <date>

    And where date should be "today's" date... how can I add that?

  4. #4
    Change the following line
    .subject = sSubj
    to
    .subject = sSubj & " <" & Format(Date, "Short Date") & ">"
    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
    Thank you very much gmayor!! Works 100%

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
  •