Thursday, June 3, 2010

Add multiple attachments to a rich text field

The following is a simple function that takes in an array of filenames and attachs them to the mentioned field in the mentioned document,,,,

Function addFilesToRichTextField(contextDoc As NotesDocument,rtFieldName As String, fileNames As Variant) As Boolean
 '@Author    :    Karthikeyan A
'@Name    :    addFilesToRichTextField
'@Purpose:    To attach multiple attachments to a particular document in a particular richtext field 'with the information on the source path of the files
'@Param    :    contextDoc - the document that contains the richtext field
'@Param    :    rtFieldName - The name of the rich text field that should hold the attachments
'@Param    :    fileNames - An array of file names that are to be uploaded to the document's rtfield
'@Return    :    Boolean : true if operation succeeds and false otherwise
    'mark the flow of control getting inside the current function
    'declare all variables and objects necessary for further manipulation
    Dim rtItem As NotesRichTextItem
    'handle errors in case of abrupt termination
    On Error Goto errHandler
    'validate parameters
    'if the context document is nothing then inform the same to the user and exit
    If contextDoc Is Nothing Then
        Msgbox "The handle to the context document to which the attachments are to be added is not set in function ""addFilesToRichTextField""",,"Error in ""addFilesToRichTextField"""
        Exit Function
    End If
    'if there are no file names provided then exit function
    If Isempty(fileNames) Then
        'no problem skip execution
        Exit Function
    End If
    'if the rich text field name is not provided throw an error
    If Trim(rtFieldName)="" Then
        Error 1001,"Invalid input param - ""fileNames is Empty"" in function ""addFilesToRichTextField"""
    End If
    'set the handle for the rich text item associated with the field name provided
    Set rtItem=contextDoc.GetFirstItem(rtFieldName)
    'loop through all the filenames and embed them into the rich text field
    Forall fileName In fileNames
        If Trim(Cstr(fileName)) <> "" Then
            Call rtItem.EmbedObject(EMBED_ATTACHMENT,"",Cstr(fileName))
        End If
    End Forall
    'update richtext item
    Call rtItem.Update()
    'save the current document so that the changes get updated to the document permanently
    Call contextDoc.Save(True,False)
    'mark the flow of control moving out of the current function
    Exit Function
    'inform the user about the error that resulted in abrupt termination
    Msgbox "Error: ***" & Error & "*** occured online ***" & Cstr(Erl) & "*** with error number ***" & Cstr(Err) &"***",,"Error in function""addFilesToRichTextField"""
    Exit Function   
End Function

Hope this helps.... :)

No comments:

Post a Comment