Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

vba - Outlook rename attachments and save to folder

I have a script that I found that currently saves attachments but I also need it to rename those attachments to the same name.

For a little background I am creating a system that updates inventory on one of my websites. To do this I get a report(CSV) from our ERP with all my item numbers and inventory counts.

They are emailed to me but the attached CSV always has a name that ends in a different number.

I need the file to have the same name every time because the script I have saves that CSV to a folder and I have a FTP program that auto FTPs that CSV to my web server.

From here I have a PHP script on a cron job that runs through the CSV and updates the quantity on my products.

Believe me, if my ERP had an API this would be so much easier but no luck on that.

Anyhow, my issue lies in the naming of the attachment. The script below saves it but I need it to also strip the numbers from the name or just name it to 'CPNINVTRUM'.

Public Sub saveAttachtoDisk(itm As Outlook.MailItem)
     Dim objAtt As Outlook.Attachment
     Dim saveFolder As String
     saveFolder = "C:PATH"
     For Each objAtt In itm.Attachments
     objAtt.SaveAsFile saveFolder & "" & objAtt.DisplayName
     Set objAtt = Nothing
     Next
End Sub

The attachment is always in this form: CPNINVTRUM###.csv. The ### signifies the the 3 digit random number that is generated by the ERP.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Change the save line to

objAtt.SaveAsFile saveFolder & "CPNINVTRUM.csv"

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...