【问题标题】:Sending Emails with Image of Excel Table as Message Body以 Excel 表格图像作为消息正文发送电子邮件
【发布时间】:2019-10-11 08:59:58
【问题描述】:

我的目标是发送包含 Excel 表格的电子邮件。

我找到了一个宏,可以发送带有抄送、主题、附件和文本作为邮件正文的电子邮件。

我需要将图像作为消息正文发送。

Option Explicit

Sub Send_Mails()
Dim sh As Worksheet
Set sh = ThisWorkbook.Sheets("Send_Mails")
Dim i As Integer

Dim OA As Object
Dim msg As Object

Set OA = CreateObject("outlook.application")

Dim last_row As Integer
last_row = Application.CountA(sh.Range("A:A"))

For i = 2 To last_row
    Set msg = OA.createitem(0)
    msg.To = sh.Range("A" & i).Value
    msg.cc = sh.Range("B" & i).Value
    msg.Subject = sh.Range("C" & i).Value
    msg.Body = sh.Range("D" & i).Value 

    If sh.Range("E" & i).Value <> "" Then
        msg.Attachments.Add sh.Range("E" & i).Value
    End If

    msg.send

    sh.Range("F" & i).Value = "Sent"

Next i

MsgBox "All the mails have been sent successfully"

End Sub

【问题讨论】:

标签: excel vba outlook


【解决方案1】:

我正在使用以下代码发送附件。请检查它是否适合您 代码礼貌@HirSinghRajput

Set objEmail = objOutlook.CreateItem(olMailItem)
 With objEmail
  .To = Sheets("DataSheet").Cells(itr, 2)' Add "To" Email Id

  .Subject = Sheets("DataSheet").Cells(itr, 3)' Add Subject Line

  .Body = Sheets("DataSheet").Cells(itr, 4)' Add Email Body

  For iAttachmentCol = 5 To 9
   strPath = CStr(Sheets("DataSheet").Cells(itr, iAttachmentCol)) ' Copy the attachment Path

   If strPath <> "" Then' Verify attachment path is empty or not

    .Attachments.Add strPath' Attach the attachment if path is not empty

   End If

  Next

  .Send

【讨论】:

    【解决方案2】:

    您需要附加一张图片,然后设置一个可以在消息正文中使用的 CID:

    Attachment attachment = newMail.Attachments.Add(@"E:\Pictures\image001.jpg"
        , OlAttachmentType.olEmbeddeditem , null , "Some image display name" );
    
       string imageCid = "image001.jpg@123";
    
       attachment.PropertyAccessor.SetProperty(
         "http://schemas.microsoft.com/mapi/proptag/0x3712001E", imageCid);
    
       newMail.HTMLBody = String.Format("<body><img src=\"cid:{0}\"></body>", imageCid );
    

    【讨论】:

      猜你喜欢
      • 2015-11-27
      • 1970-01-01
      • 1970-01-01
      • 2015-10-10
      • 2018-05-16
      • 2015-10-26
      • 1970-01-01
      • 2018-05-09
      • 2015-02-11
      相关资源
      最近更新 更多