Outlook 2010:宏:使用动态文件名添加附件

时间:2014-02-07 18:02:56

标签: vba outlook outlook-vba

计划:Outlook 2010年 操作系统: Win8
VBA技能:新手

要求
我有一个template.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

我想要什么 使用通配符调用文件。

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

这样它总是会选择帐户文件,但是日期或动态通配符将在当前月份或其他日期中显示。

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

注意
我已经尝试了以下内容,但它只附加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

请告知。

1 个答案:

答案 0 :(得分:1)

我找到了问题的答案。

    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