CopyToDatabse - But not all fields!

Hi all,

I want create a mini document from a Calendar entry and place it into the ($Alarms) folder in another database.

I have managed to get the following code to copy the whole document, but cannot seem to incorporate the code to create a mini-doc then Copy.

Please could somebody help me locate the problem.

Cheers

Paul

Sub Click(Source As Button)

Dim session As New NotesSession

Dim workspace As New NotesUIWorkspace

Dim db As NotesDatabase

Dim db2 As NotesDatabase

Dim uidoc As NotesUIDocument

Dim doc As NotesDocument

Dim doc2 As NotesDocument

Dim item As NotesItem



Set uidoc = workspace.CurrentDocument

Set doc = uidoc.Document

Set db = session.CurrentDatabase

Set db2 = session.Getdatabase ("Spinoza","mail\pthomas.nsf")

====================================================

This is the code to create a mini-doc, but I cannot seem to get it to work with CopyToDatabase. Seems fine when using Send.

'Set doc2 = New NotesDocument(db)



'Set item = doc.GetFirstItem("$Alarm")

'Call item.CopyItemToDocument(doc2, "$Alarm")

'Set item = doc.GetFirstItem("$AlarmOffset")

'Call item.CopyItemToDocument(doc2, "$AlarmOffset")

'Set item = doc.GetFirstItem("CalendarDateTime")

'Call item.CopyItemToDocument(doc2, "CalendarDateTime")

'Set item = doc.GetFirstItem("Subject")

'Call item.CopyItemToDocument(doc2, "Subject")

======================================================

Set doc2 = doc.CopyToDatabase(db2)

Call doc2.PutInFolder ("($Alarms)")

End Sub

Subject: CopyToDatabse - Variant does not contain an object

As is very often the case, your problem here arises from failing to use Option Declare. I suggest you use the setting in the Programmer’s Pane properties to have this line automatically included in the future.

Also, please learn how to use the debugger. Your error occurs well before CopyToDatabase.

Don’t create a document in database A, just so you can CopyToDatabase into database B. Create the document in database B to begin with, then just save it once you’ve added all the fields you want.

Subject: RE: CopyToDatabse - Variant does not contain an object

It easy when you know this. But I’m learning fast.

Cheers - figured this stage out

Subject: CopyToDatabse - Invalid or Non-existant document

The best thing you could do to find the problem is to step though the LS debugger to see what is happening. Or put in some error handling to tell you what line is causing the problem.

Off hand, I see a problem with your doc2. I see that you are building it from the back end but at no time do I see it being saved before it is copied to another database or placed in the folder.

Hope that helps.

Subject: CopyToDatabse

I have amended the code and first posting to remove the error. Had referenced a dimension wrongly.

Have code working to Copy whole document, but I only want to copy certain fields.

But I cannot seem to incorporate my code for a mini-doc into the code. Probably to do with not saving the doc at any time. Although, I do not want to save the mini-doc into the original database.

Any help would be great

Paul

Subject: RE: CopyToDatabse

Then Andre’s advice would be best. Build the new document in the other database and save it there.

Set doc2 = New NotesDocument(db2)

Hope that helps.

Subject: Example of Wing’s suggestion.

’ these statements declare the variables Dim session As New NotesSession

Dim db As NotesDatabase

Dim db2 As New notesdatabase("servername", "dbname.nsf")  

Dim dc As NotesDocumentCollection

Dim doc As Notesdocument, docA As NotesDocument, docB As NotesDocument

Dim itemA As NotesItem, itemB As NotesItem, itemC As NotesItem, itemD As NotesItem, itemE As NotesItem, itemF As NotesItem

Dim owner As notesitem

Dim i As Integer



 ' these statements assign an object reference to the variables

Set db = session.CurrentDatabase

Set db2 = New notesdatabase("servername", "dbname.nsf")

Set dc = db.Unprocesseddocuments



 ' this statement collect each document selected in the view 

For j = 1 To dc.Count

	Set doc = dc.GetNthDocument(j)

	Set itemA = doc.getfirstitem ("body")

	Set itemB = doc.getfirstitem ("from")

	Set itemC = doc.getfirstitem("CopyTo")

	Set itemD = doc.getfirstitem("SendTo")

	Set itemE = doc.getfirstitem("Subject")

	

			

	'this statement declares the document in the database

	Set docB = New notesdocument(db2)

	

	'this is the form the items are copied to and the values on the form

	docB.Form = "Document"

	

	Call itemA.copyitemtodocument (docB, "body")

	Call itemB.copyitemtodocument(docB, "from2")

	If Not itemC Is Nothing Then  

		Call itemC.copyitemtodocument(docB, "CopyTo2")

	End If

	Call itemD.copyitemtodocument(docB, "SendTo2")

	Call itemE.copyitemtodocument(docB, "Subject2")

	

	Set itemF = docB.AppendItemValue("From", session.username)

	

	Call docB.Save(True, True)

	

	

Next

'these statements delete the collection when completed

'If dc.count <> 0 Then

'	Call dc.removall (True)

'End If



Print "Agent completed"

Subject: RE: CopyToDatabse - But not all fields!!

It seems you keep changing your original question. This is at least your third different subject line. It makes it a little confusing for people trying to search for answers here when you change your question so that the answers you got previously no longer match your question. Please respond to the thread instead.

As Wing suggested, CopyToDatabase probably doesn’t work so well with a document that has not been saved. Create a new document in memory in your destination database, and then either individually copy the items you want to keep, or use CopyAllItems. Note if there is rich text and you’re in edit mode, you will need to call NotesUIDocument.Refresh(True) to update it in the back end.