VBA电子邮件签名代码证明

时间:2018-07-10 15:48:56

标签: excel vba excel-vba outlook

我对VBA还是很陌生,但在编写宏时却遇到了麻烦,该宏创建的电子邮件已附加我的签名。有人可以让我知道我的代码到底有什么问题以及如何解决该问题吗?

Sub SendFIleAsAttachment()

    Dim OLApp As Outlook.Application
    Dim OLMail As Object
    signature As String

    Set OLApp = New Outlook.Application
    Set OLMail = OLApp.CreateItem(0)
    OLApp.Session.Logon

    With OLMail
    .Display
    End With
        signature = OMail.Body
    With OMail
    .To = "email"
    .CC = "email"
    .BCC = ""
    .Subject = "New Copy Proofing Request"
    .Body = "body text. Thanks!" & vbNewLine & signature
    .Attachments.Add ActiveWorkbook.FullName
    End With
    Set OLMail = Nothing
    Set OLApp = Nothing
End Sub

1 个答案:

答案 0 :(得分:3)

您误拼了OLMailOMail,并且在代码中一次错过了单词Dim。要解决这种语法错误,请按照良好的做法在模块/工作表顶部插入Option Explicit-What do Option Strict and Option Explicit do?

Sub SendFIleAsAttachment()

    Dim OLApp As Outlook.Application
    Dim OLMail As Object
    Dim signature As String

    Set OLApp = New Outlook.Application
    Set OLMail = OLApp.CreateItem(0)

    With OLMail
        .Display
    End With
    signature = OLMail.Body
    With OLMail
        .To = "email"
        .CC = "email"
        .BCC = ""
        .Subject = "New Copy Proofing Request"
        .Body = "body text. Thanks!" & vbNewLine & signature
    End With

End Sub