Subject: RE: Automatically save attachments from emails as they come in
It may not be the best coding out there but it works for me. Let me know if you have any questions
Function isFile(Byval sFileName As String) As Integer
On Error Resume Next
Dim lFileLength As Long
Const ATTR_NORMAL = 0
isFile = False
If Dir$(sFileName, ATTR_NORMAL) <> "" Then
lFileLength = Filelen(sFileName)
If (lFileLength > 0) Then isFile = True
End If
End Function
Function isDirectory (Byval Pathname As String) As Boolean
isDirectory = False
isDirectory = (Dir$(Pathname,16) <> "")
End Function
DECLARE
Const errecip$ = “YourGroupNameHere”
Const recipients$ = “YourGroupNameHere”
Const DownLoadPathNY = “T:\IncomingEmail”
Const DownLoadPathDR = “T:\IncomingEmail”
Dim doc As NotesDocument
Dim doc2 As NotesDocument
Dim rtitem As Variant, rtitem2 As Variant
Dim fileCount As Integer, count As Integer, I As Integer, acount As Integer, ercount As Integer, ocount As Integer
Dim filesn() As String, erlog() As String, d1 As String, d2 As String, fname As String
Dim etf As Boolean, ostatus As Boolean
Dim owf() As String
Dim obj As NotesEmbeddedObject
Dim DownLoadPath As String
Dim Sname As String
Option Public
Option Declare
%INCLUDE “lserr.lss”
%INCLUDE “lsxbeerr.lss”
Sub Initialize
On Error Goto errhandler
Dim aLog As NotesLog
Dim s As New NotesSession
Dim db As NotesDatabase
count = 0
acount = 0
ercount = 0
ocount = 0
ostatus = "False"
Set db = s.CurrentDatabase
Set doc = s.DocumentContext
Set rtitem = doc.GetFirstItem( "Body")
Set aLog = New NotesLog("Database Log")
Sname = db.Server
If isDirectory(DownloadPathNY) = True And (Sname = "YourServerName1" Or Sname = "YourServerName2") Then
DownLoadPath = DownLoadPathNY & "\"
Elseif isDirectory(DownloadPathDR) And Sname = "YourServerName3" = True Then
DownLoadPath = DownLoadPathDR & "\"
Else
Exit Sub
End If
'******Log the start of the script
Call aLog.OpenNotesLog(db.Server,db.filepath)
Call aLog.LogAction("Processing incoming attachments at " & Now())
Call aLog.LogAction("Processing on " & Sname)
'******Look for and loop thru the attachments
Forall rtitem3 In Doc.Items
If RTItem.Type = RICHTEXT Then
If Isarray(RTItem.EmbeddedObjects) Then
If ( rtitem.Type = RICHTEXT ) Then
Forall o In rtitem.EmbeddedObjects
If ( o.Type = EMBED_ATTACHMENT ) _
Then
count = count + 1
fname = o.Name
If isFile(DownLoadPath & fname) Then '***** If file already exists copy it and then save new one
Redim Preserve owf(ocount)
owf(ocount) = fname
ocount = ocount + 1
ostatus = "True"
Filecopy DownLoadPath & fname, DownLoadPath & fname & ".bak"
End If
Redim Preserve filesn(acount) '***** Add file names to array and save them to VPM server
filesn(acount) = fname
Call o.ExtractFile _
(DownLoadPath & fname )
acount = acount + 1
End If
End Forall
If etf = "False" Then '*****If script ran with no errors then log it and send confirmation email
Call aLog.OpenNotesLog(db.Server,db.filepath)
Call aLog.LogAction("Finished processing incoming " & count & " attachments at " & Now())
Set doc2 = db.CreateDocument
d1 = Format(Now(),"mmmm d, yyyy")
d2 = Format(Now(),"hh:mm AM/PM")
doc2.Form = "Memo"
doc2.Subject = "Attachments from XXXXXXX have been processed at " & d2 & " , " & d1
Set rtitem2 = doc2.CreateRichTextItem( "Body" )
rtitem2.AppendText(count & " attachments have been processed at " & Now() & ".") '**** Add text to body including file names and if file already existed.
Call rtitem2.AddNewLine( 3 )
For I = 0 To (acount-1)
Call rtitem2.AddNewLine( 1 )
rtitem2.AppendText((I+1) & ". ---> " & filesn(I))
Next I
Call rtitem2.AddNewLine( 3 )
For I = 0 To (ocount-1)
Call rtitem2.AddNewLine( 1 )
rtitem2.AppendText((I +1) & ".---> " & owf(I) & " already existed. The old file has been copied to " & owf(I) & ".bak")
Next I
Call doc2.Send(False,recipients)
End If
End If
Exit Sub
Else
Goto NoAttach
End If
End If
End Forall
NoAttach:
Call aLog.OpenNotesLog(db.Server,db.FilePath)
Call aLog.LogAction("There were no attachments in the email from XXXXXXX at" & " " & Now())
Set doc2 = db.CreateDocument
d1 = Format(Now(),"mmmm d, yyyy")
d2 = Format(Now(),"hh:mm AM/PM")
doc2.Form = "Memo"
doc2.Subject = "ERROR: There were no incoming attachments at " & d2 & " , " & d1
Set rtitem2 = doc2.CreateRichTextItem( "Body" )
rtitem2.AppendText("There were no attachments in the email from XXXXXXX at " & Now() & ".")
Call doc2.Send(False,recipients)
Exit Sub
ErrHandler:
If etf = "True" Then
ercount = ercount + 1
End If
Call aLog.OpenNotesLog(db.Server,db.FilePath)
Call aLog.LogAction("There was an error " & Error$ & " on line " & Cstr(Erl) & " at" & " " & Now())
Redim Preserve erlog(ercount)
erlog(ercount) = ("There was an error " & Error$ & " on line " & Cstr(Erl) & " at" & " " & Now())
etf = "True"
Set doc2 = db.CreateDocument
d1 = Format(Now(),"mmmm d, yyyy")
d2 = Format(Now(),"hh:mm AM/PM")
doc2.Form = "Memo"
doc2.Subject = "ERROR: There were some errors processing incoming attachments at " & d2 & " , " & d1
Set rtitem2 = doc2.CreateRichTextItem( "Body" )
Call rtitem2.AddNewLine( 3 )
For I = 0 To (ercount)
Call rtitem2.AddNewLine( 1 )
rtitem2.AppendText((I+1) & ". ---> " & erlog(I))
Next I
Call doc2.Send(False,errecip)
Resume Next
End Sub