PDA

View Full Version : Copying Outlook attachments to server



never3putt
10-06-2010, 04:35 AM
I have copied an Outlook VBA script and copied into ThisOutlookSession and is working just fine...However I need to (1) move message to "Deleted Items" after it is marked as read and (2) identify additional senders and/or subjects to be acted upon. How can I accomplish this?

Please Advise....Thanks

Code below:

Private WithEvents Items As Outlook.Items
Sub Application_Startup()
Dim olApp As Outlook.Application
Dim objNS As Outlook.NameSpace
Set olApp = Outlook.Application
Set objNS = olApp.GetNamespace("MAPI")
Set Items = objNS.GetDefaultFolder(olFolderInbox).Items
End Sub
Private Sub Items_ItemAdd(ByVal item As Object)
On Error GoTo ErrorHandler
'Only act if it's a MailItem
Dim Msg As Outlook.MailItem
If TypeName(item) = "MailItem" Then
Set Msg = item
'Change variables to match need. Comment or delete any part unnecessary.
If (Msg.SenderName = "Dean Sewell") And _
(Msg.Subject = "test") And _
(Msg.Attachments.Count >= 1) Then
'Set folder to save in.
Dim olDestFldr As Outlook.MAPIFolder
Dim myAttachments As Outlook.Attachments
Dim Att As String
'location to save in. Can be root drive or mapped network drive.
Const attPath As String = "H:\purchase requests\"
' save attachment
Set myAttachments = item.Attachments
Att = myAttachments.item(1).DisplayName
myAttachments.item(1).SaveAsFile attPath & Att
' mark as read
Msg.UnRead = False
End If
End If
ProgramExit:
Exit Sub
ErrorHandler:
MsgBox Err.Number & " - " & Err.Description
Resume ProgramExit
End Sub

JP2112
11-01-2010, 07:22 AM
After you mark the message as Read, add this line:

Msg.Delete

There are several ways you could check for additional subjects or senders. I would write a function that takes the sender name and returns True or False depending on whether the name is one you are looking for. For example,

Function CheckSender(messageSender As String) As Boolean
Dim tempString() As String
' comma-delimited list of names to look for
Const subjectArray As String = "Name 1,Name 2,Name 3"
' create array of names
tempString = Split(subjectArray, ",")
CheckSender = (UBound(Filter(tempString, messageSender)) > -1)
End Function

Edit subjectArray to reflect the sender names you want to look for, then call the function like this:

If CheckSender(Msg.SenderName) And _
(Msg.Subject = "test") And _
(Msg.Attachments.Count >= 1) Then