PDA

View Full Version : Autosave Attachements in Outlook 2010



JV28132
06-07-2011, 07:46 AM
Hello - I am trying to autosave my 2010 Outlook csv files automatically when they are sent via email.

I am trying to install the macro within the This Outlook Session but it is not working properly. There is an error being received in regards to the Personal Folders. I'm not sure what a "personal Folder" is referring to.


Set TargetFolderItems = ns.Folders.Item( _
"Personal Folders").Folders.Item("Temp").Items
I think I need to modify this to reflect where I would like the attachment to save but I'm not sure. Can you please expand on this term? Where I can find the personal folders and what it is used for?

I'm new to outlook vba so please be kind. I appreciate any guidance and help. Oh and the original post from Killian that I am referring to is posted below.. Thanks again!

The original post was as follows:


'########################################################################## #####
'### Module level Declarations
'expose the items in the target folder to events
Option Explicit
Dim WithEvents TargetFolderItems As Items
'set the string constant for the path to save attachments
Const FILE_PATH As String = "C:\Temp\TEST\"

'########################################################################## #####
'### this is the Application_Startup event code in the ThisOutlookSession module
Private Sub Application_Startup()
'some startup code to set our "event-sensitive" items collection
Dim ns As Outlook.NameSpace
'
Set ns = Application.GetNamespace("MAPI")
Set TargetFolderItems = ns.Folders.Item( _
"Personal Folder File").Folders.Item("Temp").Items

End Sub

'########################################################################## #####
'### this is the ItemAdd event code
Sub TargetFolderItems_ItemAdd(ByVal Item As Object)
'when a new item is added to our "watched folder" we can process it
Dim olAtt As Attachment
Dim i As Integer

If Item.Attachments.Count > 0 Then
For i = 1 To Item.Attachments.Count
Set olAtt = Item.Attachments(i)
'save the attachment
olAtt.SaveAsFile FILE_PATH & olAtt.FileName

'if its an Excel file, pass the filepath to the print routine
If UCase(Right(olAtt.FileName, 3)) = "XLS" Then
PrintAtt (FILE_PATH & olAtt.FileName)
End If
Next
End If

Set olAtt = Nothing

End Sub

'########################################################################## #####
'### this is the Application_Quit event code in the ThisOutlookSession module
Private Sub Application_Quit()

Dim ns As Outlook.NameSpace
Set TargetFolderItems = Nothing
Set ns = Nothing

End Sub

'########################################################################## #####
'### print routine
Sub PrintAtt(fFullPath As String)

Dim xlApp As Excel.Application
Dim wb As Excel.Workbook

'in the background, create an instance of xl then open, print, quit
Set xlApp = New Excel.Application
Set wb = xlApp.Workbooks.Open(fFullPath)
wb.PrintOut
xlApp.Quit

'tidy up
Set wb = Nothing
Set xlApp = Nothing

End Sub



Thank you,
JV28132

Charlize
06-24-2011, 01:19 AM
It depends where your watch-folder is located. Is it a subfolder of your inbox folder or not. If it is, you declare a folder variable and use the getdefaultfolder stuff to get the default inbox. If it's not, you add .parent after the code for getting to the toplevel. Little example for the application_startup sub ...
Dim ns As Outlook.NameSpace
Dim myfolder As Outlook.Folder
Dim targetfolderitems As Object

Set ns = Application.GetNamespace("MAPI")
'This is for the subfolder of inbox - Delayed is folder we want to watch
Set myfolder = ns.GetDefaultFolder(olFolderInbox).Folders("Delayed")
Set targetfolderitems = myfolder.Items
MsgBox targetfolderitems.Count
'This is for a folder on the same level as your inbox folder - Spam is folder we want to watch
Set myfolder = ns.GetDefaultFolder(olFolderInbox).Parent.Folders("Spam")
Set targetfolderitems = myfolder.Items
MsgBox targetfolderitems.Count
Charlize