Link to home
Create AccountLog in
Avatar of GalinaYM
GalinaYM

asked on

File Attachments - Programmatically

I need more info or other ideas on the answer to the question Q_21232430
(https://www.experts-exchange.com/questions/21232430/how-do-you-specify-the-correct-icon-to-display-for-an-attached-or-embedded-file.html)

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.

Help!
Avatar of marilyng
marilyng

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.
http://www-10.lotus.com/ldd/nd6forum.nsf/55c38d716d632d9b8525689b005ba1c0/a71713dedccf7d7e8525705f0081e373?OpenDocument
http://www-10.lotus.com/ldd/nd6forum.nsf/55c38d716d632d9b8525689b005ba1c0/a008c9145f964b0785256e6800332ded?OpenDocument

http://www-10.lotus.com/ldd/46dom.nsf/55c38d716d632d9b8525689b005ba1c0/bf0ea763b6a23f6485256a4e0081b23b?OpenDocument
Regards!
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:
http://www-10.lotus.com/ldd/nd6forum.nsf/55c38d716d632d9b8525689b005ba1c0/0999c7700a589d7285256e5200542ffe?OpenDocument 

However I believe only C/C++ gives you enough power to authenticate:
http://www-10.lotus.com/ldd/nd6forum.nsf/55c38d716d632d9b8525689b005ba1c0/6f45f7198cc4e8338525711a005031d9?OpenDocument

And a book:
http://www.ls2capi.com/

There are C and C++ API toolkits downloadable from Lotus that explain the classes and calls.
http://www-128.ibm.com/developerworks/lotus/downloads/toolkits.html
Avatar of Zvonko
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?

Avatar of GalinaYM

ASKER

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

cantembed:
    Resume handled4286

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

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)
       
    ttUidoc.Refresh
   
    'do it this way first to get the type.
    'Set ttEmbedObj = ttRtitem.EmbedObject(EMBED_OBJECT, "", szFileName)
   
    ttDoc.SAVE True, False
    ' close the ui document
    ttUidoc.Close
   
    Exit Sub

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

End Sub


Thank you for helping!
GalinaYM,
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 ?
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.

Galina
ASKER CERTIFIED SOLUTION
Avatar of marilyng
marilyng

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
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 )
)

tmpFile:="c:\\payrollinput.xls";
thisFile:=@SetEnvironment("GetFile";tmpFile);
-----------------------------------------------------------------
This was the agent that I used:

getFile:=@Environment("GetFile");
@If(getFile = "";@Return("");"");
@Command([EditDocument];"1";"0");
@Command([EditGotoField];"Body");
@Command([EditInsertText];@NewLine + @NewLine + "Attached please find the file: " + getFile);
@Command([EditInsertText];@NewLine + @NewLine);
@Command( [EditInsertFileAttachment] ; getFile ; "0" );
@SetEnvironment("GetFile";"")

-----------------
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?




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

http://www-12.lotus.com/ldd/doc/tools/c/6.5/api65ref.nsf/61fd4e9848264ad28525620b006ba8bd/00d900fc0085002785255e920074b32b?OpenDocument

My hopes are that this is possible, what do you think?
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:

   @Command
   @DbLookup
   @DbColumn
   @DDEInitiate
   @DDETerminate
   @DDEExecute
   @DDEPoke
...............
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?)
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.
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" (http://www-12.lotus.com/ldd/doc/tools/c/6.0.2/api60ref.nsf/0/00D900FC0085002785255E920074B32B?OpenDocument)

Correct?

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.
GalinaYM,
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: http://www.rtlib.com/normunds/rtlibhome.nsf/vwCookbook/*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. ;)
GalinaYM,
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. :)