VBA代码在电子邮件正文中发送工作表

时间:2016-04-28 13:49:02

标签: excel vba excel-vba

我是否可以附加任何方式以及将特定工作表作为电子邮件正文发送。

我使用的是以下vba,但这只会将工作表作为附件发送。你能否建议我可以在电子邮件正文中发送工作表。

谢谢! RAIC

Sub Email()
Dim OutApp As Object
Dim OutMail As Object
Dim wbTemp As Workbook
Dim strFilename As String
Dim Sendrng As Range

ThisWorkbook.Worksheets("Test Worksheet").Copy


Set wbTemp = ActiveWorkbook


wbTemp.SaveAs ThisWorkbook.Path & "/" & "TestWb", XlFileFormat.xlOpenXMLWorkbook

strFilename = wbTemp.FullName


Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
On Error Resume Next
With OutMail
    .To = "test@testdomain.com"
    .CC = ""
    .BCC = ""
    .Subject = "Test Email"
    .Body = ""
    .Attachments.Add strFilename
    .display
End With

结束     On Error GoTo 0     设置OutMail =没什么     设置OutApp = Nothing

wbTemp.Close

Kill strFilename

End Sub

1 个答案:

答案 0 :(得分:0)

Private Sub CommandButton1_Click()

    Dim AWorksheet As Worksheet
    Dim Sendrng As Range
    Dim rng As Range

    On Error GoTo StopMacro

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

     Set Sendrng = Worksheets("Sheet1").Range("A1:g15")

     Set AWorksheet = ActiveSheet

     With Sendrng
    .Parent.Select
    Set rng = ActiveCell
     .Select
      ActiveWorkbook.EnvelopeVisible = True
        With .Parent.MailEnvelope
            With .Item
                .To = "recipient"
                .CC = ""
                .BCC = ""
                .Subject = "My subject"
                .Send
            End With

        End With
              rng.Select
    End With

        AWorksheet.Select

Sheet1.Activate
Range("c2:c3").ClearContents
Range("e2:e3").ClearContents
Range("c5") = ""
Range("B8:b10").ClearContents
Range("c8:c10").ClearContents
Range("d8:d10").ClearContents
Range("e8:e10").ClearContents
Range("f8:f10").ClearContents
Range("g8:g10").ClearContents
Range("b13") = ""



 StopMacro:
    With Application
        .ScreenUpdating = True
        .EnableEvents = True
    End With
    ActiveWorkbook.EnvelopeVisible = False

End Sub

您应该添加您的代码,并将其复制到此代码中。原谅你在这里不需要的所有垃圾,我只是复制并粘贴一个我一直在使用的垃圾