vba Outlook 2010:宏:添加带有动态文件名的附件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21634702/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 01:49:16  来源:igfitidea点击:

Outlook 2010: Macro: Add attachments with dynamic file name

vbaoutlookoutlook-vba

提问by MrsAdmin

Program:Outlook 2010
OS:Win8
VBA Skill:Novice

程序:Outlook 2010
操作系统:Win8
VBA 技能:新手

Requirement
I have a template.oft for sales reports which I call through this Macro.
The macro attaches a file with a static & secondly, a dynamic name.
I want to attach the dynamic file using another variable of some sort.

要求
我有一个模板.oft 用于我通过此宏调用的销售报告。
该宏附加了一个静态文件,其次是动态名称。
我想使用某种类型的另一个变量附加动态文件。

'Working File
    Sub zzzAccs()
        Dim newItem As Outlook.mailItem
        Dim dateFormat As String
            dateFormat = Format(Now, "YYYYMMDD")

        Set newItem = CreateItemFromTemplate(":\location\zzz accs.oft")

            newItem.Attachments.Add ":\location\" & "zzz sales_" & Format(Now, "YYYYMMDD") & ".pdf"

        'Attachment 2 - always will have the same name, general notice/reminder
            newItem.Attachments.Add ":\location\zzz Notice.pdf"

            newItem.Display
    End Sub

What I wantTo call a file with a wildcard.

我想用通配符调用文件。

It will always have:
":\location\zzz Acc (wildcard, namely date within the last month).pdf"

它总是有:
“:\location\zzz Acc(通配符,即上个月内的日期).pdf”

This way it will always pick the Account file, but the date or dynamic wildcard will either be dated in the current month, or something different.

这样它总是会选择帐户文件,但日期或动态通配符将在当月或不同的月份注明日期。

eg: ":\location\zzz Acc 20140201.pdf" (current month, but not a defined 'date from now') ":\location\zzz Acc statement Feb 2014.pdf" (will change based on what the file is for).

例如:“:\location\zzz Acc 20140201.pdf”(当前月份,但不是定义的“从现在开始的日期”)“:\location\zzz Acc statement Feb 2014.pdf”(将根据文件的用途而改变)。

Note
I have since tried the following, however it will only attach 1 file, not all files with wildcards:

注意
我已经尝试了以下操作,但是它只会附加 1 个文件,而不是所有带有通配符的文件:

'source:    http://stackoverflow.com/a/13729215/2337102
    Dim strPath As String
    Dim strFilter As String
    Dim strFile As String

    strPath = "E:\My Documents\"      'Edit to your path
    strName = "test_"  'added in file core name as I didn't want all the .pdf attached
    strFilter = "*.pdf"
    strFile = Dir(strPath & strName & strFilter)

'New email message from Template
    Set newItem = CreateItemFromTemplate("E:location\test.oft")

'File Locations
    newItem.Attachments.Add "E:\My Documents\" & "test - " & Format(Now, "YYYYMMDD") & ".pdf"
    newItem.Attachments.Add "E:\My Documents\test.pdf"  

    newItem.Attachments.Add (strPath & strFile)
    'the above line only attached 1 file, not 3 that were named according to the str Rules eg:  
    'test_2014; test_20140131; test_agreement

Please advise.

请指教。

回答by MrsAdmin

I have found the answer to my question.

我已经找到了我的问题的答案。

    Sub AccswithOFT()
        'source:    http://bit.ly/1jzoTy7  (slipstick hyperlink templates)
        'source:    http://bit.ly/1dlG0Qr  (mrexcel dynamic-attachment)
            '.Attachments.Add "G:\Financial Planning\" & Format(PrevDay, "yyyy") & " Daily Sales\Production\" & Format(PrevDay, "mmmm") & "\Daily Sales " & Format(PrevDay, "mmmm yyyy") & " by Channel_" & Format(PrevDay, "mmddyy") & ".pdf"
        'source:    http://bit.ly/1magjbd  (stackoverflow strLocation)
        'source:    http://bit.ly/1g9fxG7  (html body text)
        'source:    http://bit.ly/1kEdRb0  (How to add signature, with my solution)
        'source:    http://bit.ly/1nDrKnd  (excelforum HTML with "")
        'source:    http://bit.ly/1fqqYpd  (mrexcel HTML with ' ')

    Dim newItem As Outlook.MailItem
    Dim dateFormat As String
        dateFormat = Format(Now, "YYYYMMDD")
    Dim sig As String  

    'New email message from Template
    Set newItem = CreateItemFromTemplate("D:\yourlocation\accstest.oft")

    Dim strPath As String
    Dim strFilter As String
    Dim strFile As String 'might need to use as variant
    Dim strName As String

    'source:  http://bit.ly/1jqUmPS   (Based off Unavergae Guy post)
    strPath = "D:\My Documents\"      'Edit to your full path
    strName = "test_"  'added as I don't want all .pdf to attach
    strFilter = "*.pdf"
    strFile = Dir(strPath & strName & strFilter)

        'source:    http://bit.ly/1b923u9  (outlookforums loop for files)
    While (strFile <> "")
        If InStr(strFile, "test") > 0 Then
            'MsgBox "found " & strFile   'I don't use the MsgBox
        newItem.Attachments.Add (strPath & strFile)
        End If
    strFile = Dir
    Wend

        newItem.Display
    End Sub