Spencer Barnes
Spencer Barnes

Reputation: 2877

Add attachments from an array, without creating file paths

I want to amalgamate attachments that are in draft emails (auto-created by another program) so there is one email with possibly multiple attachments.

I have three arrays:

I'm creating a new email for each unique email address.

My challenge is adding the attachments from the array arrAtt().

I get that .Attachments.Add is meant to work with file paths.

Is there a way to add attachments from arrAtt()? i.e. without saving the attachments to create file paths?

Dim OpenItem As Object
Dim arrDraft() As MailItem 'all drafts
Dim arrAtt() As Attachment 'all attachments
Dim arrAdd() As String 'all email addresses
Dim arrUnqAdd() As String 'unique email addresses
Dim strAddrUnique As String  'unique list of email addresses, delimited

For a = Application.Inspectors.Count To 1 Step -1
    Set OpenItem = Application.Inspectors(a).CurrentItem
    If TypeOf OpenItem Is MailItem Then
        If OpenItem.Subject Like "*New*Invoice*" Then
            b = b + 1
            ReDim Preserve arrDraft(1 To b)
            Set arrDraft(b) = OpenItem
        End If
    End If
Next

ReDim Preserve arrAtt(1 To UBound(arrDraft))
ReDim Preserve arrAdd(1 To UBound(arrDraft))

For a = 1 To UBound(arrDraft)
    arrAdd(a) = arrDraft(a).To
    If Not strAddrUnique Like "*" & arrDraft(a).To & "*" Then _
        strAddrUnique = strAddrUnique & IIf(Len(strAddrUnique) = 0, "", "/") & arrDraft(a).To
    Set arrAtt(a) = arrDraft(a).Attachments.Item(1)
Next

arrUnqAdd = Split(strAddrUnique, "/")

Dim NewMail As MailItem
For a = LBound(arrUnqAdd) To UBound(arrUnqAdd())
    Set NewMail = Application.CreateItem(olMailItem)
    NewMail.To = arrUnqAdd(a)
    For b = LBound(arrAdd) To UBound(arrAdd)
        If arrAdd(b) = arrUnqAdd(a) Then

            '****
            'HERE IS THE PROBLEM
            NewMail.Attachments.Add arrAtt(b) 
            '****

        End If
    Next
    Set NewMail.SendUsingAccount = NewAccount
    NewMail.Display
Next

End Sub

Upvotes: 1

Views: 238

Answers (1)

artnib
artnib

Reputation: 508

You can try to add attachment as embedded item using Type parameter. Personally I've had runtime error 438 when adopted your code after adding OlAttachmentType.olEmbeddeditem as second parameter.

Also there is an example with attaching contact item instead of file from filesystem.

Upvotes: 1

Related Questions