Sub Postsave(Source As Notesuidocument)
' Save file metadata.
Dim doc As NotesDocument
Dim fileMap List As Variant
Dim hasQuote As Boolean
Dim hasCSS As Boolean
Dim hasOA As Boolean
Set doc = Source.Document
Call DocGetFileLocations(doc, fileMap)
If (Isempty(fileMap)) Then
doc.FileDisplay = "No Attachments"
Else
Forall fileNames In fileMap
Select Case Listtag(fileNames)
Case "QuotationDocument"
Forall fileName In fileNames
If (Right(Lcase(fileName), 4) = ".doc") Then
hasQuote = True
Exit Forall
End If
End Forall
Case "CostingSheet"
Forall fileName In fileNames
If (Right(Lcase(fileName), 4) = ".xls") Then
hasCSS = True
Exit Forall
End If
End Forall
Case "OrderAcknowledgement"
Forall fileName In fileNames
If (Right(Lcase(fileName), 4) = ".doc") Then
hasOA = True
Exit Forall
End If
End Forall
End Select
End Forall
If (hasQuote) Then
doc.QuoteAttached = "Yes"
Else
doc.QuoteAttached = "No"
End If
If (hasCSS) Then
doc.CSSAttached = "Yes"
Else
doc.CSSAttached = "No"
End If
If (hasOA) Then
doc.OrderAckAttached = "Yes"
Else
doc.OrderAckAttached = "No"
End If
End If
Call doc.Save(True, False)
End Sub
Sub DocGetFileLocations(doc As NotesDocument, fileMap List As Variant)
'/**
' * Locates all file attachments in a document and returns the result in a hash table (list).
' * @param doc The document that contains the attachments.
' * @param fileList (Return) A hash table (list) used to store and return the results of this function.
' */
Dim allFiles As Variant ' all files embedded within the specified document regardless of which (if any) richtext field contains them.
Dim rtFiles As Variant ' files found embedded within any rich text field.
Erase fileMap ' clear the return param.
' Get all attachment names.
allFiles = ArrayTrimArray(Evaluate("@AttachmentNames", doc))
If (Isempty(allFiles)) Then Exit Sub
' Check all richtext items for embedded files.
Forall item In doc.Items
If (item.Type = RICHTEXT) Then
If (Not Isempty(item.EmbeddedObjects)) Then
Forall obj In item.EmbeddedObjects
fileMap(item.Name) = ArrayAdd(fileMap(item.Name), obj.Name)
rtFiles = ArrayAdd(rtFiles, obj.Name)
End Forall
End If
End If
End Forall
' Get the files that are not embedded within an item.
Forall file In allFiles
If (Not ArrayIsMember(rtFiles, file, False)) Then
fileMap("$DOCUMENT") = ArrayAdd(fileMap("$DOCUMENT"), file)
End If
End Forall
End Sub
The agent below will do the same thing to all documents in the "DocumentsToUpdate" view (you'll need to change the view name in the code below to the name of a view that contains your documents).
To create the agent...
1) From the designer, select New Agent.
2) Change the type to "LotusScript".
3) If you added the DocGetFileLocations to a script library, then add this line to the agent's "Options" module (replacing "common_functions" with the name of your library):
Use "common_functions"
4) If you don't have a library, then paste all of the DocGetFileLocations code into the "Declarations" module.
5) Paste the code below into the "Declarations" module.
6) Set the agent's trigger to ""Action menu selection".
7) Set the agent's target to "None".
8) Save and run the agent.
Open in new window