【问题标题】:Attach jpg to Outlook Body & center将 jpg 附加到 Outlook 正文和中心
【发布时间】:2020-05-02 14:00:45
【问题描述】:

我在发送一封带有 jpg 附件的电子邮件时遇到问题,该附件应在文本之前居中位于顶部。

EmailBody 是在此上游定义的,Target 只是 Excel 工作表中的一个名称(此宏是从另一个遍历范围的宏调用的)。

我认为问题在于我如何声明 Outlook 对象。在Set oAttach = colAttach.Add("C:\Users\urdearboy\Desktop\@\Meme.jpg") 出现当前错误(一旦删除On Error Go To 行)


知道我哪里出错了吗?在 Outlook 对象/属性方面没有非常丰富的经验,并试图关注here 的帖子,但没有运气


Sub Sender(EmailBody As String, Target As Range)

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

Dim OutApp As Object
Dim OutMail As Object

On Error GoTo BNP:

    Set OutApp = CreateObject("Outlook.Application")
    Set OutMail = OutApp.CreateItem(0)
    Set colAttach = OutMail.Attachments
    Set oAttach = colAttach.Add("C:\Users\urdearboy\Desktop\@\Meme.jpg")
    Set olkPA = oAttach.PropertyAccessor

    olkPA.SetProperty PR_ATTACH_CONTENT_ID, "Meme.jpg"

        With OutMail
            .SentOnBehalfOfName = "urdearboy@so.com"
            .to = Target.Offset(0, 1)
            .cc = Target.Offset(0, 2)
            .Subject = "Check Out This Meme " & Target.Offset(0, 3)
            .HTMLBody = "<p style = 'font-family:arial' >" _
                        & "<BODY><CENTER><IMG src =""cid:Meme.jpg""></CENTER></BODY>" _
                        & "Hi " & Target.Offset(0, 0) & ", " & "<br>" _
                        & EmailBody & "</p>" _
                        & "<p style = 'font-family:arial' >" & "<br>" & "Thanks, " & "<br>" _
                        & "urdearboy" & "</p>"

            'Change to .Send to actually send emails
            .Display

            Target.Offset(, -1) = "Sent"

        End With

BNP:
    Set OutApp = Nothing
    Set OutMail = Nothing

End Sub

【问题讨论】:

    标签: vba outlook


    【解决方案1】:

    您缺少常量声明

    Sub Sender(EmailBody As String, Target As Range)
    
    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    
    Dim OutApp As Object
    Dim OutMail As Object
    Dim colattach As Object
    Dim oAttach As Object
    Dim olkPA As Object
    
    On Error GoTo BNP:
    
        Set OutApp = CreateObject("Outlook.Application")
        Set OutMail = OutApp.CreateItem(0)
        Set colattach = OutMail.Attachments
        Set oAttach = colattach.Add("C:\Users\urdearboy\Desktop\@\Meme.jpg")
        Set olkPA = oAttach.PropertyAccessor
    
        Const PR_ATTACH_CONTENT_ID = "http://schemas.microsoft.com/mapi/proptag/0x3712001F" ' <-----This line
    
        olkPA.SetProperty PR_ATTACH_CONTENT_ID, "Meme.jpg"
    
            With OutMail
                .SentOnBehalfOfName = "urdearboy@so.com"
                .to = Target.Offset(0, 1)
                .cc = Target.Offset(0, 2)
                .Subject = "Check Out This Meme " & Target.Offset(0, 3)
                .HTMLBody = "<p style = 'font-family:arial' >" _
                            & "<BODY><CENTER><IMG src =""cid:Meme.jpg""></CENTER></BODY>" _
                            & "Hi " & Target.Offset(0, 0) & ", " & "<br>" _
                            & EmailBody & "</p>" _
                            & "<p style = 'font-family:arial' >" & "<br>" & "Thanks, " & "<br>" _
                            & "urdearboy" & "</p>"
    
                'Change to .Send to actually send emails
                .Display
    
                Target.Offset(, -1) = "Sent"
    
            End With
    
    BNP:
        Set OutApp = Nothing
        Set OutMail = Nothing
    
    End Sub
    

    【讨论】:

    • 出于好奇,这个常数是什么?
    • 这是一个 DASL 属性,Outlook 使用它来设置/获取嵌入附件的名称
    猜你喜欢
    • 1970-01-01
    • 2012-02-01
    • 1970-01-01
    • 2020-11-21
    • 1970-01-01
    • 2016-08-29
    • 1970-01-01
    • 2014-11-30
    • 1970-01-01
    相关资源
    最近更新 更多