OpenNTF.org - Archive email attachments with
My Links (Not logged in)
Code Bin Search
 
Hosted by Prominic.NET
Rate This Code
5 - brilliant stuff
4 - very nice
3 - average
2 - needs work
1 - bad
   OpenNTF Code Bin
About This Code
Brief Description:
Archive email attachments without deleting the original email. 
Rating:
Not Rated Yet 
Contributor:
Rebecca McGowan 
Category:
Lotusscript 
Type:
Email 
Notes Version:
R6.x, R7.x 
Last Modified:
05 Apr 2006 
OpenNTF Disclaimer

All of the program code and information presented in the OpenNTF.org Code Bin are provided "as-is", and should be used at your own risk. OpenNTF.org make no express or implied warranty about anything in the Code Bin, and OpenNTF.org will not be responsible or liable for any damage caused by the use or misuse of anything from this site. OpenNTF.org makes no guarantees about anything. Please thoroughly test all of the knowledge and code you find here before you attempt to use them in your production environment.

Code / Description
Option Public

Option Declare
Use "Common"
Dim StringTable As mailtoolsstringtable
Sub Initialize
On Error Goto Trap
Dim s As New NotesSession
Dim uiws As New NotesUIWorkspace
Dim uiv As NotesUIView
Dim policyList As Variant
Dim result As Integer
Dim justOne As Boolean
Dim policy As String
Dim collection As NotesDocumentCollection
Dim dlgNote As NotesDocument
Dim needsSetup, view As String
Set StringTable = New mailtoolsstringtable
Set db = s.currentdatabase
'comment the following if you FolderReferences performance concerns.
If Not (db.FolderReferencesEnabled) Then
db.FolderReferencesEnabled = True
End If
Set uiv = uiws.CurrentView
view = uiv.View.Name
Set collection = db.unprocesseddocuments
If s.Notesbuildversion < 167 Then
Goto ArchivePreRnext
End If
Archive:
policyList = db.ArchiveDestinations
If Fulltrim( policyList(0) ) = "" Then
needsSetup = |In order to archive selected documents you must have at least one archive setting enabled that specifies an archive database destination. To change your archive settings use the Actions->Archive->Settings menu item.|
Goto Trap
End If
If Ubound(policyList) = 0 Then
policy = policyList(0)
justOne = True
Goto SkipDialog
End If
'prompt for which policy to archive to
Set dlgNote = s.currentDatabase.createDocument
Call dlgNote.replaceitemvalue("tmpListOfPolicies", Fulltrim(policyList))
Call dlgNote.replaceitemvalue("tmpPrompt", |Select an Archive Destination:|)
' load up ArchivePolicyList Dialog
result = uiws.DialogBox("(RepeatOpenList)", True, True, False, , , , "Move to Archive Destination", dlgNote, True)
' if user didn't cancel, send collection to selected policy
If result <> False Then
policy = dlgNote.GetItemValue("tmpPolicySelected")(0)
justOne = True
End If
SkipDialog:
If justOne Then
Call archiveAttachments( collection, policy, view )
Call uiws.Viewrefresh()
End If
Exit Sub
ArchivePreRNext:
If YesNoPrompt(StringTable.GetString(TOOL_STRING+76,collection.count),StringTable.GetString(TOOL_STRING+75,Null)) Then
Call archiveAttachments( collection,"Archive Profile", view )
Call uiws.Viewrefresh()
End If
Exit Sub
Trap:
Call DisplayWarn(needsSetup & Error$ & Chr(10),MB_OK,StringTable.GetString(TOOL_STRING+75,Null))
Exit Sub
End Sub
Sub archiveAttachments(cdoc, policy, view)
On Error Goto Trap
Dim s As New NotesSession
Dim doc As NotesDocument
Dim adb As NotesDatabase
Dim adoc As NotesDocument
Dim rtitem As NotesRichTextItem
Dim filenames, archiveloc, archivepath, needsSetup As String
Dim obj As NotesItem
Dim doArchive As Boolean
archivepath = db.getarchivepath(policy)
archiveloc = ""
Set adb = s.GetDatabase(archiveloc, archivepath)
If adb Is Nothing Then
needsSetup = |The selected archive does not exist in this location: | + archiveloc + |:| + archivepath + |.|
Goto Trap
End If
'Move the documents that have attachments to the archive file and then replace
'the attachment in the original with a doc link to the archived copy
Set doc = cdoc.GetFirstDocument
While Not doc Is Nothing
doArchive = False
If (doc.HasEmbedded) Then
Set rtitem = doc.GetFirstItem("Body")
Set adoc = adb.CreateDocument
Call doc.CopyAllItems(adoc,True)
If Not rtitem Is Nothing Then
If Isarray(rtitem.EmbeddedObjects) Then
Forall o In rtitem.EmbeddedObjects
If o.Type = EMBED_ATTACHMENT Then
doArchive = True
If filenames = "" Then
filenames = o.Name
Else
filenames = filenames + ", " + o.Name
End If
Call o.Remove
End If
End Forall
Elseif Isarray(doc.EmbeddedObjects) Then
Forall o In doc.EmbeddedObjects
If o.Type = EMBED_ATTACHMENT Then
doArchive = True
If filenames = "" Then
filenames = o.Name
Else
filenames = filenames + ", " + o.Name
End If
Call o.Remove
End If
End Forall
End If
Else
'If all else fails, Check for earlier notes version attachments
Set obj = doc.GetFirstItem("$File")
While Not obj Is Nothing
doArchive = True
If filenames = "" Then
filenames = obj.Values(0)
Else
filenames = filenames + ", " + obj.Values(0)
End If
obj.Remove
Set obj = doc.GetFirstItem("$File")
Wend
End If
If doArchive Then
'Move new archive docs to the appropriate folders and then save the doc link to the original.
Call adoc.save(True, True)
If (db.FolderReferencesEnabled) Then
Forall fref In doc.FolderReferences
Call adoc.PutInFolder(fref, True)
End Forall
Else
Call adoc.PutInFolder(view, True)
End If
If rtitem Is Nothing Then
Set rtitem = New NotesRichTextItem( doc, "Body" )
End If
Call rtitem.AddNewline(2)
Call rtitem.AppendDocLink( adoc, "Link to archived attachments: "+filenames)
Call rtitem.AppendText(" Link to archived attachments: "+filenames)
Call doc.Save(True, True)
filenames = ""
End If
End If
Call s.UpdateProcessedDoc(doc)
Set doc = cdoc.GetNextDocument(doc)
Wend
Exit Sub
Trap:
Call DisplayWarn(needsSetup & Error$ & Chr(10),MB_OK,StringTable.GetString(TOOL_STRING+75,Null))
Exit Sub
End Sub

Usage / Example
This is a modified version of the Archive\Selected Documents agent in the OpenNTF Mail Experience. Our users send and receive huge attachments and their mail files quickly fill up past the quota. They hate having to archive email for projects they are still working on and need to locate quickly. So, this agent creates a copy of the email in the archive file, but leaves the original email in place. The attachment in the original is replaced with a doc link to the archived copy and a listing of the filenames that were archived. When the user no longer needs the email in question, they can delete it from their mailbox entirely, since a full copy is already in the archive. It can be used at the users discretion and doesn't require changes to their archive settings.

Maybe someone else will find this useful too. Or, improve on it.
 Comments
Posted by Brandon Zylstra on 07/25/2006 11:56:59 AMis this foolproof for regular users?
Has anyone tested this in a regular mail template? Will it cause any problems if the user also uses regular archiving of a message after having already archived the attachment? (For instance, does the archive with the attachment get overwritten by the archive without the attachment?)
Posted by Rebecca McGowan on 08/23/2006 10:56:03 AMI use it in my production environment...
I use it with both the standard 6.5.x DWA mail template in production and the openntf mail template with out any problems.
It does not replace what is in the archive file. It creates another copy without the attachment. My users are told that if they do not want duplicates in their archive file, delete emails in their mailbox that have had attachments archived, since it already exists in the archive file. If I have to modify the standard templates, I keep the code in separate elements for easier upgrading later on. So, the standard archive routine has not been modified to ignore these documents. But you could if this feature was needed.
Also, replace the code in the section that is checking for folders with this code to allow archival from views as well....
If (db.FolderReferencesEnabled) Then
Forall fref In doc.FolderReferences
If Not (fref = "") Then
Call adoc.PutInFolder(fref, True)
Else
Call adoc.PutInFolder(view, True)
End If
End Forall
Elseif (uiv.View.IsFolder) Then
Call adoc.PutInFolder(view, True)
End If
Posted by Rebecca McGowan on 09/08/2006 09:19:10 AMAlso, make a change here....
in the test for folder references....
Forall fref In doc.FolderReferences
If Not (fref = "") Then
Call adoc.PutInFolder(fref, True)
Elseif (uiv.View.IsFolder) Then
Call adoc.PutInFolder(view, True)
End If
End Forall
Posted by Mohamed Hassan on 05/29/2007 05:30:30 AMMAILTOOLSSTRINGTABLE Class not found
Could you please help me to solve this problem as I used your code but it give me the following error message :
"Class or type name not found : MAILTOOLSSTRINGTABLE"
Posted by Rebecca McGowan on 05/29/2007 11:36:49 AMMAILTOOLSSTRINGTABLE Class not found
In the Options section:
Option Public
Option Declare
Use "Common"
In the Declarations section:
Dim StringTable As mailtoolsstringtable
In the Initialize section:
Set StringTable = New mailtoolsstringtable
Posted by Mohamed Hassan on 05/30/2007 04:44:08 AMMAILTOOLSSTRINGTABLE Class not found
First thanx for your reply
I do your steps but I have another problem that
"PUBLIC is not allowed in this module"
Posted by Kjeld Larsen on 08/01/2007 07:13:11 AMMail 6
Hi,
I have tried to implement the agent in the normal mail 6 template but it doesnt work.
Is it possible to use or modify the standard Archive/Selected Document agent so I dont have to use the openntf mail template. I dont find any info about the archivenow function. Isnt it possible to get a link to the archived documents. Would be nice to have a doclink in the org mail pointing to the one in the archive.
Pretty much as here but would prefer to use the mail6 template. Possible. Or am I doing something wrong????
Thanks, Kjeld
Posted by Kjeld Larsen on 08/01/2007 08:06:21 AMServer Archive
Ps. Forgot to mention that I am looking for an archive agent which can be used as server archiving. Means to manually document selection (e.g. only mails older than 4 months).
Can anyone help me here?
Kjeld
 Add your comment!