Sending email attachments from Excel via VBA

前端 未结 1 926
太阳男子
太阳男子 2020-12-03 09:23

I\'ve written a macro that at the click of a button it sends an automated email via Outlook. Everything runs smoothly except I just can\'t figure out how to attach a file to

相关标签:
1条回答
  • 2020-12-03 09:40

    You need the Attachments.Add code inserted into the MailItem setup:

    With objOutlookMsg
        Set objOutlookRecip = .Recipients.Add("blah@blah")
        objOutlookRecip.Type = olTo
       .Subject = "Blah " & WeekendingDate
       .Body = "blah blah blah"
    'Add attachments to the message [some code]
       .Attachments.Add "pathToFile"
       For Each objOutlookRecip In .Recipients
           objOutlookRecip.Resolve
       Next
       If DisplayMsg Then
           .Display
       Else
           .Save
       End If
    End With
    Set objOutlook = Nothing
    

    In one of my own scripts I pass a collection of attachments to the MailItem to be attached using a Dictionary object and the following code:

    With oMailItem
            Set .SendUsingAccount = oOutlook.Session.Accounts.Item(iAccount)
            .To = EmailData("To")
            .CC = EmailData("CC")
            .BCC = EmailData("BCC")
            .Subject = EmailData("Subject")
            .Body = EmailData("Body")
            sAttachArray = Split(EmailData("AttachmentPaths"), ";")
            For Each sAttachment In sAttachArray
                .Attachments.Add(sAttachment)
            Next
            .Recipients.ResolveAll
            .Display    ' debug mode - uncomment this to see email before it's sent out
        End With
    
    0 讨论(0)
提交回复
热议问题