将电子邮件中的附件保存到每月更改的文件夹中 [英] Save attachment from an email in to a folder that changes every month

查看:21
本文介绍了将电子邮件中的附件保存到每月更改的文件夹中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 Outlook 中获取一个 VBA 宏,该宏会将电子邮件的附件保存到特定文件夹(每个月都会更改),并将上个月收到的 YYYYMM 添加到文件名中.

Outlook 规则标识电子邮件标头包含来自某人的NTMR".

当它这样做时,它会运行脚本,将附件保存在一个文件夹中.

因此,当宏标识收到电子邮件的月份时,它会保存在后一个月的文件夹中.例如:

在 DD/04/17 收到的电子邮件为这是给您的 NTMR 文件",它会将文件保存在 201703 父文件夹中的文件夹中,名称为 NTMR - 201703

所以文件的路径是 C:UsersalitalhDownloadsTest201703Source FilesNTMR 201703

我想出了一个 follownig 宏 - 请告诉我如何修复它?

Public Sub saveAttachtoDisk(itm As Outlook.MailItem)Dim objAtt 作为 Outlook.Attachment将保存文件夹调暗为字符串将日期格式调暗为字符串saveFolder = "C:UsersalitalhDownloadsTest"日期格式 = 格式(现在,yyyymm" - 1, 1)对于每个 objAtt 在 itm.AttachmentsobjAtt.SaveAsFile saveFolder &" &日期格式 &" &源文件"&" &objAtt.DisplayName &日期格式设置 objAtt = 无下一个结束子

如何从标题中去除 NTMR 并将其放入文件名中?

我有另一个宏可以在电子邮件之前设置文件夹,因此我们不需要创建另一个文件夹

解决方案

MSDN 拆分功能

主题行上的示例这是为您准备的 NTMR 文件 按空格字符分割 (" ")

代码示例

子示例()将项目变暗为 Outlook.mailitem设置项目 = ActiveExplorer.Selection.Item(1)Debug.Print Item.subject ' 在立即窗口上打印 (Ctrl+G)Item.subject = Split(Item.subject, " ")(3)Debug.Print Item.subject ' 在立即窗口上打印 (Ctrl+G)结束子

你的主题 = (Here)(1) (is)(2) (the)(3) (NTMR)(4) (file)(5) (for)(6) (you)(7)

现在 Split(subject line), "space")(3) 同时分配给字符串变量

Dim FileName As StringFileName = Split(Item.subject, " ")(3)

FileName

替换 objAtt.DisplayName<小时>

Dim FileName As String对于每个 objAtt 在 itm.AttachmentsobjAtt.SaveAsFile saveFolder &" &_日期格式 &" &_源文件"&" &文件名 &日期格式下一个

<块引用>

默认情况下,或当 Limit 等于 -1 时,Split 函数会在每次出现分隔符字符串时拆分输入字符串,并在数组中返回 子字符串.
当 Limit 参数大于零时,Split 函数在分隔符的第一个 Limit-1 出现处拆分字符串,并返回一个包含结果子字符串的数组.
例如,Split("a:b:c", ":") 返回数组 {"a", "b", "c"},
Split("a:b:c", ":", 2) 返回数组 {"a", "b:c"}.

<小时>

要获得上个月的信息,请尝试 DateAdd 函数

示例

选项显式公共子示例()Dim PrevMonth As StringPrevMonth = Format(DateAdd("m", -1, Date), "yyyymm")Debug.Print PrevMonth结束子

<块引用>

一些值得在其他上下文中探索的有用日期函数包括 DateDiffDatePartDateSerial、日、月、年和 IsDate.IsDate(检查字符串是否为有效日期)对于诸如 UserForms 之类的内容特别有用,您可能希望强制用户输入某个文本框中的有效日期.

<小时>

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.

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:

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

So the path of the file will be C:UsersalitalhDownloadsTest201703Source FilesNTMR 201703

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:UsersalitalhDownloadsTest"
    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

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

解决方案

Work with MSDN Split Function

Example on your Subject line Here is the NTMR file for you split it by space character (" ")

Code Example

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

Your subject = (Here)(1) (is)(2) (the)(3) (NTMR)(4) (file)(5) (for)(6) (you)(7)

Now Split(subject line), "space")(3) While assigning to string variable

Dim FileName As String
FileName = Split(Item.subject, " ")(3)

Replace objAtt.DisplayName with FileName


Dim FileName As String
For Each objAtt In itm.Attachments
    objAtt.SaveAsFile saveFolder & "" & _
                       dateFormat & "" & _
                    "Source Files" & "" & FileName & dateFormat
Next

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"},
while Split("a:b:c", ":", 2) returns the array {"a", "b:c"}.


To get previous month try DateAdd Function

Example

Option Explicit
Public Sub Example()
    Dim PrevMonth As String

    PrevMonth = Format(DateAdd("m", -1, Date), "yyyymm")
    Debug.Print PrevMonth
End Sub

Some useful date functions worth exploring in other contexts include DateDiff, DatePart, DateSerial, Day, Month, Year, and IsDate. IsDate (which checks whether a string is a valid date) is particularly useful for things like UserForms where you may want to force the user to type a valid date into a certain textbox.


这篇关于将电子邮件中的附件保存到每月更改的文件夹中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆