Solved

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

Posted on 2010-08-23
20
2,839 Views
Last Modified: 2013-12-18
Hi,
I have found a great code snippet provided by Bill
(http://www.experts-exchange.com/Programming/Languages/.NET/Visual_CSharp/Q_24463136.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.
0
Comment
Question by:nobana81
  • 7
  • 6
  • 4
  • +1
20 Comments
 
LVL 9

Expert Comment

by:puru1981
Comment Utility
0
 

Author Comment

by:nobana81
Comment Utility
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


0
 
LVL 5

Expert Comment

by:iPinky
Comment Utility
I am no expert in this but I don't think it's possible to call UI classes
0
 
LVL 5

Expert Comment

by:iPinky
Comment Utility
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
0
 

Author Comment

by:nobana81
Comment Utility
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.................??????
0
 
LVL 5

Expert Comment

by:iPinky
Comment Utility
0
 
LVL 9

Expert Comment

by:puru1981
Comment Utility
can you invoke the method CreateRichTextItem and see what happens. I think this will attach the file to the mail.
0
 
LVL 9

Expert Comment

by:puru1981
Comment Utility
i am hoping that CreateObject is only creating the object not sending the mail.
0
 
LVL 9

Expert Comment

by:puru1981
Comment Utility
you can use this link to invoke the method above for parameters

http://www.access-programmers.co.uk/forums/showthread.php?t=39253
0
 

Author Comment

by:nobana81
Comment Utility
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.
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 5

Expert Comment

by:iPinky
Comment Utility
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
0
 
LVL 9

Expert Comment

by:puru1981
Comment Utility
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.
0
 
LVL 9

Expert Comment

by:puru1981
Comment Utility
0
 

Author Comment

by:nobana81
Comment Utility
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.
0
 
LVL 9

Expert Comment

by:puru1981
Comment Utility
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.
0
 
LVL 22

Accepted Solution

by:
Bill-Hanson earned 500 total points
Comment Utility
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.

http://www.experts-exchange.com/Software/Office_Productivity/Office_Suites/Lotus_SmartSuite/Lotus_Notes/Q_24468076.html#24575544

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.
0
 

Author Comment

by:nobana81
Comment Utility
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?
0
 
LVL 22

Expert Comment

by:Bill-Hanson
Comment Utility
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

0
 

Author Comment

by:nobana81
Comment Utility
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                                                                           


0
 
LVL 22

Expert Comment

by:Bill-Hanson
Comment Utility
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.
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
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 video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

771 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now