Adding attachment to lotus notes memo using C# and OLE Automation Classes

Posted on 2010-08-23
Medium Priority
Last Modified: 2013-12-18
I have found a great code snippet provided by Bill
that allows me to create a new memo using C# and OLE.  Now I want to add attachments to that memo and Iam not able to do that. I would be very grateful if someone could share some C# code to add attachments to a memo.

I have tried the following but without success:
NotesUIDocument.InvokeMember("CreatObject", BindingFlags.InvokeMethod, null, uidoc, new Object[] { "Attachment", "", "C:\\Temp\\notesAttachment.txt", "notesAttachment.txt" });

Many thanks in advance.
Question by:nobana81
  • 7
  • 6
  • 4
  • +1

Author Comment

ID: 33499633
Thank you for the replay.

I have already visited all the links you have listed above, but unfortunately the solutions proposed their are based on the COM-Interface.

Since I want to interact with the frontend objects (i.e. create a memo and show it to the user for further edit), I need the OLE automation classes (i.e. late binding).

I would really appreciate some further hints for adding attachments to a memo using C# and late binding


Expert Comment

ID: 33499742
I am no expert in this but I don't think it's possible to call UI classes
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.


Expert Comment

ID: 33499789
here an exerpt from the Designer Help in Notes:

OLE automation uses late binding. You cannot create new Domino objects as you would in LotusScript. You must create (for example, with CreateObject) a Notes.NotesUIWorkspace or Notes.NotesSession object and work down through the hierarchies using the available methods. For example, if you want to open a Domino back-end database, create a Notes.NotesSession OLE automation object, and then use the GetDatabase method of NotesSession to set a reference variable.

In Visual Basic, declare the reference variables for all Domino objects as type Object. When you finish using a Domino object, set the reference variable to Nothing to free the memory it uses.

Use dot notation, just as in LotusScript, to access the properties and methods of an object.

Constants must be specified by actual numeric value rather than name. In LotusScript, you can get the value by displaying it in LotusScript

Author Comment

ID: 33499856
Yes it is possible, as you can see in the solution of Bill, you can create a memo, set all its indexes (to, cc, bcc...) and display it. Now I only want to add an attachment to that memo.

Bill's working solution:

Type NotesSession = Type.GetTypeFromProgID("Notes.NotesSession");
Type NotesUIWorkspace = Type.GetTypeFromProgID("Notes.NotesUIWorkspace");
Object sess = Activator.CreateInstance(NotesSession);
Object ws = Activator.CreateInstance(NotesUIWorkspace);

String mailServer = (String)NotesSession.InvokeMember("GetEnvironmentString", BindingFlags.InvokeMethod, null, sess, new Object[] { "MailServer", true });
String mailFile = (String)NotesSession.InvokeMember("GetEnvironmentString", BindingFlags.InvokeMethod, null, sess, new Object[] { "MailFile", true });
NotesUIWorkspace.InvokeMember("OpenDatabase", BindingFlags.InvokeMethod, null, ws, new Object[] { mailServer, mailFile });
Object uidb = NotesUIWorkspace.InvokeMember("GetCurrentDatabase", BindingFlags.InvokeMethod, null, ws, null);
Object db = NotesUIWorkspace.InvokeMember("Database", BindingFlags.GetProperty, null, uidb, null);
Type NotesDatabase = db.GetType();

// compose a new memo
Object uidoc = NotesUIWorkspace.InvokeMember("ComposeDocument", BindingFlags.InvokeMethod, null, ws, new Object[] { mailServer, mailFile, "Memo", 0, 0, true });
Type NotesUIDocument = uidoc.GetType();
NotesUIDocument.InvokeMember("FieldSetText", BindingFlags.InvokeMethod, null, uidoc, new Object[] { "EnterSendTo", "foo1@google.com" });
 NotesUIDocument.InvokeMember("FieldSetText", BindingFlags.InvokeMethod, null, uidoc, new Object[] { "EnterCopyTo", "foo2@google.com" });
NotesUIDocument.InvokeMember("FieldSetText", BindingFlags.InvokeMethod, null, uidoc, new Object[] { "Subject", "This is a memo created using ole automation classes" });
NotesUIDocument.InvokeMember("FieldSetText", BindingFlags.InvokeMethod, null, uidoc, new Object[] { "Body", "this is the body" });

//add attachment to memo?????????....................HowTo.................??????

Expert Comment

ID: 33499999
can you invoke the method CreateRichTextItem and see what happens. I think this will attach the file to the mail.

Expert Comment

ID: 33500004
i am hoping that CreateObject is only creating the object not sending the mail.

Expert Comment

ID: 33500192
you can use this link to invoke the method above for parameters


Author Comment

ID: 33500306
all the links above use the COM-Interface and/ or vb-programming language but as said before I need OLE Late-Binding and C# as programming language to be able to access frontend objects.

Expert Comment

ID: 33500684
sorry.. that's not correct.. if you go through all the answers on those questions above you will find that they actually do relate to OLE (even if the title is misleading) also they show the "late binding" and the last link even shows how to do it in C#

I guess the point of them was:

declare your objects as "objects" (cause their type is not yet defind upon binding, therefore latebinding) and uste Notes.Classname instead of Lotus.Classname, as Lotus refers to COM and Notes to OLE

Expert Comment

ID: 33500928
create an object of NotesRichTextItem using Activator.CreateInstance

invoke the method AppendRTFile("Path")

now attach the object of NotesRichTextItem in your object array.

Author Comment

ID: 33508144
Thanks for the reply.

@ iPinky: your links discuss how to create plaintext memos and how to edit existing notes documents using there universalId but unfortunately NOT how to add attachments to a composed document using late binding. If you I have already done this, could you please post the two lines of code (additional to the code I have posted above, which creates the memo) to attach a file to that memo? I would be very grateful!

@puru198: your last link does not use late binding and is written in vb. the next to the last link sounds great, I have tried this approach with the following code but without success. I would be very nice if you could please correct the code or better send me only the code (additional to the code I have posted above, which creates the memo) to attach a file to the memo using late binding?
Type rti = Type.GetTypeFromProgID("Notes.NotesRichTextItem"); // this returns nullObject rtiObj = Activator.CreateInstance(rti);rti.InvokeMember("AppendRTFile", BindingFlags.InvokeMethod, null, rtiObj, new Object[] { "C:\\temp\\test.txt" });

Other hints or better sample code to attach files to memos using C# and late binding would be highly appreciated.

Thanks in advance.

Expert Comment

ID: 33508276
i don't have lotus notes installed here. Since this is not my personal computer so can't install Lotus notes here. Sorry for leaving you in between

you can get the classes and there methods definition here:


the above link is bible for the lotus notes.

i don't think you will get more information on OLE automation on net. everywhere you will find the similar type of code (early binding because it is easy to reference the objects and use in the project).

if you have bought this then IBM can help you out on OLE automation.
LVL 22

Accepted Solution

Bill-Hanson earned 2000 total points
ID: 33524695
Composing new memos in the UI with file attachments is tricky!  There are no native UI methods for doing so, but there are a couple of workarounds...

Workaround #1:
Copy the file to the clipboard, then use NotesUIDocument.Paste.  Not my favorite because it obviously destroys anything the user had on the clipboard, but this is pretty easy to do.

Workaround #2:
Create the email using only back-end classes, then display it to the user using the NotesUIWorkspace.EditDocument method. I notice puru1981 mentioned using NotesRichTextItem and NotesEmbeddedObject, but I find using richtext a bit clunky.  Instead, I'm using the newer mime classes (NotesMIMEEntity for example).  I don't have any examples using OLE or C#, but here's a COM/VBA solution that you can use to get started.


A couple of things to keep in mind:

1) I cannot think of a situation where you would need to call CreatObject.  That is a LotusScript/VB command that should be reserved for extending the client from within.

2) Only call CreateInstance for NotesSession and NotesUIWorkspace.  Those are the root objects in Notes land.  NotesSession for back-end access, NotesUIWorkspace for GUI access.  All other handles should be obtained from one of these objects or a child thereof.

Author Comment

ID: 33528083
Hi Bill,

thanks for the helpful hints. I have already tried the Workaround#1. It works fine for Notes8 but unfortunately not for Notes7 (an exception is thrown) and the most of my users have Notes7 running:-(

Iam using the following line of code to paste a file from the clipboard to the created memo:

NotesUIDocument.InvokeMember("Paste", BindingFlags.InvokeMethod, null, uidoc, new Object[] { "Body" });

Iam I missing something?
LVL 22

Expert Comment

ID: 33531126
Man! Lotus really needs to create a new library for accessing the front end! Using InvokeMember is such a pain and a bit confusing until you get the hang of it.
There's two things that you need to be aware of in order to use InvokeMember effectively.
  1. You need to understand what each parameter in InvokeMember is used for.
  2. You need to understand the signature of the method that you are trying to invoke.
Both of these are answered by the product documentation.
Type.InvokeMember looks like this:
public object InvokeMember(string name, BindingFlags invokeAttr, Binder binder, object target, object[] args);
  • name - the name of the method that you want to invoke.
  • invokeAttr - for most Lotus objects, this will be either BindingFlags.GetProperty or BindingFlags.InvokeMethod depending on what you need to do.
  • binder - null.
  • target - The object that contain the property or method that you will invoke.
  • args - an array or arguments to pass to the method.
NotesUIDocument.Paste looks like this:
Call notesUIDocument.Paste
As you can see, paste does not take any arguments, so the args parameter in InvokeMember needs to be null.
Also, the documentation says "Pastes the contents of the Clipboard at the current cursor position on a document.".  The important thing to note here is that it pastes the contents 'at the current cursor position'.  Since the cursor is placed the Subject field on compose, you need to change the focus to the Body field before pasting. You can use NotesUIDocument.GotoField for this...
Call notesUIDocument.GotoField( fieldName$ )
Since GotoField takes 1 parameter, our args param needs to be new Object[] { "Body" }.

NotesUIDocument.InvokeMember("GotoField", BindingFlags.InvokeMethod, null, uidoc, new Object[] { "Body" });
NotesUIDocument.InvokeMember("Paste", BindingFlags.InvokeMethod, null, uidoc, null);

Open in new window


Author Comment

ID: 33540435
Hi Bill, thanks for the comments.

Copying Files from Clipboard using "GotoField" and "Paste" work only with Notes8 but unfortunately not with Notes7.

Using Notes7 Iam getting the exception: {System.Runtime.InteropServices.COMException (0x00001137): Document command is not available.}. With Notes8 it works fine.

I think this is because copy-paste (using ctrl+c and ctrl+v) from outside Notes doesn' t work using Notes7; but It does using Notes8.

My idea was to use "GotoField" -> "Attachment" and then use "Paste" like in the following Code. But I think there is no field called "Attachment":

NotesUIDocument.InvokeMember("GotoField", BindingFlags.InvokeMethod, null, uidoc, new Object[] { "Attachment" });NotesUIDocument.InvokeMember("Paste", BindingFlags.InvokeMethod, null, uidoc, null);


LVL 22

Expert Comment

ID: 33541972
I used the copy/paste solution in production in Notes 5, 6, & 7.  Yesterday was the first time I've tried it in 8.  Not sure why it is not working for you.  I abandoned that as a poor workaround years ago.  The best way to handle this is to create the email in the backend, then display it to the user.  You can use either richtext or mime.

Regarding "GotoField", Notes does not have a field named "Attachment".  All file attachments belong in the "Body" field in Lotus land.

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Problem "Can you help me recover my changes?  I double-clicked the attachment, made changes, and then hit Save before closing it.  But when I try to re-open it, my changes are missing!"    Solution This solution opens the Outlook Secure Temp Fold…
This article covers general Notes 8.5 troubleshooting information including recreating the Notes\Data folder.
Planning to migrate your EDB file(s) to a new or an existing Outlook PST file? This video will guide you how to convert EDB file(s) to PST. Besides this, it also describes, how one can easily search any item(s) from multiple folders or mailboxes…
Is your organization moving toward a cloud and mobile-first environment? In this transition, your IT department will encounter many challenges, such as navigating how to: Deploy new applications and services to a growing team Accommodate employee…
Suggested Courses
Course of the Month9 days, 3 hours left to enroll

621 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question