如何使Excel向包含工作簿中的特定工作表的特定收件人发送电子邮件

时间:2014-02-04 12:53:13

标签: vba excel-vba excel

我需要excel从打开的工作簿中选择一个特定的工作表,并在每个月末将其发送给特定的收件人。

1 个答案:

答案 0 :(得分:0)

这会将打开的工作表保存到指定的路径,并将工作表名称作为文件名,然后为指定的Receiver创建一个Outlook-Mail,并附上新保存的Excel工作表......

Sub SendMail()
    Dim Anrede As String, Text1 As String, Text2 As String, path As String, sheetname As String
    path = "\\enter\your\path\here\"
    sheetname = ActiveSheet.Name
    Anrede = "Dear Sir or Madam,"
    Text1 = "blablabla"
    Text2 = "Kind regards, "
    Sheets(sheetname).Copy
    ChDir path
    ActiveWorkbook.SaveAs Filename:= _
            path & sheetname & ".xlsx", FileFormat:= _
            xlOpenXMLWorkbook, CreateBackup:=False

    Application.ScreenUpdating = False
    On Error Resume Next
    With CreateObject("Outlook.Application").CreateItem(0)
        .To = "mail@adress.here" 'specify receiver mail here, may be a variable too
        .Subject = "Your subject here "
        .Body = Anrede & vbCrLf & vbCrLf & Text1 & vbCrLf & vbCrLf & Text2
        .Attachments.Add path & sheetname & ".xlsx"
        .Display 'Or use .Send
    End With
End Sub