Solved

Copy Rich text items in lotusscript

Posted on 2008-10-27
18
8,386 Views
Last Modified: 2013-12-18
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

0
Comment
Question by:jkee54
  • 8
  • 7
  • 2
  • +1
18 Comments
 
LVL 22

Accepted Solution

by:
Bill-Hanson earned 500 total points
ID: 22815707
Sure.

The easiest way is to use the CopyItemToDocument method of the NotesRichTextItem class.

Replace line #27 with these three lines...

Dim body as NotesRichTextItem
set body = curDoc.GetFirstItem("Body")
call body.CopyItemToDocument(doc, "Attachments")
0
 
LVL 46

Expert Comment

by:Sjef Bosman
ID: 22815821
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
0
 
LVL 22

Expert Comment

by:Bill-Hanson
ID: 22815951
>> "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.
0
 
LVL 46

Expert Comment

by:Sjef Bosman
ID: 22816056
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.
0
 
LVL 22

Expert Comment

by:Bill-Hanson
ID: 22816126
>> "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.
0
 
LVL 46

Expert Comment

by:Sjef Bosman
ID: 22816562
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??  :-))
0
 

Author Comment

by:jkee54
ID: 22816618
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.
0
 
LVL 22

Expert Comment

by:Bill-Hanson
ID: 22816621
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   :).
0
 
LVL 22

Expert Comment

by:Bill-Hanson
ID: 22816722
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.
0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 46

Expert Comment

by:Sjef Bosman
ID: 22816883
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.
0
 
LVL 22

Expert Comment

by:Bill-Hanson
ID: 22817004
>> "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 :)
0
 
LVL 46

Expert Comment

by:Sjef Bosman
ID: 22817138
> 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!
0
 

Author Comment

by:jkee54
ID: 22817154
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?
0
 
LVL 22

Expert Comment

by:Bill-Hanson
ID: 22817333
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.
0
 
LVL 46

Expert Comment

by:Sjef Bosman
ID: 22817389
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...
0
 
LVL 46

Expert Comment

by:Sjef Bosman
ID: 22817408
Funny... I just had the most bizarre déjà-read experience here...

:-))
0
 
LVL 46

Expert Comment

by:Sjef Bosman
ID: 22817415
That is:   déjà-lu ...
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 24926548
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
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

For beginners of Lotus Notes user this is important to know about the types of files and their location supported by IBM Notes. Mostly users are unaware about how many file types are created and what their usages are. This Article is fully dedicated…
IBM Notes offer Encryption feature using which the user can secure its NSF emails or entire database easily. In this section we will discuss about the process to Encrypt Incoming and Outgoing Mails in depth.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

747 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