Gil
01-30-2011, 07:48 PM
Hello
I am attempting to save an Excel file from Outlook Exchange to a file on my pc automatically when an email arrives. In my Inbox I have a rule that moves an email with the word Testing in the Subject to a sub folder called Project, it then is set to run a script, which I assume is the code that I placed in Project 1/This Outlook session.
All this as a result of trawling the Outlook forum and finding this possible solution from JP2112 in a thread dated 09-09-2009. I have seen several others which all seem to be variations of this code.
Trouble is I cannot get it to work for me. I have changed the bits that I think are particular to my set up but am now stuck. This code may go a bit further than I need but if someone could review it and point me in the right direction I am quite happy to try and edit out the bits I need.
One question to start is in my novice attempts at Excel I was often told to put Option Explicit first, this doen't seem to work with this code.
A second question is in Set Items = objns.GetDefaultFolder(olFolderInbox).Items I tried to change olFolderInbox to olFolderProject.
Any help would be greatly appreciated.
Private WithEvents Items As Outlook.Items
Private Sub Application_Startup()
Dim objns As Outlook.NameSpace
Set objns = GetNamespace("MAPI")
Set Items = objns.GetDefaultFolder(olFolderInbox).Items
End Sub
Private Sub Items_ItemAdd(ByVal item As Object)
On Error Goto ErrorHandler
Const folder As String = "d:\Desktop\Project Log\"
Const fileExtensions As String = "xls"
Dim fileExts As Variant
Dim Msg As Outlook.MailItem
Dim atts As Outlook.Attachments
Dim att As Outlook.Attachment
Dim i As Long, j As Long
Dim strFilePath As String
' parse file extensions
fileExts = Split(fileExtensions, ",")
' only act on mail items
If TypeName(item) <> "MailItem" Then Goto ProgramExit
Set Msg = item
' exit if message is too small or no attachments
If (Msg.Size < 1500000) And (Msg.Attachments.Count = 0) Then Goto ProgramExit
Set atts = Msg.Attachments
' loop through attachments, if the file extension matches one of the
' specified file types, save it to the given folder
With atts
For i = .Count To 1 Step -1
If UBound(Filter(fileExts, GetFileType(.item(i).fileName))) > -1 Then
strFilePath = folder & BuildFileName(.Count, Msg, .item(i))
With .item(i)
.SaveAsFile strFilePath
.Delete
End With
If Msg.BodyFormat = olFormatHTML Then
Msg.HTMLBody = Msg.HTMLBody & "<p>" & _
"The file was saved to: " & strFilePath & "</p>"
Else
Msg.Body = Msg.Body & vbCrLf & "The file was saved to: " & strFilePath & vbCrLf
End If
End If
Next i
End With
Msg.Save
MsgBox "Xls files are extracted from" & vbCrLf & _
"the emails in Project folder.", vbInformation
ProgramExit:
Exit Sub
ErrorHandler:
MsgBox Err.number & " - " & Err.Description
Resume ProgramExit
End Sub
Function GetFileType(ByVal fileName As String) As String
' get file extension
GetFileType = Mid$(fileName, InStrRev(fileName, ".") + 1, Len(fileName))
End Function
Private Function BuildFileName(ByRef number As Long, ByRef mlItem As Outlook.MailItem, ByRef att As Outlook.Attachment, _
Optional dateFormat As String = "m_d_yyyy-H-MM-SS") As String
'Builds file name to preferred format. Can be changed to personal
'preference.
Const strInfoDlmtr_c As String = " - "
Const lngMxFlNmLen_c As Long = 255
BuildFileName = VBA.Left$(number & strInfoDlmtr_c & _
Format$(mlItem.ReceivedTime, dateFormat) & strInfoDlmtr_c & _
mlItem.senderName & strInfoDlmtr_c & att.fileName, lngMxFlNmLen_c)
End Function
I am attempting to save an Excel file from Outlook Exchange to a file on my pc automatically when an email arrives. In my Inbox I have a rule that moves an email with the word Testing in the Subject to a sub folder called Project, it then is set to run a script, which I assume is the code that I placed in Project 1/This Outlook session.
All this as a result of trawling the Outlook forum and finding this possible solution from JP2112 in a thread dated 09-09-2009. I have seen several others which all seem to be variations of this code.
Trouble is I cannot get it to work for me. I have changed the bits that I think are particular to my set up but am now stuck. This code may go a bit further than I need but if someone could review it and point me in the right direction I am quite happy to try and edit out the bits I need.
One question to start is in my novice attempts at Excel I was often told to put Option Explicit first, this doen't seem to work with this code.
A second question is in Set Items = objns.GetDefaultFolder(olFolderInbox).Items I tried to change olFolderInbox to olFolderProject.
Any help would be greatly appreciated.
Private WithEvents Items As Outlook.Items
Private Sub Application_Startup()
Dim objns As Outlook.NameSpace
Set objns = GetNamespace("MAPI")
Set Items = objns.GetDefaultFolder(olFolderInbox).Items
End Sub
Private Sub Items_ItemAdd(ByVal item As Object)
On Error Goto ErrorHandler
Const folder As String = "d:\Desktop\Project Log\"
Const fileExtensions As String = "xls"
Dim fileExts As Variant
Dim Msg As Outlook.MailItem
Dim atts As Outlook.Attachments
Dim att As Outlook.Attachment
Dim i As Long, j As Long
Dim strFilePath As String
' parse file extensions
fileExts = Split(fileExtensions, ",")
' only act on mail items
If TypeName(item) <> "MailItem" Then Goto ProgramExit
Set Msg = item
' exit if message is too small or no attachments
If (Msg.Size < 1500000) And (Msg.Attachments.Count = 0) Then Goto ProgramExit
Set atts = Msg.Attachments
' loop through attachments, if the file extension matches one of the
' specified file types, save it to the given folder
With atts
For i = .Count To 1 Step -1
If UBound(Filter(fileExts, GetFileType(.item(i).fileName))) > -1 Then
strFilePath = folder & BuildFileName(.Count, Msg, .item(i))
With .item(i)
.SaveAsFile strFilePath
.Delete
End With
If Msg.BodyFormat = olFormatHTML Then
Msg.HTMLBody = Msg.HTMLBody & "<p>" & _
"The file was saved to: " & strFilePath & "</p>"
Else
Msg.Body = Msg.Body & vbCrLf & "The file was saved to: " & strFilePath & vbCrLf
End If
End If
Next i
End With
Msg.Save
MsgBox "Xls files are extracted from" & vbCrLf & _
"the emails in Project folder.", vbInformation
ProgramExit:
Exit Sub
ErrorHandler:
MsgBox Err.number & " - " & Err.Description
Resume ProgramExit
End Sub
Function GetFileType(ByVal fileName As String) As String
' get file extension
GetFileType = Mid$(fileName, InStrRev(fileName, ".") + 1, Len(fileName))
End Function
Private Function BuildFileName(ByRef number As Long, ByRef mlItem As Outlook.MailItem, ByRef att As Outlook.Attachment, _
Optional dateFormat As String = "m_d_yyyy-H-MM-SS") As String
'Builds file name to preferred format. Can be changed to personal
'preference.
Const strInfoDlmtr_c As String = " - "
Const lngMxFlNmLen_c As Long = 255
BuildFileName = VBA.Left$(number & strInfoDlmtr_c & _
Format$(mlItem.ReceivedTime, dateFormat) & strInfoDlmtr_c & _
mlItem.senderName & strInfoDlmtr_c & att.fileName, lngMxFlNmLen_c)
End Function