Microsoft Excel

Ron de Bruin
Excel Automation

Microsoft MVP Program

Mail Range or Selection with Outlook 2011

The code on this page is tested in Lion OS X  and up with Excel 2011 and create the mail in Outlook 2011.

Read this page for Tips:  Tips for changing the Mac Outlook mail examples

Because there are a few bugs in VBA SendMail in Excel and there is no Outlook object model in Outlook 2011 like there is in Outlook for Windows we Run a AppleScript string with the built-in VBA MacScript function in the mail examples to get the same or better result.

Note: To run the mail examples you must copy the macro of your choice and also two functions named MailFromMacWithOutlook and KillFileOnMac that you find on the bottom of this page. If you want to test more then one macro you only have to copy the two functions one time.

 

Example 1

The following subroutine sends a newly created workbook with just the visible cells
in the Range("A1:K50").The cells will be PasteSpecial as values in the workbook you send.

It is saving the workbook before mailing it with a date/time stamp.
After the file is sent the workbook will be deleted from your hard disk or move to the Trash.

Change the mail address in the macro before you run it, if you want to use multiple recipients separate them with an ,

Sub Mail_Range_In_Excel2011()
'For Excel 2011 for the Mac and Outlook 2011
    Dim Source As Range
    Dim Destwb As Workbook
    Dim wb As Workbook
    Dim TempFilePath As String
    Dim TempFileName As String
    Dim FileExtStr As String
    Dim FileFormatNum As Long

    If Val(Application.Version) < 14 Then Exit Sub

    Set Source = Nothing
    On Error Resume Next
    Set Source = Range("A1:K50").SpecialCells(xlCellTypeVisible)
    On Error GoTo 0

    If Source Is Nothing Then
        MsgBox "The source is not a range or the sheet is protected, " & _
               "please correct and try again.", vbOKOnly
        Exit Sub
    End If

    With Application
        .ScreenUpdating = False
        .EnableEvents = False
    End With

    Set wb = ActiveWorkbook
    Set Destwb = Workbooks.Add(xlWBATWorksheet)

    Source.Copy
    With Destwb.Sheets(1)
        .Cells(1).PasteSpecial Paste:=8
        .Cells(1).PasteSpecial Paste:=xlPasteValues
        .Cells(1).PasteSpecial Paste:=xlPasteFormats
        .Cells(1).Select
        Application.CutCopyMode = False
    End With

    'Save format and extension
    FileExtStr = ".xlsx": FileFormatNum = 52

    'Or if you want it in xls format, use:
    'FileExtStr = ".xls": FileFormatNum = 57


    'Save the new workbook/Mail it/Delete it
    'If you want to change the file name then change only TempFileName
    TempFilePath = MacScript("return (path to documents folder) as string")
    TempFileName = "Range of " & wb.Name & " " & Format(Now, "dd-mmm-yy h-mm-ss")

    With Destwb
        .SaveAs TempFilePath & TempFileName & FileExtStr, FileFormat:=FileFormatNum
        MailFromMacwithOutlook bodycontent:="Hi there", _
                    mailsubject:="Mail Range Test", _
                    toaddress:="ron@debruin.nl", _
                    ccaddress:="", _
                    bccaddress:="", _
                    attachment:=.FullName, _
                    displaymail:=False
        .Close SaveChanges:=False
    End With

    KillFileOnMac TempFilePath & TempFileName & FileExtStr

    With Application
        .ScreenUpdating = True
        .EnableEvents = True
    End With
End Sub

 

Example 2

The following subroutine sends a newly created workbook with just the visible cells
in the Selection. The cells will be PasteSpecial as values in the workbook you send.

It is saving the workbook before mailing it with a date/time stamp.
After the file is sent the workbook will be deleted from your hard disk.

Change the mail address in the macro before you run it, if you want to use multiple recipients separate them with an ,

Sub Mail_Selection_In_Excel2011()
'For Excel 2011 for the Mac and Outlook 2011
    Dim Source As Range
    Dim Destwb As Workbook
    Dim wb As Workbook
    Dim TempFilePath As String
    Dim TempFileName As String
    Dim FileExtStr As String
    Dim FileFormatNum As Long

    If Val(Application.Version) < 14 Then Exit Sub

    Set Source = Nothing
    On Error Resume Next
    Set Source = Selection.SpecialCells(xlCellTypeVisible)
    On Error GoTo 0

    If Source Is Nothing Then
        MsgBox "The source is not a range or the sheet is protected, " & _
               "please correct and try again.", vbOKOnly
        Exit Sub
    End If

    If ActiveWindow.SelectedSheets.Count > 1 Or _
       Selection.Cells.Count = 1 Or _
       Selection.Areas.Count > 1 Then
        MsgBox "An Error occurred :" & vbNewLine & vbNewLine & _
               "You have more than one sheet selected." & vbNewLine & _
               "You only selected one cell." & vbNewLine & _
               "You selected more than one area." & vbNewLine & vbNewLine & _
               "Please correct and try again.", vbOKOnly
        Exit Sub
    End If

    With Application
        .ScreenUpdating = False
        .EnableEvents = False
    End With

    Set wb = ActiveWorkbook
    Set Destwb = Workbooks.Add(xlWBATWorksheet)

    Source.Copy
    With Destwb.Sheets(1)
        .Cells(1).PasteSpecial Paste:=8
        .Cells(1).PasteSpecial Paste:=xlPasteValues
        .Cells(1).PasteSpecial Paste:=xlPasteFormats
        .Cells(1).Select
        Application.CutCopyMode = False
    End With

    'Save format and extension
    FileExtStr = ".xlsx": FileFormatNum = 52

    'Or if you want it in xls format, use:
    'FileExtStr = ".xls": FileFormatNum = 57


    'Save the new workbook/Mail it/Delete it
    'If you want to change the file name then change only TempFileName
    TempFilePath = MacScript("return (path to documents folder) as string")
    TempFileName = "Selection of " & wb.Name & " " & Format(Now, "dd-mmm-yy h-mm-ss")

    With Destwb
        .SaveAs TempFilePath & TempFileName & FileExtStr, FileFormat:=FileFormatNum
        MailFromMacwithOutlook bodycontent:="Hi there", _
                    mailsubject:="Mail Selection Test", _
                    toaddress:="ron@debruin.nl", _
                    ccaddress:="", _
                    bccaddress:="", _
                    attachment:=.FullName, _
                    displaymail:=False
        .Close SaveChanges:=False
    End With

    KillFileOnMac TempFilePath & TempFileName & FileExtStr

    With Application
        .ScreenUpdating = True
        .EnableEvents = True
    End With
End Sub

 

Important Functions

Copy both functions below in a normal module of your workbook.
Without the functions the macro(s) above will not work.

Note: If you want to test more then one macro you only have to copy the two functions one time.

Function MailFromMacwithOutlook(bodycontent As String, mailsubject As String, _
            toaddress As String, ccaddress As String, bccaddress As String, _
                          attachment As String, displaymail As Boolean)
'Ron de Bruin, function to Mail with Outlook for the Mac, 19-Aug-2013
    Dim scriptToRun As String

    scriptToRun = scriptToRun & "tell application " & _
                  Chr(34) & "Microsoft Outlook" & Chr(34) & Chr(13)

    scriptToRun = scriptToRun & _
     "set NewMail to make new outgoing message with properties" & _
       "{content:""" & bodycontent & """, subject:""" & mailsubject & """}" & Chr(13)
       
    scriptToRun = scriptToRun & "tell NewMail" & Chr(13)
    If toaddress <> "" Then
        scriptToRun = scriptToRun & "set toaddressList to {" & _
                  Chr(34) & Replace(toaddress, ",", """,""") & Chr(34) & "}" & Chr(13)
        scriptToRun = scriptToRun & "repeat with i from 1 to count toaddressList" & Chr(13)
        scriptToRun = scriptToRun & "make new to recipient at end of to recipients with " & _
                     "properties {email address:{address:item i of toaddressList}}" & Chr(13)
        scriptToRun = scriptToRun & "end repeat" & Chr(13)
    End If
    If ccaddress <> "" Then
        scriptToRun = scriptToRun & "set ccaddressList to {" & _
                  Chr(34) & Replace(ccaddress, ",", """,""") & Chr(34) & "}" & Chr(13)
        scriptToRun = scriptToRun & "repeat with i from 1 to count ccaddressList" & Chr(13)
        scriptToRun = scriptToRun & "make new cc recipient at end of cc recipients with " & _
                     "properties {email address:{address:item i of ccaddressList}}" & Chr(13)
        scriptToRun = scriptToRun & "end repeat" & Chr(13)
    End If
    If bccaddress <> "" Then
        scriptToRun = scriptToRun & "set bccaddressList to {" & _
                  Chr(34) & Replace(bccaddress, ",", """,""") & Chr(34) & "}" & Chr(13)
        scriptToRun = scriptToRun & "repeat with i from 1 to count bccaddressList" & Chr(13)
        scriptToRun = scriptToRun & "make new bcc recipient at end of bcc recipients with " & _
                     "properties {email address:{address:item i of bccaddressList}}" & Chr(13)
        scriptToRun = scriptToRun & "end repeat" & Chr(13)
    End If
    If attachment <> "" Then
        scriptToRun = scriptToRun & "set attachmentList to {" & _
                  Chr(34) & Replace(attachment, ",", """,""") & Chr(34) & "}" & Chr(13)
        scriptToRun = scriptToRun & "repeat with i from 1 to count attachmentList" & Chr(13)
        scriptToRun = scriptToRun & "make new attachment at end of attachments with " & _
                        "properties {file:item i of attachmentList}" & Chr(13)
        scriptToRun = scriptToRun & "end repeat" & Chr(13)
    End If
    scriptToRun = scriptToRun & "end tell" & Chr(13)

    If displaymail = False Then
        scriptToRun = scriptToRun & "send NewMail" & Chr(13)
    Else
        scriptToRun = scriptToRun & "open NewMail" & Chr(13)
        scriptToRun = scriptToRun & "activate NewMail" & Chr(13)
    End If
    scriptToRun = scriptToRun & "end tell" & Chr(13)

    If Len(toaddress) + Len(ccaddress) + Len(bccaddress) = 0 Or mailsubject = "" Then
        MsgBox "There is no To, CC or BCC address or Subject for this mail"
        Exit Function
    Else
        On Error Resume Next
        MacScript (scriptToRun)
        On Error GoTo 0
    End If
End Function


Function KillFileOnMac(Filestr As String)
'Ron de Bruin, 30-July-2012
'Delete files from a Mac.
'Uses AppleScript to avoid the problem with long file names
    Dim ScriptToKillFile As String
    ScriptToKillFile = "tell application " & Chr(34) & _
                       "Finder" & Chr(34) & Chr(13)
    ScriptToKillFile = ScriptToKillFile & _
                 "do shell script ""rm "" & quoted form of posix path of " & _
                  Chr(34) & Filestr & Chr(34) & Chr(13)
    ScriptToKillFile = ScriptToKillFile & "end tell"

    On Error Resume Next
    MacScript (ScriptToKillFile)
    On Error GoTo 0
End Function