Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Saving Certain Attachments in Email

Status
Not open for further replies.

calvarado536

Technical User
Aug 14, 2014
10
0
0
US
Hello everyone.

I'm trying to create a rule that runs a code on a certain email that comes into my inbox. This email always has 2 files in csv form. They are called non-delivered_email_report_YYYMMDD and Batch_Failed_Report_YYYYMMDD. I would only like to save the non-delivered_email_report but in txt form. This is the code that I have so far but this saves all the attachments in the form they are. How can I get it to save only the email report and in txt form? Much appreciated.

Code:
Public Sub saveAttach(itm As Outlook.MailItem)
Dim objAtt As Outlook.Attachment
Dim saveFolder As String
saveFolder = "I:\My Documents\email report"
     For Each objAtt In itm.Attachments
          objAtt.SaveAsFile saveFolder & "\" & objAtt.DisplayName
          Set objAtt = Nothing
     Next
End Sub
 
csv IS just a text file, you can open it in Notepad and see it for yourself.

Try:
Code:
objAtt.SaveAsFile [blue]Replace([/blue]saveFolder & "\" & objAtt.DisplayName[blue], ".csv", ".txt")[/blue]

Have fun.

---- Andy

A bus station is where a bus stops. A train station is where a train stops. On my desk, I have a work station.
 
Yes I know the csv file is a txt file but I need it to be a text file so when I run an import data code in excel I can parse the entire text file.

The reason why is because the csv file comes with 3 columns of the information and it's different every day so it's hard to delimit it with 1 code so saving it as text is easier but thank you for the code to save it as txt! I appreciate it.

Is there a way where I can only save one of the attachments and not the other? just based off the beginning of the name?
 
If “They are (ALAWYS) called non-delivered_email_report_YYYMMDD and Batch_Failed_Report_YYYYMMDD” you may do:

Code:
Public Sub saveAttach(itm As Outlook.MailItem)
Dim objAtt As Outlook.Attachment
Dim saveFolder As String
saveFolder = "I:\My Documents\email report"
     For Each objAtt In itm.Attachments[blue]
       If Left(objAtt.DisplayName, 26) = "non-delivered_email_report" then[/blue]
          objAtt.SaveAsFile Replace(saveFolder & "\" & objAtt.DisplayName, ".csv", ".txt") 
          Set objAtt = Nothing[blue]
       End If[/blue]
     Next
End Sub

Have fun.

---- Andy

A bus station is where a bus stops. A train station is where a train stops. On my desk, I have a work station.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top