Saving Attachment from Outlook to a specified location on my computer
Good morning all!!!
I am looking for some code that will search for a specific subject line and then take the attachment and save as an excel worksheet to a specified destination. I have been playing with the following code...
Code:
Sub SaveFile()
Dim olApp As Outlook.Application
Dim olNs As Namespace
Dim Fldr As MAPIFolder
Dim olMi As MailItem
Dim olAtt As Attachment
Dim Mypath As String
Dim i As Long
Dim InStr As Object
Set olApp = Outlook.Application
Set olNs = olApp.GetNamespace("MAPI")
Set Fldr = olNs.GetDefaultFolder(olFolderInbox)
Set MoveToFldr = Fldr.Folders("Repair Reports")
Mypath = "S:\Departments\Service & Production\Public\Repair Reports"
For i = Fldr.Items.Count To 1 Step -1
If olMi.Subject = "Daily repair report" Then
For Each olAtt In olMi.Attachments
If olAtt.Filename = "_repairs.csv" Then
Attachment.SaveAsFile Mypath & olMi.SenderName & ".xlsx"
End If
Next olAtt
olMi.Save
olMi.Move MoveToFldr
End If
Next i
Set olAtt = Nothing
Set olMi = Nothing
Set Fldr = Nothing
Set MoveToFldr = Nothing
Set olNs = Nothing
Set olApp = Nothing
End Sub
I keep getting block errors and even when I thought I resolved those errors and the macro runs, nothing happens. Here is some additional background. I get an email every Mon through Friday at 9am and the attachment is dated for the day before.
Ex.
I received the email this morning(8/15) and the attachment is named...8-14-2012_repair report.csv
I would like to modify the code so it saves the file as just the date and as an excel file not a CSV file.
I look forward to your assistance!!!!!
Thanks!!!!
Re: Saving Attachment from Outlook to a specified location on my computer
renaming the file .xlsx, does not make the file an excel workbook, to do that you would need to open in excel then saveAs workbook, by specifying the correct file format parameter
this could be done within your code, but just get it to save first, when that works you can convert the saved file
Quote:
If olAtt.Filename = "_repairs.csv" Then
of course while the attachment name may contain the string, it is never = to that, you should try instr to find if that string is contained within the name
or you could try
Code:
If olAtt.Filename = format(date - 1,"m-d-yyyy") & "_repairs.csv" Then
though this would likely not work for monday