Link to home
Start Free TrialLog in
Avatar of jkee54
jkee54Flag for United States of America

asked on

Copy Rich text items in lotusscript

I have this code,in an agent, that transfers an email from my inbox to a form in another database where it becomes a task for me to do.  Currently it only copies text and leaves any rich text objects (i.e. attachments) behind.  

This has been OK as a first step, but I need to finish it up.  I don't even know if this is possible:

what modification can I make to this code to also transfer rich text from the "Body" field of my email document to the "Attachments" field of the task database?
Sub Initialize
	
	Dim sess As New NotesSession
	Dim db As New NotesDatabase("MyServer","MyDatabase.nsf")
	Dim curDoc As NotesDocument   'the email
	Dim doc As NotesDocument         'the task document created in the log database
	Dim dtNow As New NotesDateTime("Today")
	Dim ws As New NotesUiWorkspace
	Dim source As NotesUIDocument
	Set source = ws.CurrentDocument
	Set curDoc = source.Document
	
	Set curDoc = source.Document
	Set doc = db.CreateDocument
	Call doc.ReplaceItemvalue("Form", "Request")
	Call doc.ReplaceItemvalue("Summary", curDoc.GetItemValue("Subject")(0))
	Call doc.ReplaceItemValue("Received", curDoc.GetItemValue("DisplayDate")(0))
	Call doc.ReplaceItemValue("Status","New")
	Call doc.ReplaceItemValue("Priority","High")
	Call doc.ReplaceItemValue("Type","Choose Type")
	Call doc.ReplaceItemValue("UserName", sess.CommonUserName)
	Call doc.ReplaceItemValue("Date_2", dtNow.LocalTime)
	Call doc.ReplaceItemValue("Requestor", curDoc.GetItemValue("From")(0))
	Call doc.ReplaceItemValue("EmailCC", curDoc.GetItemValue("EnterCopyTo"))
	Call doc.ReplaceItemValue("EmailTo", curDoc.GetItemValue("EnterSendTo"))
	Call doc.ReplaceItemValue("Assigned", sess.CommonUserName)
	Call doc.ReplaceItemValue("Attachments", curDoc.GetItemValue("Body")(0))
	Call doc.ComputeWithForm(False, True)
'	Call doc.Save(True, False, True)
	Call ws.EditDocument(True, doc)
	
	
'Cleanup before we finish
	Set dtNow = Nothing
	Set doc = Nothing
	Set curDoc = Nothing
	Set db = Nothing
	Set sess = Nothing
	
End Sub

Open in new window

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
Avatar of Sjef Bosman
Sure... but there's an even surer way to do it, but it'll cost ya... :-)
At the expense of just some additional programming lines and a temp doc, you'll be able to copy whatever you need.

In pseudo code:

create a temp NotesDocument
copy ALL items from the document you want to copy to temp.doc. (CopyAllItems...)
' here's the ugly part...
remove ALL fields you don't need, looping through the Items (GetFirstItem, etc.), checking names and types
copy ALL remaining fields to your new document  (CopyAllItems...)
dump temp.doc.

Let me see if I can find you an example... here is one, hope it is open to you:
http://searchdomino.techtarget.com/tip/0,289483,sid4_gci1037183,00.html
>> "but there's an even surer way to do it,"

I disagree.  I think that both methods are equally "sure".

Also, the target form does not use that same field names as the source form, so in addition to copying all the items, he would have to rename them too.  In this case, it is simpler to just copy one field at a time.
I painfully found out that some fields just cannot be copied using CopyItemToDocument or ReplaceItemValue. Fields with attachments are a real pain-in-the-behind... Really difficult are V3-type attachments.

The fieldnames are indeed a problem. Unfortunately, fields be renamed by just changing their Name-property.
>> "Fields with attachments are a real pain-in-the-behind"

This is very true, and my code above will not copy file attachments.  If you need file attachments, then you can try copying all of the $FILE fields as well, but I've only had real success transferring files by the old extract & attach method.
Quote from the question:
> Currently it only copies text and leaves any rich text objects (i.e. attachments) behind.  

May I then conclude that the wrong answer was accepted??  :-))
Avatar of jkee54

ASKER

Bill's three lines were what worked - Did I assign the points to the wrong one?

I had tried it first and it works perfectly! Copies any attachment as well as the rich text 'to and from' sections that are forwarded.
Well, here I go again...

This discussion got me thinking that I should revisit this age old problem and try again to develop an elegant solution -- a single function that will copy a rich-text item and all of the attachments that go with it to another document.

Guess what I found?  The function already exists!  What's more, it's the same function that I just said would not copy file attachments!

It appears that the CopyItemToDocument function now automatically handles any embedded object.  Here's the test I ran to verify it:

1) Started with a back-end document saved with a file attachment, an embedded image, and an embedded object (a bitmap that uses MS Paint).

2) Selected that document in a view and ran this code from a view action...

      Dim sess As New NotesSession      
      Dim db As NotesDatabase
      Dim source As NotesDocument
      Set db = sess.CurrentDatabase
      Set source = db.UnprocessedDocuments.GetFirstDocument

      Dim target As NotesDocument
      Set target = sess.CurrentDatabase.CreateDocument
      target.Form = source.Form
      target.Subject = "Copy of " & source.Subject(0)

      Dim body As NotesRichTextItem
      Set body = source.GetFirstItem("Body")
      Call body.CopyItemToDocument(target, "Body")

      Dim ws As New NotesUIWorkspace
      Call ws.EditDocument(True, target)

3) When I click the view action, a new form is composed and guess what?  All three elements are present, and all three work!  For example, I can save the file attachment to my hard drive, resize the embedded image, and draw directly to the embedded bitmap.

What's more is that I can do all of this without ever saving the target document!  I've tested this several different ways including copying multiple rich-text items at once (some with attachments and some without).  In every case, the correct rich text and attachments were copied.

What I would like to know is "When did this start functioning the way we always thought it should?".  It sure would have been nice to know this sooner   :).
Here's another little tidbit regarding CopyItemToDocument...

The Designer 7 help article for the Java version includes this disclaimer:

"When you call this method using a RichTextItem object, file attachments, embedded objects, and object links that are contained within the rich-text item are not copied to the destination document."

The article for the LotusScript version includes no such warning.
Thanks for testing, wow!

I have proof this doesn't work in R6.5.1. Yes, the attachments in rich-text fields are copied, but the V3-type attachments aren't. My client has an "old-fashioned" fax-to-mail server that delivers faxes in mail as V3-type attachment. Horrible...

What's your release? And where is the code? Ah, agent, yes, a rich text item in an unsaved doc works as of R6x or R7x (dunno which one). There is a call to refresh rich-text as well nowadays. But for compatibility with other (read: older) applications, CopyItemToDocument just isn't enogh.
>> "I have proof this doesn't work in R6.5.1."

Thanks!  Jkee54 stated that he is using R6.5 (which revision?)

>> "but the V3-type attachments aren't."

I wouldn't expect CopyItemToDocument to work for V3-type attachments since they are not contained in a rich text item (unless you count $Body).

>> "What's your release?"

7.0.2

>> "But for compatibility with other (read: older) applications, CopyItemToDocument just isn't enough."

A fair statement, but luckily for me I don't have to make anything backward compatible :)
> I don't have to make anything backward compatible
You lucky so-and-so :-))  All my client's server are R7 or R8, just the fax-to-mail server isn't     *in tears*

AFAIK, the easiest way to copy V3-attachments is the temp.doc. method.  The suggestion even wasn't worth $0.02 ... Better next time!
Avatar of jkee54

ASKER

I have 6.5.  I tested it on various types of emails - straight attachments of various types, other objects such as pictures, and as I said, the banner-type To From Subject headers from forwarded emails in the documents also copied.

What are V3 types?  I am migrating to 7.0 clients and designer in January, will I have issues?
V3 type attachments are attached directly to the document, not a specific rich-text field.  Back in the day, these were the only attachments available.  I very rarely use them anymore, and you should be fine going forward.
Since you don't have 'm right now, I suppose you'll have no problems then.

Actually, I made a mistake. it's even worse: they're called V2-type attachments, or just V2attachments. In Notes V2, back in the old days, the attachments were not stored inside a rich-text field but somewhere else, in a special field. To copy these fields over is a non-trivial matter: CopyItemToDocument can't handle it, nor can ReplaceItemValue. The only way to make it work is to remove the fields you know and leave only the difficult ones. Then copy all remaining stuff over to the destination document.

There are but a few of these applications left, and unfortunately I had just one of them...
Funny... I just had the most bizarre déjà-read experience here...

:-))
That is:   déjà-lu ...
Thanks sjef and Bill.... I'd got it in my head I could use and appendrtitem..... which I'm sure I had working somewhere in the past but failed this time..... googled and wondered why I didn't just look on EE in the first place! (Works fine btw).

Steve