Tuesday 25 August 2015

Notes Client: Insert Image Banner for All Outgoing Emails

 

image

Functionality: Insert Banner image for each new Mail in Notes Client.

Steps:

1. Insert Banner into CalendarProfile.

Create Agent in Mailfile and name it like “Insert Banner”

Ensure that the banner image is available on Local machine. Update the path in below code before running the agent.

Run the code one time. It’ll create a RichText item in CalendarProfile and it’ll be used for inserting in new mails.

%REM
    Agent Insert Banner
    Created Aug 25, 2015 by Vikas Tiwari2/India/IBM
    Description: Comments for Agent
%END REM
Option Public
Option Declare

Sub Initialize
    'Dim w As New NotesUIWorkspace
    Dim s As New NotesSession
    Dim db As NotesDatabase
    Dim doc As NotesDocument
    Set db = s.Currentdatabase
    Set doc = db.Getprofiledocument("CalendarProfile") 'CalendarProfile
    If EmbedPictureIntoRichText(doc, "D:/banner.jpg") Then
        MsgBox "success"
    Else
        MsgBox "error"
    End If
End Sub

Sub Terminate
End Sub

Function EmbedPictureIntoRichText(doc As NotesDocument, strFilePath As String) As Boolean
    EmbedPictureIntoRichText = False
    Dim session As New NotesSession
    Dim db As NotesDatabase
    Dim body As NotesMIMEEntity
    Dim header As NotesMIMEHeader
    Dim child As NotesMIMEEntity
    Dim stream As NotesStream
    Dim fileFormat As String
    Dim rtitemA As NotesRichTextItem
    Dim rtitemB As NotesRichTextItem
    Set db = doc.Parentdatabase
    On Err GoTo ErrHandler
    'Delete DummyRichText
    Set rtitemB = doc.GetFirstItem("BannerField")
    If Not rtitemB Is Nothing Then
        Call rtitemB.Remove()
    End If
    Set stream = session.CreateStream
    'Call stream.Open(strFilePath)
    Set body = doc.CreateMIMEEntity("BannerField")
    Set header = body.CreateHeader("Content-Type")
    Call header.SetHeaderVal("multipart/mixed")
    Set child = body.CreateChildEntity
    Call stream.WriteText(Chr(10) & Chr(10))
    Call child.SetContentFromText(stream, "text/plain", ENC_NONE)
    Call stream.Truncate
    Call stream.close()
    REM Create another child entity
    Set stream = session.CreateStream
    Call stream.Open(strFilePath)
    'Set body = doc.CreateMIMEEntity("BannerField")
    'Set header = body.CreateHeader("Content-Type")
    'Call header.SetHeaderVal("multipart/mixed")
    Set child = body.CreateChildEntity()
    fileFormat = "image/jpeg" 'Other formats are "image/gif" "image/bmp"
    Call child.Setcontentfrombytes(stream, fileFormat, 1730)
    'Call child.EncodeContent(ENC_BASE64)
    'Call stream.Truncate
    Call stream.Close()
    Call doc.save(False, False) 'JUST TO REFRESH
    EmbedPictureIntoRichText = True
    Exit Function
    'not required
    Set rtitemA = doc.GetFirstItem("Body")
    Set rtitemB = doc.GetFirstItem("BannerField")
    Call rtitemA.AppendRTItem( rtitemB )
    Call rtitemB.Remove()
    Call doc.save(False, False)
    EmbedPictureIntoRichText = True
    Exit Function
ErrHandler:
    MsgBox "Error occurred: " & CStr(Err) & " : " & Error & " at line no. " & CStr(Erl)
End Function

2. Modify InsertSignature Function by inserting following code as per screenshot below:

If profileDoc.HasItem("BannerField") Then
        Call uiDoc.ImportItem(profileDoc, "BannerField")   
End If   

image

This code is just a starting point to help someone trying to achieve the desired functionality. If more automation is required, let me know I can guide for next steps.

No comments:

Post a Comment