Copying mails from a specified source database folder to destination database with same folder name

Following code takes source server file by selection from a dialog , then takes input of destination server and destination file name . Then takes Input of folder name from local file and copies all mails from local .nsf file’s folder to destination .nsf folder . (Creates a new folder on destination file if folder doesnot exists)Issue is I require dialogbox to select folder name from local .nsf file (not input box) and then that selected folder name should be passed as folderstirng.

Any help will be appreciated . Thanks in advance.

Dim ws As New NotesUIWorkspace

Dim session As New NotesSession	

Dim SourceServer As Variant

Dim SourceDatabase As Variant

Dim DestServer As Variant

Dim DestDatabase As Variant

Dim FolderString As Variant

Dim askme As Integer		



SourceServer = "" 'taking local server for local mail file	

DestServer = ws.Prompt (PROMPT_OKCANCELEDIT, "Destination Server", "Enter Destination Server here.", "Servername/IN/Server/xyz")

DestDatabase = ws.Prompt (PROMPT_OKCANCELEDIT, "Destination Database", "Enter Destination Database here.", "Archive\xyz.nsf")

FolderString = ws.Prompt (PROMPT_OKCANCELEDIT, "Folder Information", "Enter a folder sting to match on (for example, clients will get all folders starting with 'client' plus any subfolders).Leave blank to copy all folders (including All documents , Inbox etc) from Destination Database here.", "clients\a")



'Option to select local mail file

szFilter ="Notes Databases|*.nsf"

filenames = ws.OpenFileDialog(False,"Select File", szFilter)	

If Not(Isempty(filenames)) Then

	SourceDatabase=filenames(0)		

	

End If

Dim SourceDB As New NotesDatabase(SourceServer,SourceDatabase)	

Dim DestDB As New NotesDatabase(DestServer,DestDatabase)



Dim viewTitle As NotesView

Dim colViewEntries As NotesViewEntryCollection

Dim doc As NotesDocument

Dim views As Variant

Dim nextdoc As NotesDocument

Dim newdoc As NotesDocument

Dim x As Integer



views = SourceDB.Views 'all view in the data base



Forall v In views 'Loop through all views

	If Left(v.Name, Len(FolderString)) = FolderString Then		

		If v.IsFolder Then 'is it a folder?

			x = 0

			Print "Copying folders " & v.Name

			Call DestDB.EnableFolder(v.Name) 'Copies folder if it doesn't exist

			Set viewTitle = SourceDB.GetView(v.Name)

			Set colViewEntries = viewTitle.AllEntries 'Make an Entrycollection of all entries in folder

			Set doc = viewTitle.GetFirstdocument

			

			While Not doc Is Nothing 'Loop through all docs in the Entrycollection

				x=x+1

				Print "Copying documents : " & x & " to " & v.Name & " Folder"

				Set newdoc = doc.CopyToDatabase(DestDB)

				Call newdoc.PutInFolder( v.Name )

				Set nextdoc = viewTitle.GetNextDocument(doc)

				Set doc = nextdoc

			Wend

			

		End If

	End If

End Forall

Subject: Copying mails from a specified source database folder to destination database with same folder name

You’re already getting all of the folder names from the source database; you just need to do it earlier in the code and feed them to a PROMPT_OKCANCELLIST instead of using a PROMPT_OKCANCELEDIT.

Subject: RE: Copying mails from a specified source database folder to destination database with same folder name

Thanks Stan.

This worked for me , I have made following changes in my code and tested same was working fine , can you please confirm once if this is OK.

A bit worried because same will be used for all the users on mailbox.

Dim ws As New NotesUIWorkspace

Dim session As New NotesSession 



Dim SourceServer As Variant 

Dim SourceDatabase As Variant 

Dim DestServer As Variant 

Dim DestDatabase As Variant 

Dim FolderString As Variant 

Dim askme As Integer 



SourceServer = "" 'taking local server for local mail file 	

szFilter ="Notes Databases|*.nsf" 

filenames = ws.OpenFileDialog(False,"Select File", szFilter) 

If Not(Isempty(filenames)) Then 

	SourceDatabase=filenames(0)                 

End If 

DestServer = ws.Prompt (PROMPT_OKCANCELEDIT, "Destination Server", "Enter Destination Server here.", "IN-xxx/IN/Server/xxxx") 

DestDatabase = ws.Prompt (PROMPT_OKCANCELEDIT, "Destination Database", "Enter Destination Database here.", "Archive\xyz.nsf") 





Dim SourceDB As New NotesDatabase(SourceServer,SourceDatabase) 

Dim DestDB As New NotesDatabase(DestServer,DestDatabase) 



Dim viewTitle As NotesView 

Dim colViewEntries As NotesViewEntryCollection 

Dim doc As NotesDocument 

Dim views As Variant 

Dim nextdoc As NotesDocument 

Dim newdoc As NotesDocument 

Dim x As Integer 



views = SourceDB.Views 'all view in the data base 

'******************************************

Forall a In SourceDB.Views

	Redim Preserve viewnames(0 To i)

	viewnames(i) = a.Name

	i = i + 1

End Forall



FolderString  = ws.Prompt (PROMPT_OKCANCELLIST, _

"Select a folder", _

"Select a folder to open.", _

viewnames(0) , viewnames)

'******************************************

Forall v In views 'Loop through all views 		

	If Left(v.Name, Len(FolderString)) = FolderString Then 

		If v.IsFolder Then 'is it a folder? 

			x = 0 

			Print "Copying folders " & v.Name 

			Call DestDB.EnableFolder(v.Name) 'Copies folder if it doesn't exist 

			Set viewTitle = SourceDB.GetView(v.Name) 

			Set colViewEntries = viewTitle.AllEntries 'Make an Entrycollection of all entries in folder 

			Set doc = viewTitle.GetFirstdocument 

			

			While Not doc Is Nothing 'Loop through all docs in the Entrycollection 

				x=x+1 

				Print "Copying documents : " & x & " to " & v.Name & " Folder" 

				Set newdoc = doc.CopyToDatabase(DestDB) 

				Call newdoc.PutInFolder( v.Name ) 

				Set nextdoc = viewTitle.GetNextDocument(doc) 

				Set doc = nextdoc 

			Wend 

			

		End If 

	End If 

End Forall

Subject: RE: Copying mails from a specified source database folder to destination database with same folder name

That should work fine, though it may be just a little bit slow for anyone who has a ridiculous number of folders (hundreds). Redimming an array is an expensive and (relatively) slow process. You could use a List instead of redimming an array to speed it up, or you could double the size of the array every time i reaches the upper bound, them fulltrim it once you’re finished, or you could think (like I do) “serves them right”.

Subject: RE: Copying mails from a specified source database folder to destination database with same folder name

Thanks Stan for all your help.

Subject: RE: Copying mails from a specified source database folder to destination database with same folder name

In addition to this code can we restrict copying of duplicate mails. For example if user runs this code second time on the same folder can we skip already copied mails .

There is a $MessageID field on every mail document which is copied to another archive db it remains same . But how to use it as unique identifier. No change is allowed in design of the mail box , can’t work with getdocumentbykey as this will require a view creation.

something like before copying from source to destination folder - a check can be applied on any ID common to both mail document . If keys doesn’t match then allow copy else not.

Any help will be appeciated.

Subject: RE: Copying mails from a specified source database folder to destination database with same folder name

I don’t have a Notes installation handy, but you might want to look at the threads view. If nothing else, it may give you a starting point.

Subject: RE: Copying mails from a specified source database folder to destination database with same folder name

I did a work around but some how facing error where I’m using db.search with searchformula$. I think I’m missing here something . like way I used searchformula$ value assigned to db.search.

I am using $messageID as key field to search docs from source db and copy only when that $messageID is not found in destination db.

Thanks in advance , any guidance will be helpful.

Forall v In varFolders 'Loop through all views

		If Left(v.Name, Len(FolderString)) = FolderString Then 

			If v.IsFolder Then 'is it a folder? 

				x = 0 

				Print "Copying folders " & v.Name 

				Call DestDB.EnableFolder(v.Name) 'Copies folder if it doesn't exist 

				Set viewTitle = SourceDB.GetView(v.Name) 

				Set colViewEntries = viewTitle.AllEntries 'Make an Entrycollection of all entries in folder 

				Set doc = viewTitle.GetFirstdocument 

				strCurrMessageID = doc.GetItemValue("$MessageID")(0)

'********************************************

'searchFormula$ = {doc.GetItemValue(“$MessageID”)(0)} ’ Error : Notes Error : Formula Error with value of $messageId field

'searchFormula$={$MessageID} ’ This gives no values and name of the field itself

				Set  collection= DestDB.Search(searchFormula$, Nothing,0)		

'*********************************************** xxx=collection.Count

				Set newdoc= collection.GetFirstdocument 

				strNewdocCurrMessageID= newdoc.GetItemValue("$MessageID")(0)

				While Not doc Is Nothing 'Loop through all docs in the Entrycollection 

					x=x+1 

					If strCurrMessageID <> strNewdocCurrMessageID  Then				

						Print "Copying documents : " & x & " to " & v.Name & " Folder" 

						Set newdoc = doc.CopyToDatabase(DestDB) 

						Call newdoc.PutInFolder( v.Name ) 

						Set nextdoc = viewTitle.GetNextDocument(doc) 

						Set doc = nextdoc 

					End If

				Wend 				

			End If 

		End If 

	End Forall

Subject: RE: Copying mails from a specified source database folder to destination database with same folder name

The search formula for db.Search is essentially the same thing as a view selection formula minus the “SELECT” keyword. You want to create a string that looks like this:

$MessageID = “[message ID of document here]”

So your Lotusscript should look like this:

searchFormula$ = {$MessageID = “} & doc.GetItemValue(”$MessageID")(0) & {"}

Subject: RE: Copying mails from a specified source database folder to destination database with same folder name

Thanks Stan , really appreciate your help. This worked.

Cheers