简体   繁体   English

如何在电子邮件正文中发送工作表?

[英]How to send worksheet in email body?

Is there any way I can attach as well as send a worksheet as an email body.有什么方法可以附加以及将工作表作为电子邮件正文发送。

The below VBA code sends the worksheet as an attachment.下面的 VBA 代码将工作表作为附件发送。

How can I send a worksheet in the body of email?如何在电子邮件正文中发送工作表?

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
    
    End With
    On Error GoTo 0
    Set OutMail = Nothing
    Set OutApp = Nothing
    
    wbTemp.Close
    
    Kill strFilename
    
End Sub
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

you should add your code that also copies it to this.您应该添加也将其复制到此的代码。 Forgive all the crap you dont need here, I just copy and pasted one I have been using原谅你在这里不需要的废话,我只是复制并粘贴了我一直在使用的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM