Outlook Email and Signature from Excel VBA - .Body vs .HTMLbody

前端 未结 2 766
礼貌的吻别
礼貌的吻别 2020-12-10 23:23

I have a button on my worksheet to send an email (plus more, but not important). I want my default signature with its HTML formatting but neither option is producing the re

相关标签:
2条回答
  • 2020-12-10 23:41

    try to insert your data into the properly html tags:

    .HTMLBody = "<font face=""verdana"" color=""black"">This is some text!</font>"
    

    for spaces you must to add this tag "<br>", For example:

    .HTMLBody = "<font face=""calibri"" color=""black""> hello <br>"
    .HTMLBody = .HTMLBody & " how <br>" & " are <br>" & " you?</font>"
    

    result in:

    hello


    how


    are


    you?

    Edit2

    In order to insert images (signature as images) you could use the following code:

    1 step. Copy this code an paste in class module and name that class module like "MailOptions"

    Private Message As CDO.Message
    Private Attachment, Expression, Matches, FilenameMatch, i
    
    Public Sub PrepareMessageWithEmbeddedImages(ByVal FromAddress, ByVal ToAddress, ByVal Subject, ByVal HtmlContent)
    
        Set Expression = CreateObject("VBScript.RegExp")
        Expression.Pattern = "\<EMBEDDEDIMAGE\:(.+?)\>"
        Expression.IgnoreCase = True
        Expression.Global = False 'one match at a time
    
        Set Message = New CDO.Message
        Message.From = FromAddress
        Message.To = ToAddress
        Message.Subject = Subject
    
        'Find matches in email body, incrementally increasing the auto-assigned attachment identifiers
        i = 1
        While Expression.Test(HtmlContent)
            FilenameMatch = Expression.Execute(HtmlContent).Item(0).SubMatches(0)
            Set Attachment = Message.AddAttachment(FilenameMatch)
            Attachment.Fields.Item("urn:schemas:mailheader:Content-ID") = "<attachedimage" & i & ">" ' set an ID we can refer to in HTML
            Attachment.Fields.Item("urn:schemas:mailheader:Content-Disposition") = "inline" ' "hide" the attachment
            Attachment.Fields.Update
            HtmlContent = Expression.Replace(HtmlContent, "cid:attachedimage" & i) ' update the HTML to refer to the actual attachment
            i = i + 1
        Wend
    
        Message.HTMLBody = HtmlContent
    End Sub
    
    Public Sub SendMessageBySMTP(ByVal SmtpServer, ByVal SmtpUsername, ByVal SmtpPassword, ByVal UseSSL)
        Dim Configuration
        Set Configuration = CreateObject("CDO.Configuration")
        Configuration.Load -1 ' CDO Source Defaults
        Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
        Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = SmtpServer
        'Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = SmtpPort
        Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 30
    
        If SmtpUsername <> "" Then
            Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
            Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = SmtpUsername
            Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = SmtpPassword
        End If
        Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = UseSSL
        Configuration.Fields.Update
        Set Message.Configuration = Configuration
        Message.Send
    End Sub
    

    Step 2. In an standar module you will elaborate your .html content and instantiate a object from the class:

    public sub send_mail()
    
    Dim signature As String
    dim mail_sender as new MailOptions 'here you are instantiating an object from the class module created previously
    dim content as string
    
    signature = "C:\Users\your_user\Documents\your_signature.png"
    
    content = "<font face=""verdana"" color=""black"">This is some text!</font>"
    content = content & "<img src=""<EMBEDDEDIMAGE:" & signature & " >"" />"
    
    mail_sender.PrepareMessageWithEmbeddedImages _
                        FromAddress:="chrism_mail@blablabla.com", _
                        ToAddress:="addressee_mail@blablabla.com", _
                        Subject:="your_subject", _
                        HtmlContent:=content
    
    'your_Smtp_Server, for example: RelayServer.Contoso.com
    correos.SendMessageBySMTP "your_Smtp_Server", "your_network_user_account", "your_network_user_account_password", False
    
    end sub
    
    0 讨论(0)
  • 2020-12-10 23:50

    When you set the HTMLBody property, make sure you merge the existing HTMLBody (with the signature) and your new data - you cannot just concatenate two HTML strings and expects a valid HTML. find the position of the "<body" string, find the position of the next ">" (to take care of the body elements with attributes), insert your data after that ">".

    0 讨论(0)
提交回复
热议问题