The following code detaches and removed attachements from mailfiles replacing them with a link. The code sometimes misses an attachment. Out of hundreds of docs it’ll miss 1 or 2. The attachments show using @Attachments but do not show up using rtitem.Embedded objects. Even doc.GetFirstItem(“$File”) doesn’t work. If I use…
varObjects = Evaluate({@AttachmentNames}, doc)
If Isarray(varObjects) Then
Forall attName In varObjects
strExtractName = attName
iCounter = iCounter + 1
Set ObjAttachment= doc.GetAttachment(attName)
I can extract the attchment but get “Could not locate Attached File” when I try to remove it.
So close…
Function fDetachRemoveAndLink(session As NotesSession, doc As NotesDocument, _
strDetachFolder As String, filelog As String, User As String, MailFile As String) As Integer
'Detach and remove file attachment from document
'Replace attachment with target filepath
'Write activity to log file
On Error Goto HandleError
Dim objAttachment As NotesEmbeddedObject
Dim rtStyleText As NotesRichTextStyle
Dim rtStyleSep As NotesRichTextStyle
Dim rtStyleLink As NotesRichTextStyle
Dim Username As NotesName
Dim rtitem As Variant
Dim iCounter As Integer
Dim strExtractName As String
Dim strPath As String
Dim lAnswer As Long
Dim iFilenum As Integer
Dim lBoxTYpe As Long
Dim strMessage As String
Dim strNewLink As String
Dim strNamePart As String
Dim strExtPart As String
Dim FileItem As NotesItem
fDetachRemoveAndLink = 0
iCounter = 0
' Look for rich text items in the document. Use the first rich text
' item found for rtitem - unless we find one named "Body" which will
' supercede any rich text item found thus far.
' If we don't find a rich text item then quit out of this document
Set rtitem = Nothing
Forall item In doc.Items
If item.Type = RICHTEXT Then
If rtitem Is Nothing Then
Set rtitem = doc.GetFirstItem(item.Name)
Elseif Strcompare(item.Name, "body", 5) = 0 Then
Set rtitem = doc.GetFirstItem(item.Name)
End If
End If
End Forall
' If we have not found a rich text item to use then scream and quit
' this document
If rtitem Is Nothing Then
strMessage = "Unable to locate any rich text items to hold the links " & _
"to the attachments that may be removed. Cannot process this document " & _
"with NoteId of: " & fFormatNoteId(doc.NoteId)
'lBoxType = MB_OK+MB_ICONEXCLAMATION
'Messagebox strMessage, lBoxType, "No RichText Item Found"
FileLog = FileLog & {"} + strMessage+ {"} & Chr(13)
Exit Function
End If
' Iterate through each of the document's items looking for attachments
Forall item In doc.Items
If item.Type = Attachment Then 'CODE SKIPS A FEW ATTACHMENTS HERE
success = True
' Now that we have an attachment, get the embedded object
' associated with it. The first thing in the Values array
' is the name of the attachment.
Set objAttachment = doc.GetAttachment(item.Values(0))
iCounter = iCounter + 1
'get the attachment filename
If Not ObjAttachment Is Nothing Then
strExtractName = objAttachment.Name
Else
FileLog = FileLog & "Error on note id " & doc.NoteID & ": " & {"} + User + {","} + strpath + {","} + MailFile + {"} & Chr(13)
Exit Function
End If
'generate a unique path for the file to be detached to - this
'involves checking for the existence of a file with the same name
'and incrementing a counter prepended to the filename until a
'name is found that does not exist in the detach folder.
strPath = strDetachFolder & strExtractName
iFilenum = 1
While fFileExists(strPath)
strNamePart = Strleftback(strExtractName, ".", 5)
strExtPart = Strrightback(strExtractName, ".", 5)
strPath = strDetachFolder & strNamePart & "_" & Cstr(iFilenum) & "." & strExtPart
iFilenum = iFilenum + 1
Wend
' Detach the attachment to the unique path
Call objAttachment.ExtractFile(strPath)
If Not doc.From(0) = "" Then
Set Username = New NotesName(Doc.From(0))
End If
FileLog = FileLog & {"} + User + {","} + strpath + {","} + MailFile + {","} + UserName.Abbreviated + {","} + Cstr(Doc.UniversalID) + {","} + Cstr(doc.created) + {"} & Chr(13)
' Now create the link to the detached file so that we will
' be able to get to it from this document
' The link will be in the format:
' "01/01/2000 Removed Attached file: <attachment> to [file:\\<pathtodetachedfile>]"
' Build the string to be used in the link
strNewLink = "file:\\" & fGetShortPathName(strPath)
' Append the link to the rich text field
If iCounter = 1 Then
Call rtitem.AddNewLine( 2 )
End If
Call rtitem.AppendText( Date$ + ": Removed attached file: " & strExtractName & " to " )
Call rtitem.AppendText( " --> [ " )
Call rtitem.AppendText( strNewLink )
Call rtitem.AppendText( " ]" )
Call rtitem.AddNewLine( 1 )
' Remove the attachment from the document
'Use the rtitem so icons are removed
If Not Isempty(rtitem.EmbeddedObjects) Then
Forall o In rtitem.EmbeddedObjects
If ( o.Type = EMBED_ATTACHMENT ) Then
Call o.Remove
End If
End Forall
End If
End If
End Forall
If Not success Then
Exit Function
End If
'remove all $File fields so attachments are completely removed and icons do not show at bottom
Set FileItem = doc.GetFirstItem("$FILE")
While Not fileitem Is Nothing
Call FileItem.Remove()
Set FileItem = doc.GetFirstItem("$FILE")
Wend
' Save the document so that the changes we just made will be retained
If doc.Save(True, False) Then
Print {Removed attachments from "} + doc.Subject(0) + {" in } + Mailfile
End If
fDetachRemoveAndLink = iCounter
Exit Function
HandleError:
strMessage = "Error in fDetachRemoveAndLink: (" & Err & ") " & Error & " at line: " & Erl
lBoxType = MB_OK + MB_ICONSTOP
Messagebox strMessage, lBoxType, "Error!"
Exit Function
End Function