【问题标题】:Outlook 2010: Macro: Add attachments with dynamic file nameOutlook 2010:宏:添加具有动态文件名的附件
【发布时间】:2020-06-30 07:08:03
【问题描述】:

程序: 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

请指教。

【问题讨论】:

    标签: vba outlook


    【解决方案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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-08-29
      • 2016-11-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-28
      • 1970-01-01
      相关资源
      最近更新 更多