File Attachments - Programmatically

I need more info or other ideas on the answer to the question Q_21232430

qwaletee referred to an API and OLE Automation Class in the answer - can I get more details, sample code on that?
My biggest challenge is that I want to be able to attach files without the help of the Designer, i.e., ideally, all work has to be performed via COM API.

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Hi GalinaYM,

It's really not possible with back end classes, unless you want to go through a whole lot of trouble :)  You could grab the images for all your attachment programs and put them into the database resource, and then programmatically create a hotspot in the RT field to insert the image with a link to the file.

Or you can create a rich text lite field for each attachment type, which should display the correct icon.

Some links.
Let's see re-reading your original question... the link that you referred to begs the question of how to get Lotus Script to attach files and have the right icon show up, (if I recall).  Was that your question?  So, clarifying, you cannot get the icon to show the correct OLE icon using back end classes.

You would like to use COM API, then it depends on what language you're going to use.  

Shows a VB example: 

However I believe only C/C++ gives you enough power to authenticate:

And a book:

There are C and C++ API toolkits downloadable from Lotus that explain the classes and calls.
ZvonkoSystems architectCommented:
GalinaYM, can you please provide more details what you are doing?
Do you want to use COM API from VBA in Excel, Word or Access?
On what Event do you want to start the script and where comes the file name from?
What files do you want to attach to what Documents in what Databases?

Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

GalinaYMAuthor Commented:
I am trying to emulate a file attachment from a local hard drive to an open Lotus Notes e-mail (new/reply/forward) using COM API from Visual Basic.
I have a solution that allows me to accomplish this, however, the solution requires additions to the template (a new field and a new agent) - this is what I want to avoid.
The point where I got stuck is that when I perform a file attachment using the API, the icon comes up as a gray box (not a pretty Word or Adobe icon).

Here are the two approaches that I am working with, with code.
1. Attaches the file - with gray icon. Not mine - used a link returned by a search on this site.

Sub AttachFile(szFileName As String)

    On Error GoTo errHandler

    Dim ttWs As Object ' New NotesUIworkspace
    Dim ttUidoc As Object ' NotesUIDocument
    Dim ttDoc As Object ' NotesDocument
    Dim ttRtitem As Object ' NotesRichTextItem
    Dim ttEmbedObj As Object ' NotesEmbeddedObject
    Dim ttEmbederr As Integer
    ttEmbederr = False
    Set ttWs = CreateObject("Notes.NotesUIWorkspace")
    Set ttUidoc = ttWs.CurrentDocument
    ttUidoc.EditMode = True
    Call ttUidoc.SAVE
    Set ttDoc = ttUidoc.Document
    Set ttRtitem = ttDoc.GetFirstItem("Body")
    'do it this way first to get the type.
    Set ttEmbedObj = ttRtitem.EmbedObject(EMBED_OBJECT, "", szFileName)
    ttDoc.AttachType = ttEmbedObj.Name
handled4286: 'not all can be attached that way (system doesn't recognize it)

    If ttEmbedObj Is Nothing Then
        ttEmbederr = True
    End If
    Call ttDoc.RemoveItem("Body")
    Call ttDoc.SAVE(True, True)
    Set ttRtitem = ttDoc.CreateRichTextItem("Body")
    Set ttEmbedObj = ttRtitem.EmbedObject(EMBED_ATTACHMENT, "", szFileName)
    If ttEmbederr Then 'just use the extension as the type
        ttDoc.AttachType = Right$(ttEmbedObj.Name, Len(ttEmbedObj.Name) - InStr(ttEmbedObj.Name, "."))
    End If
    ttDoc.AttachName = ttEmbedObj.Name
    ttDoc.AttachSize = ttEmbedObj.FileSize
    ttDoc.LastSaved = FileDateTime(szFileName)
    Call ttDoc.SAVE(True, True)
    Call ttDoc.AppendItemValue("SaveOptions", "0")
    Call ttUidoc.Close
    GoTo finished

    Resume handled4286

    MsgBox "Error " & Error$ & ": " & CStr(Erl) & ": AttachFile", 16, "Error: " & CStr(Err)
    Resume finished


End Sub

2. Creates an attachment with an icon, however, it is an OLE object, there is no file name:

Sub AttachFile(szFileName As String)

    On Error GoTo errHandler

    Dim ttWs As Object ' New NotesUIworkspace
    Dim ttUidoc As Object ' NotesUIDocument
    Dim ttDoc As Object ' NotesDocument
    Dim ttRtitem As Object ' NOTESITEM
    Dim ttEmbedObj As Object ' NOTESEMBEDDEDOBJECT
    Set ttWs = CreateObject("Notes.NotesUIWorkspace")
    Set ttUidoc = ttWs.CurrentDocument
    ttUidoc.EditMode = True
    Call ttUidoc.SAVE
    Set ttDoc = ttUidoc.Document
    Set ttRtitem = ttDoc.GetFirstItem("Body")
    Call ttUidoc.FIELDAPPENDTEXT("Body", szFileName)
    'do it this way first to get the type.
    'Set ttEmbedObj = ttRtitem.EmbedObject(EMBED_OBJECT, "", szFileName)
    ttDoc.SAVE True, False
    ' close the ui document
    Exit Sub

'    MsgBox "Error " & Error$ & ": " & CStr(Erl) & ": AttachFile", 16, "Error: " & CStr(Err)

End Sub

Thank you for helping!
Two more questions..

Where are the emails being sent?  Is there some reason users can't use the UI to attach the file?

Some tests... Two xls files, one attached back-end, one-attached in the client UI.  (so one blank.gif, one ole icon)
Outlook to notes... some windows ico instead of OLE icon.
Notes to Outlook.. doesn't matter what is sent, if the program is installed on the computer, nice OLE icon.
Notes & Outlook to hotmail.. doesn't matter, hotmail shows paperclip.
Notes to Notes.. if OLE, shows OLE icon, if blank.gif, shows blank.gif, unless the program is not installed, then shows blank.gif.

-->Doesn't matter if I attach front or backend

Embedding/Attaching does matter because of where you might be sending the file, since most programs no longer automatically show embedded content, but move it to attachment.

Zvonko.. any ideas ?
GalinaYMAuthor Commented:
The reason the user can't use the Lotus Notes UI is because I want them to use my application ui.

The Attach function in my application does the following:

1. opens the content repository (a third party content repository) for browsing to allow the user to pick a document
2. creates a temp file is created locally on the client machine to hold the content of the chosen document/file
3. attaches the file to the lotus notes e-mail message <-- here is where I need to emulate the Lotues Notes Attach function
the temp file is deleted
4. the user sees the attachment in the message they are composing, the attachment looking just as if it was attached by using the Lotus Notes Attach menu option
5. decide whether to send the document or to save it as draft or to discard - does not matter, as now the control is passed back to Lotus Notes ui

So, we need to display the right icon for the file even before it gets sent.
I have it working as intended, but am looking for a way to do this without making the user add new fields and agents, as using the LN designer turned out to be a showstopper for some users.

Haven't forgotten you.. am see if there is a workaround.. however unelegant.  Problem is the only front end class to attach OLE, @Command([FileAttach...])  that would successfully attach the right icon.   Everything else, will not.  Since you're in the front end, and have a handle on the file that you need to attach, my current suggestion is to use sendkeys, that would emulate the user's keystrokes to move to the "Body" field and then do the "File Attachment"

I will have more as soon as I set up a comperable test environment.. are you using VB 6 or .net?  if so which?

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
GalinaYMAuthor Commented:
Oh, emulate key strokes, I did not think about it, you are very resourceful!

I am digging within the C API and C Toolkit, thinking of creating an agent programmatically and then using the formula language.

I am using VB6.

Thank you for not forgetting me!
Well, how I've done these is to create the agent in the mail template only visible from the action menu, usually I set them to start with the company name:  Acme\Insert File Attachment, and then add an alias: Acme\Insert File Attachment|acmefileattach.

Your agent would need to find the file and place the reference in an environment variable in the Notes.ini file, and then call the acmefileattach agent to paste it into the open email document.

So, one way:
--your agent locates the file, creates the environment value, then calls the attach agent in the Notes email:
(I added the first two lines to test putting the file in the environment, it shows up in the notes.ini file as: $GetFile=c:\payrollinput.xls
Now, be warned, if you create using back end, the "$" may not appear, depends on how you create it, and how it is called.  For the front end environment to work, the $ has to be there.    So, the back end call:
Call notesSession.SetEnvironmentVar( "GetFile", "c:\payrollinput.xls", false )

This was the agent that I used:

@If(getFile = "";@Return("");"");
@Command([EditInsertText];@NewLine + @NewLine + "Attached please find the file: " + getFile);
@Command([EditInsertText];@NewLine + @NewLine);
@Command( [EditInsertFileAttachment] ; getFile ; "0" );

So, your agent just needs to plant the filename, and call the front end agent.

Now, the only problem that you may have is if the memo isn't opened.

Does this help?

GalinaYMAuthor Commented:
wait, but to create the agent the user will have to modify the template :(

I am trying to find out how to use C API to call a formula function from VB6.
Is there a way to execute "@Command( [EditInsertFileAttachment] ; <the attachment file name> );" from VB6?
So far, I found NSFComputeEvaluate

My hopes are that this is possible, what do you think?
GalinaYMAuthor Commented:
Can't use NSFComputeEvaluate:

Note: NSFComputeEvaluate may execute any formula that does not include one of the following functions. These functions depend on Notes user interface functionality. Any formula that uses one of these functions will have no effect when evaluated from an API program using NSFComputeEvaluate:

NSFComputeEvaluate  << yeah, could have told you that :)  was the first thing I tried.   So, it's either the agent, or sendkeys, if you must have a decorative icon :)

Is there some reason why you can't add a separate agent to a mail file template for these users?  (that is throw a separate template for the ones that have to use this?)
GalinaYMAuthor Commented:
Our product is not for in-house usage. The customers will have to update their template, which some of them do not like doing, because that step involves a system admin or an IT person.

Is there an automatic way to update the template?

Can VB6 create an agent (that would execute the formula) at run-time? Using C API, I am guessing.
GalinaYMAuthor Commented:
I thought more about my question on trying to create an agent or script an event at run-time using C API - the @Command will still be operating outside of "Notes context information that is only available you run a formula through the Notes UI" (


I am trying to search for "Lotus C API @Command" and found no search engine that would care about the '@', which is frustrating. So I still have a little hope that maybe not through NSFComputeEvaluate but through some other C API I could execute "@Command ([EditInsertFileAttachment])". Please convince me one way or the other.
You can create an agent, but it cannot use @Command calls. The problem that you're going to have external to your domain is ECL permissions, which is why I see you are using the front end calls.  

Other than using third-party programs like Midas Rich Text, which does correct the icon picture in an attachment - Basically, what Notes seems to do is create a hotspot image in the rich text field using the OLE properties of the attachment.  This is similar to using  passthrough HTML to insert an image: <a href="linktothefilename"><img src="theimageyouwanttoshow"></a>.

Agreed that you cannot update their mail template.  However, there is nothing that would prevent you from distributing a notes application to them that they can sign with their server Id and which would run without problems on their system.

Any email that is created in that application will run with the rights of the client, and you can just as effectively use Notes objects to call and launch your program to grab the file backend, and then call the agent to attach front end so you have the icon that you want.  You can call agents within agents, just remember that the @Command stuff processes faster.

Now, given this, you still don't have control over the icon, because it is generated by the client, and then the server.  So, a good icon could appear in my email, and then the server can replace it with a blank one, or the user's OS installation could be faulty and not resolve the file extension.

The only way to ensure that the file extension is resolved is if the email recipient has the application registered.  And you can't very well embed registry commands, because most end users don't have that access in a corporate environment.

Plus side to distributing your own Notes application is that you can always distribute a template update, or replicate the updates.  down side is that they have to go into the database to compose the email.  

You really can't start with mail templates, because you're going to be stuck upgrading every time each client upgrades, but a separate database doesn't have to upgrade.

Other than that: midas rich text lsx, or possibly this:*Replace+attachment+icon*

Sorry couldn't be of much more help.. The only time I've succeeded in this endeavor is by passthrough html and a link to a real file, not an attachment, using javascript to replace the attachment, or using the front end agent.  And that is usually more trouble than just seeing the stupid gray icon. ;)
Also, the external application may not work without them releasing their client security - there is a "allow third party" to use Notes ID flag that most clients have set to "no."  Exceptions are for blackberry's and cell phones..

In answer to your C API question.. no the @Command is not available.  The COM classes follow the Lotus Script classes, and if I could do it in script, it would be available.  Midas and other third party created a separate set of classes, LSX to do this, but then you'd have to install and maintain the LSX on every client machine.  That would be worse than updating the template. :)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Lotus IBM

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.