Add attachments to a defect and mail them
Sub AttachFileAndMail()
' Add attachments to a defect and mail them

' This example shows how to check if a defect has attachments,
'   how to add attachments to a defect, and how to mail
'   all the attachments of a given defect.

    Dim attachFact As AttachmentFactory
    Dim theAttachment As Attachment
    Dim attachList As List
    Dim BugFact As BugFactory
    Dim theBug As Bug
    Dim AttachmentList() As Variant
    Dim HasAttachs As Boolean

On Error GoTo AttachFileAndMailErr
' Get an arbitrary defect for this example.
' To find a particular defect, use
' BugFactory.FindSimilarBugs or Bug.FindSimilarBugs.
    'tdc is the global TDConnection object.
    Set BugFact = tdc.BugFactory
    Set theBug = BugFact.Item(1)
'------------------------------------------------------
' Use Bug.Attachments to get the attachment
' factory for the defect.
    Set attachFact = theBug.Attachments
' For demonstration purposes, print the
' names of existing attachments.
    Set attachList = attachFact.NewList("")
    For Each theAttachment In attachList
        Debug.Print "The Attachment.FileName is " _
            & theAttachment.FileName
    Next
 'For this example, guarantee that there are
' attachments to an email by attaching files if
' necessary.
 'To run the example, ensure that your workstation has files:
' "D:\temp\err.log" and "D:\temp\output.txt."
    HasAttachs = theBug.HasAttachment
    If HasAttachs Then Debug.Print vbCrLf & "----------------------------------"
    If Not HasAttachs Then
        Set theAttachment = attachFact.AddItem(Null)
        theAttachment.FileName = "C:\temp\err.log"
        theAttachment.Type = TDATT_FILE
        theAttachment.Post
'-------------------------------------------------------------
' Use AttachmentFactory.AddItem to create the new project entry.
        Set theAttachment = attachFact.AddItem(Null)
        theAttachment.FileName = "C:\temp\output.txt"
        theAttachment.Type = TDATT_FILE
        theAttachment.Post
    End If
' Populate a variant array of server file names
' for use in the SendMail method.
    Set attachList = attachFact.NewList("")
    'Count starts at one and Variant Array size
    ' starts at zero (but see Option Base in the Visual
    '  Basic help), so array size is Count minus one.
    ReDim Preserve AttachmentList(attachList.Count - 1)
    
    Dim i%
    i = LBound(AttachmentList)
'-------------------------------------------------------------
' Use Attachment.ServerFileName to specify the attachment.
    For Each theAttachment In attachList
        AttachmentList(i) = theAttachment.ServerFileName
        i = i + 1
    Next
    Debug.Print vbCrLf & "----------------------------------"
' Array is populated. Let's see what we have:
    Dim fName
    For Each fName In AttachmentList
        Debug.Print "The AttachmentList element is " & fName
    Next
'-------------------------------------------------------------
' Use TDConnection.SendMail to
' send the attachments.
    tdc.SendMail "steves,alex_qc", tdc.UserName, _
        "TestMail TDConnection.SendMail with attachments", _
        "TDConnection.SendMail: This is a test", AttachmentList
        
    
Exit Sub

AttachFileAndMailErr:
ErrHandler err, "AttachFileAndMail"

End Sub