简体   繁体   中英

Send Data from Filtered Table via Outlook email

I am trying to create a VBA that will allow me to send Outlook emails to the designated distributors with information about the project and info from a filtered table data. The filter is based on the distributor's name. Below is the code I have written so far.

Sub EmailDistro_1()

Dim xStrFile As String

    Dim xOutApp As outlook.Application
    Dim xMailOut As outlook.MailItem
    Application.ScreenUpdating = False
    Set xOutApp = CreateObject("Outlook.Application")
    Set xMailOut = xOutApp.CreateItem(olMailItem)
    
    ActiveSheet.Range("Distributor").AutoFilter field:=2, Criteria1:=Cells(2, 2).Value
ActiveSheet.ListObjects("Distributor").Range.Copy

        
        With xMailOut
         .Display
            .To = Range("D2").Value
            .Subject = Range("B8").Value & " " & Range("B9").Value & " - " & Range("B11").Value & " Tile RFQ"
            .HTMLBody = "<p style='font-family:calibri;font-size:12.0pt'>" & Split(Range("C2").Value, " ")(0) & "," & "<br/>" & vbCrLf & "Can you please provide me with pricing, lead times AND rough freight to Zipcode 21850 (Forklift on site)." & "<br/>" & vbCrLf & "<br/>" & vbCrLf & .HTMLBody                           
     
         End With
    
    Set xMailOut = Nothing
    Set xOutApp = Nothing
    Application.ScreenUpdating = True

End Sub

Photo 1 is how I want the email to populate

Photo 2 is the excel sheet I am working off of.

Be aware to get the signature added to the message body, you need to call the .Display method before dealing with the HTMLBody property in the code.

You can use the Word object model and deal with the message body represented in Word as a Document instance, so you could simply use the Paste method to insert the copied data in Excel. The WordEditor property returns the Microsoft Word Document Object Model of the message being displayed. See Chapter 17: Working with Item Bodies for more information.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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