New version of an attachment

Public Function NewVersionOfDefectAttachment(BugNum As Long) As Boolean

' Download an attachment, edit it, and upload new version


    Dim BugFact As BugFactory
    Dim bugObj As Bug
    Dim attachFact As AttachmentFactory
    Dim attachObj As Attachment
    Dim FilePath As String, AttachmentName$
    Dim rc
    
    On Error GoTo FUNC_ERR
    
' Get the bug factory from the TDConnection.
    'tdc is the global TDConnection object.
    Set BugFact = tdc.BugFactory
' Get a bug object.
    Set bugObj = BugFact.Item(BugNum)
' Get the bug attachment factory.
    Set attachFact = bugObj.Attachments
    Dim lst As TDAPIOLELib.List
    Set lst = attachFact.NewList("")
' Find SampleAttachment.txt.
    Dim pos%, zeroName$, oneName$
    For Each attachObj In lst
        'For documentation purposes, demonstrate the effect of
        '  the ViewFormat argument to Attachment.Name:
        zeroName = attachObj.name(0)
        oneName = attachObj.name(1)
        Debug.Print "0: " & zeroName & "  1: " & oneName
        '0: BUG_1_SampleAttachment.txt  1: SampleAttachment.txt
        'Wasn't that interesting? And now, back to the main
        '  theme of this example.
        pos = InStr(attachObj.name, "SampleAttachment.txt")
        If pos > 0 Then
            AttachmentName = attachObj.name
Debug.Print _
"The attachment name for SampleAttachment.txt is " & AttachmentName
' Output: The attachment name for SampleAttachment.txt is SampleAttachment.txt
            Exit For
        End If
    Next
    Set lst = Nothing
'-----------------------------------------
'Use Attachment.Load to
' get a local copy of the file.
    attachObj.Load True, FilePath
    Debug.Print "FilePath = " & FilePath
' Append the file name to the path.
    FilePath = FilePath & "\" & AttachmentName
' Make a change in the file.
    Open FilePath For Append As #1
    Print #1, vbCrLf & "This is the emended version"
    Close #1
'-----------------------------------------
'Use Attachment.Save to
' save the emended version.
    attachObj.Save True
    
' Modify the attachment description.
    attachObj.Description = "Emended Bug 1 Sample Attachment"
' Update the description in the project database.
    attachObj.Post
    
    NewVersionOfDefectAttachment = SUCCESS
Exit Function
FUNC_ERR:
    NewVersionOfDefectAttachment = FAILURE
End Function