# VBA in Outlook: Use Subject as File Name to Save Attachment



## Amapola (May 4, 2010)

Good Afternoon - I need help again!

I'm trying to save Attachments from selected Outlook messages with a certain name to a certain path. I've pinched below code from the Outlook-Tips and modified it to the required path and now I'm trying to tell it to use the Subject & .pdf as the FileName (i.e. the attached File will be a pdf but that won't be in the Subject).

I've marked up my changes in bold. The error I get is "Compile error - Object required" in the line marked red.

Any idea why it doesn't like that? Any help would be appreciated. Thanks!

Public Sub SaveAttachments()
Dim objOL As Outlook.Application
Dim objMsg As Outlook.MailItem 'Object
Dim objAttachments As Outlook.attachments
Dim objSelection As Outlook.Selection
Dim i As Long
Dim lngCount As Long
Dim strFile As String
Dim strFolderpath As String
*Dim strFileName As String*
Dim strDeletedFiles As String
' Get the path to your My Documents folder
strFolderpath = CreateObject("WScript.Shell").SpecialFolders(16)
On Error Resume Next
' Instantiate an Outlook Application object.
Set objOL = CreateObject("Outlook.Application")
' Get the collection of selected objects.
Set objSelection = objOL.ActiveExplorer.Selection
' The attachment folder needs to exist
' You can change this to another folder name of your choice
' Set the Attachment folder.
strFolderpath = strFolderpath & "\Documents and Settings\305015724\My Documents\admin\Accounts\2010"

' Check each selected item for attachments.
For Each objMsg In objSelection

* 'Set FileName to Subject
Set strFileName = MailItem.Subject*

Set objAttachments = objMsg.attachments
lngCount = objAttachments.Count

If lngCount > 0 Then

' Use a count down loop for removing items
' from a collection. Otherwise, the loop counter gets
' confused and only every other item is removed.

For i = lngCount To 1 Step -1

' Get the file name.
*strFile = strFileName & ".pdf"
* 
' Combine with the path to the Temp folder.
strFile = strFolderpath & strFile

' Save the attachment as a file.
objAttachments.Item(i).SaveAsFile strFile

Next i
End If

Next

ExitSub:
Set objAttachments = Nothing
Set objMsg = Nothing
Set objSelection = Nothing
Set objOL = Nothing
End Sub


----------



## Rollin_Again (Sep 4, 2003)

The SET keyword is used to assign a reference to an object but you have dimensioned the variable as a string. Try changing the variable type to object instead.

*Dim strFileName As Object*

Rollin


----------



## Amapola (May 4, 2010)

Hi Rollin

Thanks for that. I have put that and it no longer gives an error but it doesn't do anything either. - I tried declaring it as MailItem.Property but that doesn't make a difference. No error. No action.

Further down it has what Excel would call a circular reference (that already was in the code)

' Combine with the path to the Temp folder.
strFile = strFolderpath & strFile

I have added/changed strFileName to objSubject (declared as Object) and kept strFileName As String. - I'm not sure I had the path correct so I put that outright.

This is what I have now. But still - no error, no action:

Public Sub SaveAttachments()
Dim objOL As Outlook.Application
Dim objMsg As Outlook.MailItem 'Object
Dim objAttachments As Outlook.attachments
Dim objSelection As Outlook.Selection
Dim i As Long
Dim lngCount As Long
Dim strFile As String
Dim strFolderpath As String
Dim strFileName As String
Dim objSubject As Object
Dim strDeletedFiles As String

' Get the path to your My Documents folder
'strFolderpath = CreateObject("WScript.Shell").SpecialFolders(16)
On Error Resume Next

' Instantiate an Outlook Application object.
Set objOL = CreateObject("Outlook.Application")

' Get the collection of selected objects.
Set objSelection = objOL.ActiveExplorer.Selection

' The attachment folder needs to exist
' You can change this to another folder name of your choice
' Set the Attachment folder.
strFolderpath = "D:\Documents and Settings\305015724\My Documents\admin\Accounts\2010"

' Check each selected item for attachments.
For Each objMsg In objSelection

'Set FileName to Subject
Set objSubject = MailItem.Subject

Set objAttachments = objMsg.attachments

lngCount = objAttachments.Count
If lngCount > 0 Then
' Use a count down loop for removing items
' from a collection. Otherwise, the loop counter gets
' confused and only every other item is removed.
For i = lngCount To 1 Step -1

' Get the file name.
strFileName = objSubject & ".pdf"

' Combine with the path to the Temp folder.
strFile = strFolderpath & strFileName

' Save the attachment as a file.
objAttachments.Item(i).SaveAsFile strFile
Next i
End If
Next

ExitSub:
Set objAttachments = Nothing
Set objMsg = Nothing
Set objSelection = Nothing
Set objOL = Nothing
End Sub


----------



## Rollin_Again (Sep 4, 2003)

Can you provide a little more detail on how this macro will work? Will you be manually selecting the messages you want the macro to process or will you be processing all messages located in a particular subfolder? You also have the option to evaluate each email as it comes in and determine whether or not to process automatically based on a particular subject or sender.

Rollin


----------



## Amapola (May 4, 2010)

Hi Rollin

I will be manually selecting the invoices. And I understand that was how the code was meant to work as well.

Basically it's scanned documents, so there will be a whole batch and it will be easy to select.

Thanks, Christine


----------



## Rollin_Again (Sep 4, 2003)

I've modified your code slightly. Try the updated code below.


```
Public Sub SaveAttachments()
Dim objOL As Outlook.Application
Dim objMsg As Outlook.MailItem 'Object
Dim objAttachments As Outlook.Attachments
Dim objSelection As Outlook.Selection
Dim i As Long
Dim lngCount As Long
Dim strFile As String
Dim strFolderpath As String
Dim strFileName As String
Dim objSubject As String
Dim strDeletedFiles As String

' Get the path to your My Documents folder
'strFolderpath = CreateObject("WScript.Shell").SpecialFolders(16)
On Error Resume Next

' Instantiate an Outlook Application object.
Set objOL = CreateObject("Outlook.Application")

' Get the collection of selected objects.
Set objSelection = objOL.ActiveExplorer.Selection

' The attachment folder needs to exist
' You can change this to another folder name of your choice
' Set the Attachment folder.
strFolderpath = "D:\Documents and Settings\305015724\My Documents\admin\Accounts\2010"

' Check each selected item for attachments.
For Each objMsg In objSelection

'Set FileName to Subject
objSubject = objMsg.Subject

Set objAttachments = objMsg.Attachments

lngCount = objAttachments.Count
If lngCount > 0 Then
' Use a count down loop for removing items
' from a collection. Otherwise, the loop counter gets
' confused and only every other item is removed.
For i = lngCount To 1 Step -1

' Get the file name.
strFileName = objSubject & ".pdf"

' Combine with the path to the Temp folder.
strFile = strFolderpath & strFileName
Debug.Print strFile
' Save the attachment as a file.
objAttachments.Item(i).SaveAsFile strFile
Next i
End If
Next

ExitSub:
Set objAttachments = Nothing
Set objMsg = Nothing
Set objSelection = Nothing
Set objOL = Nothing
End Sub
```
Rollin


----------



## Amapola (May 4, 2010)

Thanks, Rollin. It's working. I only made one more change - a "\" at the end of the path as it was not using folder 2010\ but adding that to the name and saving it the folder above.

So the working code now looks like this:


```
Public Sub SaveAttachments()
Dim objOL As Outlook.Application
Dim objMsg As Outlook.MailItem 'Object
Dim objAttachments As Outlook.Attachments
Dim objSelection As Outlook.Selection
Dim i As Long
Dim lngCount As Long
Dim strFile As String
Dim strFolderpath As String
Dim strFileName As String
Dim objSubject As String
Dim strDeletedFiles As String
' Get the path to your My Documents folder
'strFolderpath = CreateObject("WScript.Shell").SpecialFolders(16)
On Error Resume Next
' Instantiate an Outlook Application object.
Set objOL = CreateObject("Outlook.Application")
' Get the collection of selected objects.
Set objSelection = objOL.ActiveExplorer.Selection
' The attachment folder needs to exist
' You can change this to another folder name of your choice
' Set the Attachment folder.
strFolderpath = "D:\Documents and Settings\305015724\My Documents\admin\Accounts\2010\"
' Check each selected item for attachments.
For Each objMsg In objSelection
'Set FileName to Subject
objSubject = objMsg.Subject
Set objAttachments = objMsg.Attachments
lngCount = objAttachments.Count
If lngCount > 0 Then
' Use a count down loop for removing items
' from a collection. Otherwise, the loop counter gets
' confused and only every other item is removed.
For i = lngCount To 1 Step -1
' Get the file name.
strFileName = objSubject & ".pdf"
' Combine with the path to the Temp folder.
strFile = strFolderpath & strFileName
Debug.Print strFile
' Save the attachment as a file.
objAttachments.Item(i).SaveAsFile strFile
Next i
End If
Next
ExitSub:
Set objAttachments = Nothing
Set objMsg = Nothing
Set objSelection = Nothing
Set objOL = Nothing
End Sub
```


----------



## Amapola (May 4, 2010)

Thanks so much for your help, Rollin! I hope one day I manage to write working code myself!


----------



## Rollin_Again (Sep 4, 2003)

Glad you got it sorted! It would probably be wise to add some error handling in the event that the subject line contains an invalid character that would prevent the subject line text from being used as the filename.

Regards,
Rollin


----------



## Amapola (May 4, 2010)

Hi Rollin

Are you working night shift ;-)? - I put the Subject in when I scan it so it's always letters and numbers, no funny characters. - But you are right, if I use it for something else, I should have it. I'll have a think about it in a quiet moment.

Thanks for all your help. Very much appreciated. - Almost weekend where I am. Have a good one!

Christine


----------

