将附件从电子邮件保存到每月更改的文件夹中 [英] Save attachment from an email in to a folder that changes every month
问题描述
我正在尝试在Outlook中获取VBA宏,该宏会将电子邮件的附件保存到特定文件夹(该文件夹每月更改),并将上个月收到的YYYYMM添加到文件名中.
i 'm trying to get a VBA macro in Outlook that will save an email's attachment to a specific folder (that changes every month) and add the YYYYMM of prior month received to the file name.
Outlook规则确定电子邮件标头包含来自某人的"NTMR".
The outlook rule identifies that an email header contains 'NTMR' from a person.
这样做时,它将运行脚本,将附件保存在文件夹中.
And when it does so, it runs the script where it saves the attachment in a folder.
因此,当宏标识收到电子邮件的月份时,它将保存在比该日期晚一个月的文件夹中.例如:
So when the macro identifies the month of the email received, it saves in the folder that is one month behind. For instance:
在DD/04/17收到的电子邮件是这里是您的NTMR文件",它将把文件保存在201703父文件夹内的文件夹中,作为NTMR-201703
email received on DD/04/17 as 'Here is the NTMR file for you', it will save the file in a folder within 201703 parent folder as NTMR - 201703
因此文件的路径将为C:\ Users \ alitalh \ Downloads \ Test \ 201703 \ Source Files \ NTMR 201703
So the path of the file will be C:\Users\alitalh\Downloads\Test\201703\Source Files\NTMR 201703
我想出了follownig宏-请告知我该如何修复它?
I have come up with the follownig macro - please advise as To how i can fix it?
Public Sub saveAttachtoDisk(itm As Outlook.MailItem)
Dim objAtt As Outlook.Attachment
Dim saveFolder As String
Dim dateFormat As String
saveFolder = "C:\Users\alitalh\Downloads\Test"
dateFormat = Format(Now, "yyyymm" - 1, 1)
For Each objAtt In itm.Attachments
objAtt.SaveAsFile saveFolder & "\" & dateFormat & "\" & "Source Files" & "\" & objAtt.DisplayName & dateFormat
Set objAtt = Nothing
Next
End Sub
我如何从标题中剥离NTMR并将其放入文件名中?
how can i strip out the NTMR from the header and put it in the filename?
我还有另一个宏,用于在发送电子邮件之前设置文件夹,因此我们无需创建另一个文件夹
I have another macro that sets up the folder prior to the email so we don't need to create another folder
推荐答案
使用 主题行上的示例 Here is the NTMR file for you
用空格字符 (" ")
代码示例
Sub Example()
Dim Item As Outlook.mailitem
Set Item = ActiveExplorer.Selection.Item(1)
Debug.Print Item.subject ' Print on Immediate Window (Ctrl+G)
Item.subject = Split(Item.subject, " ")(3)
Debug.Print Item.subject ' Print on Immediate Window (Ctrl+G)
End Sub
您的主题= (Here)(1)
(is)(2)
(the)(3)
(NTMR)(4)
(file)(5)
(for)(6)
(you)(7)
现在 Split(subject line), "space")(3)
分配给字符串变量时
Dim FileName As String
FileName = Split(Item.subject, " ")(3)
将 objAtt.DisplayName
替换为 FileName
Dim FileName As String
For Each objAtt In itm.Attachments
objAtt.SaveAsFile saveFolder & "\" & _
dateFormat & "\" & _
"Source Files" & "\" & FileName & dateFormat
Next
默认情况下,或者当Limit等于-1时,Split函数在每次出现分隔符字符串时都会拆分输入字符串,并在数组中返回 substrings .
当Limit参数大于零时,Split函数将在分隔符的第一个 Limit-1 出现时对字符串进行拆分,并返回包含结果子字符串的数组.
例如,Split("a:b:c", ":") returns the array {"a", "b", "c"}
,
而Split("a:b:c", ":", 2) returns the array {"a", "b:c"}
.
By default, or when Limit equals -1, the Split function splits the input string at every occurrence of the delimiter string, and returns the substrings in an array.
When the Limit parameter is greater than zero, the Split function splits the string at the first Limit-1 occurrences of the delimiter, and returns an array with the resulting substrings.
For example,Split("a:b:c", ":") returns the array {"a", "b", "c"}
,
whileSplit("a:b:c", ":", 2) returns the array {"a", "b:c"}
.
要获取上个月的数据,请尝试 示例
Option Explicit
Public Sub Example()
Dim PrevMonth As String
PrevMonth = Format(DateAdd("m", -1, Date), "yyyymm")
Debug.Print PrevMonth
End Sub
在其他情况下值得探讨的一些有用的日期函数包括
DateDiff
,DatePart
,DateSerial
,日,月,年和IsDate
.IsDate
(检查字符串是否为有效日期)对于诸如UserForms
之类的事情特别有用,在这种情况下,您可能要强制用户在特定文本框中键入有效日期.
Some useful date functions worth exploring in other contexts include
DateDiff
,DatePart
,DateSerial
, Day, Month, Year, andIsDate
.IsDate
(which checks whether a string is a valid date) is particularly useful for things likeUserForms
where you may want to force the user to type a valid date into a certain textbox.
这篇关于将附件从电子邮件保存到每月更改的文件夹中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!