Link to home
Start Free TrialLog in
Avatar of nobana81
nobana81Flag for Mauritania

asked on

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

Hi,
I have found a great code snippet provided by Bill
(https://www.experts-exchange.com/questions/24463136/OLE-in-C.html#discussion)
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.
Avatar of puru1981
puru1981

Avatar of nobana81

ASKER

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


I am no expert in this but I don't think it's possible to call UI classes
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
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.................??????
can you invoke the method CreateRichTextItem and see what happens. I think this will attach the file to the mail.
i am hoping that CreateObject is only creating the object not sending the mail.
you can use this link to invoke the method above for parameters

http://www.access-programmers.co.uk/forums/showthread.php?t=39253
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.
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
create an object of NotesRichTextItem using Activator.CreateInstance

invoke the method AppendRTFile("Path")
http://www-12.lotus.com/ldd/doc/lotusscript/lotusscript.nsf/1efb1287fc7c27388525642e0074f2b6/cab1209e5dc2215e8525642e007537ab?OpenDocument

now attach the object of NotesRichTextItem in your object array.
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.
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:

http://publib.boulder.ibm.com/infocenter/domhelp/v8r0/index.jsp?topic=/com.ibm.designer.domino.main.doc/H_EXAMPLES_NOTESRICHTEXTITEM_CLASS.html

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.
ASKER CERTIFIED SOLUTION
Avatar of Bill-Hanson
Bill-Hanson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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?
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

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);

Greetz                                                                           


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.