通过邮件发送Excel工作表图像

时间:2019-12-27 14:17:14

标签: html excel vba outlook

我有一个带有嵌入式图像的Excel。

如何使用图像名称通过邮件发送?我不想发送附件,我想将其嵌入邮件的html中。有可能吗?

非常感谢您

示例代码:

With objMail
       'Set body format to HTML
       .BodyFormat = olFormatHTML

       .To = Sheet3.Range(inTo)
       If (inAttach1 <> "") Then .Attachments.Add inAttach1
       If (inAttach2 <> "") Then .Attachments.Add inAttach2
       If (Sheet3.Range(inCC) <> "") Then .CC = Sheet3.Range(inCC)

       sBody = Sheet1.Range("B2").Value
       '----insert Name
       iPos = InStr(1, sBody, "[FirstName]", vbTextCompare)
       iPos2 = iPos + Len("[FirstName]")
       sText1 = Mid(sBody, 1, iPos - 1)
       sText2 = Mid(sBody, iPos2, Len(sBody) - iPos2 + 1)
       sBody = sText1 & Sheet3.Range(inName) & sText2


       .Subject = inSubj
       .SentOnBehalfOfName = inFrom
       .HTMLBody = sBody
       'Importancia mail
       '.Importance = olImportanceHigh
       .sEnd
    End With

2 个答案:

答案 0 :(得分:0)

这可能对您有用:http://www.rondebruin.nl/win/s1/outlook/bmail3.htm

您可以发送工作表或所选内容作为电子邮件的正文,其中将包含任何适用的图像(在工作表上或所选区域中)。

Sub Send_Selection_Or_ActiveSheet_with_MailEnvelope()
'Working in Excel 2002-2016
    Dim Sendrng As Range

    On Error GoTo StopMacro

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

    'Note: if the selection is one cell it will send the whole worksheet
    Set Sendrng = Selection

    'Create the mail and send it
    With Sendrng

        ActiveWorkbook.EnvelopeVisible = True
        With .Parent.MailEnvelope

            ' Set the optional introduction field thats adds
            ' some header text to the email body.
            .Introduction = "This is a test mail."

            With .Item
                .To = "ron@debruin.nl"
                .CC = ""
                .BCC = ""
                .Subject = "My subject"
                .Send
            End With

        End With
    End With

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

End Sub

答案 1 :(得分:0)

创建附件并使用PR_ATTACH_CONTENT_ID设置"http://schemas.microsoft.com/mapi/proptag/0x3712001F"属性(DASL名称Attachment.PropertyAccessor)。

您的HTML正文(MailItem.HTMLBody属性)将需要通过cid引用该图像附件:

img src="cid:xyz"

其中xyz是PR_ATTACH_CONTENT_ID属性的值。

使用OutlookSpy查看现有消息(单击“ IMessage”按钮)。

attachment = mailitem.Attachments.Add("c:\temp\MyPicture.jpg")
attachment.PropertyAccessor.SetProperty("http://schemas.microsoft.com/mapi/proptag/0x3712001F", "MyId1")
mailitem.HTMLBody = "<html><body>Test image <img src=""cid:MyId1""></body></html>"
相关问题