简体   繁体   English

Outlook VBA - 来自 Excel 的电子邮件

[英]Outlook VBA - Email from excel

I have created the below code but I am unable to call the signature.我创建了以下代码,但无法调用签名。 I am looking for call the mail before so that I can get the default signature or if anyone can suggest a workaround.我正在寻找之前打电话给邮件,以便我可以获得默认签名,或者如果有人可以提出解决方法。 I am new to VBA and i am unable to find a solution.我是 VBA 新手,找不到解决方案。 Thank you in advance先感谢您

Sub Email()
Dim OutApp As Object
Dim OutMail As Object
Dim strbody, SigString, signature As String
Dim MailAttachments As String
Dim cell As Variant                                 'Not previously DIM'd
Dim GetBoiler As Object

Sheets("List").Select                               'Edit as required
Range("A2").Select

Application.ScreenUpdating = False
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
For Each cell In Columns("C").Cells
    If cell.Value Like "?*@?*.?*" And _
        LCase(Cells(cell.Row, "D").Value) = "yes" Then

        With Application.ActiveSheet
            MailAttachments = Cells(cell.Row, "E").Value
        End With
        Set OutMail = OutApp.CreateItem(0)
            On Error Resume Next
            With OutMail

                .To = cell.Value
                .Subject = "Monthly Review Meeting with Professional Direct Support for Microsoft Azure – " & Cells(cell.Row, "A").Value  'Refer value from column A (company name)
                .HTMLBody = "" & _
                "<style> body{color:black;font-family:Calibri;font-size: 11pt;}" & "<HTML><body>" & "<p>" & "Hello " & Cells(cell.Row, "B") & ", " & "<br>" & "</br>" & "<br>" & "I am the delivery manager associated to " & "<b>" & Cells(cell.Row, "A") & "</b>" & "<br>" & "</br>" _
                '.Attachments.Add MailAttachments
                .Display
                'Or use .Send

            End With
                SigString = Environ("appdata") & " Roaming\Microsoft\Signatures\Sign.htm"
                If Dir(SigString) <> "" Then
                    signature = GetBoiler(SigString)
                Else
                    signature = ""

                End If

            On Error GoTo 0
            Set OutMail = Nothing
            Set OutApp = Nothing

    End If
Next
cleanup:
Set OutMail = Nothing
Set OutApp = Nothing
Application.ScreenUpdating = True
End Sub

Function GetBoiler(ByVal sFile As String) As String
Dim fso As Object
Dim ts As Object
Set fso = CreateObject("Scripting.FileSystemObject")
Set ts = fso.GetFile(sFile).OpenAsTextStream(1, -2)
GetBoiler = ts.ReadAll
ts.Close
End Function`

Code looks good so far.到目前为止,代码看起来不错。 However let me propose an easier way to get a signature.但是,让我提出一种更简单的方法来获得签名。 When someone opens a new e-mail, the signature is displayed by default.当有人打开新电子邮件时,默认情况下会显示签名。 So if we force the e-mail to display, we can then save the html body (with the signature included) for later inserting因此,如果我们强制显示电子邮件,则可以保存 html 正文(包括签名)以供稍后插入

.With OutMail  
.Display   
t = .HTMLBody

Then for when you want to edit your .HTMLBody you would put something like然后当你想编辑你的 .HTMLBody 时,你会放一些类似的东西

.HTMLBody = "something something blah blah" & "<br/>" & t

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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