【问题标题】:Problem Attaching Excel File to Email with VBA使用 VBA 将 Excel 文件附加到电子邮件时出现问题
【发布时间】:2019-05-15 13:14:58
【问题描述】:

我正在尝试将附件添加到从模板创建的电子邮件中。这个想法是能够使用文件选择器来选择多个文件,然后 excel 会向正确的收件人发送一封带有正确附件的电子邮件。

问题是我不能使用“.Display”方法而不出现错误,我想在发送前查看电子邮件,所以我不想使用“.Send”。

但是,无论出于何种原因,如果我使用“.Body = ''”清除电子邮件模板正文,我就可以显示电子邮件并附加正确的文件。我想保留模板中的电子邮件正文,就像不清除它并重写它一样。

所以如果我想在发送前先显示,我似乎不能使用电子邮件模板?有没有人遇到过这个问题或知道如何解决?

错误信息是:

'-2147221233(8004010f)' 尝试的操作失败。找不到对象。

顺便说一句,大多数变量都是全局声明的,所以它们是不可见的。

Dim Agency As String
Dim xfullName As Variant
Dim Template As String
Dim mail As Outlook.mailItem
Dim myOlApp As Outlook.Application
Dim selectedFile As Variant
Dim emailBody As String
Dim emailType As String
Dim recipients As String

Sub Recall_Email()

    Dim fileName As String
    Dim inputFile As FileDialog


    Set myOlApp = CreateObject("Outlook.Application")
    Set inputFile = Application.FileDialog(msoFileDialogFilePicker)

    Template = "C:\Users\me\AppData\Roaming\Microsoft\Templates\Recall Templates\Recall Template.oft"

    With inputFile
        .AllowMultiSelect = True
        If .Show = False Then Exit Sub

    End With

    For Each selectedFile In inputFile.SelectedItems
        xfullName = selectedFile
        fileName = Mid(inputFile.SelectedItems(1), InStrRev(inputFile.SelectedItems(1), "\") + 1, Len(inputFile.SelectedItems(1)))
        Agency = Left(fileName, 3)

        CreateTemplate(Template)

    Next selectedFile


End Sub

Private Sub CreateTemplate(temp)

    Set myOlApp = CreateObject("Outlook.Application")
    Set mail = myOlApp.CreateItemFromTemplate(temp)
    Set olAtt = mail.Attachments

    With mail
        '.Body = "" -- If I use this line, everything attaches
        .Subject = Agency & " Recall File"
        .To = "email"
        .Attachments.Add xfullName
        .Display '.Send 
    End With

End Sub

【问题讨论】:

  • 哪一行报错?
  • .在 CreateTemplate(temp) 中显示
  • @DKM 您的CreateTemplate 子程序需要temp 的参数,但您没有在您的Recall_Email 子程序中给它任何参数...
  • @dwirony 你是对的,这是我实际修复的错误,与此问题无关。我现在将编辑代码。
  • 这将有助于查看您的全局变量,例如“selectedFile”。我们如何知道您是否为该变量声明了 Excel 对象?

标签: excel vba email


【解决方案1】:

这是一个关于如何将文件附加或嵌入到 Outlook 的工作示例。

Option Explicit
Dim titleName As String
Dim firstName As String
Dim lastName As String
Dim fullName As String
Dim clientEmail As String
Dim ccEmail As String
Dim bccEmail As String
Dim emailMessage As String

Sub GenerateInfo()

    Dim WS As Worksheet
    Dim lrow As Long
    Dim cRow As Long

    Set WS = ActiveSheet

    With WS
        lrow = .Range("E" & .Rows.Count).End(xlUp).Row
        Application.ScreenUpdating = False
        For cRow = 2 To lrow
            If Not .Range("L" & cRow).value = "" Then
                titleName = .Range("D" & cRow).value
                firstName = .Range("E" & cRow).value
                lastName = .Range("F" & cRow).value
                fullName = firstName & " " & lastName
                clientEmail = .Range("L" & cRow).value

                Call SendEmail

                .Range("Y" & cRow).value = "Yes"
                .Range("Y" & cRow).Font.Color = vbGreen

            Else
                .Range("Y" & cRow).value = "No"
                .Range("Y" & cRow).Font.Color = vbRed
            End If
        Next cRow
    End With

    Application.ScreenUpdating = True

    MsgBox "Process completed!", vbInformation

End Sub
Sub SendEmail()

    Dim outlookApp As Object
    Dim outlookMail As Object
    Dim sigString As String
    Dim Signature As String
    Dim insertPhoto As String
    Dim photoSize As String

    Set outlookApp = CreateObject("Outlook.Application")
    Set outlookMail = outlookApp.CreateItem(0)

    'Change only Mysig.htm to the name of your signature
    sigString = Environ("appdata") & _
                "\Microsoft\Signatures\Marius.htm"

    If Dir(sigString) <> "" Then
        Signature = GetBoiler(sigString)
    Else
        Signature = ""
    End If

    insertPhoto = "C:\Users\marius\Desktop\Presale.jpg" 'Picture path
    photoSize = "<img src=""cid:Presale.jpg""height=400 width=400>" 'Change image name here

    emailMessage = "<BODY style=font-size:11pt;font-family:Calibri>Dear " & titleName & " " & fullName & "," & _
                    "<p>I hope my email will find you very well." & _
                    "<p>Our <strong>sales preview</strong> starts on Thursday the 22nd until Sunday the 25th of November." & _
                    "<p>I look forward to welcoming you into the store to shop on preview.<p>" & _
                    "<p> It really is the perfect opportunity to get some fabulous pieces for the fast approaching festive season." & _
                    "<p>Please feel free to contact me and book an appointment." & _
                    "<p>I look forward to seeing you then." & _
                    "<p>" & photoSize & _
                    "<p>Kind Regards," & _
                    "<br>" & _
                    "<br><strong>Marius</strong>" & _
                    "<br>Assistant Store Manager" & _
                    "<p>"


    With outlookMail
        .To = clientEmail
        .CC = ""
        .BCC = ""
        .Subject = "PRIVATE SALE"
        .BodyFormat = 2
        .Attachments.Add insertPhoto, 1, 0
        .HTMLBody = emailMessage & Signature 'Including photo insert and signature
        '.HTMLBody = emailMessage & Signature 'Only signature
        .Importance = 2
        .ReadReceiptRequested = True
        .Display
        .Send

    End With

    Set outlookApp = Nothing
    Set outlookMail = Nothing
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

【讨论】:

    猜你喜欢
    • 2017-10-23
    • 2020-01-14
    • 2012-05-17
    • 2018-07-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-25
    相关资源
    最近更新 更多