使用不同的文件名为outlook添加附件

Aci*_*Agc 1 excel vba outlook-vba

我有一个名为"家庭音频规划"的excel文件(28-3-2013)日期将不断变化,但文本将是相同的.那么如何将这些文件附加到Outlook?

Sub Test()

    Dim OutApp As Object
    Dim OutMail As Object
    Set OutApp = CreateObject("Outlook.Application")
    Set OutMail = OutApp.CreateItem(0)
    With OutMail
        .To = ""
        .CC = ""
        .BCC = ""
        .Subject = "This is the Subject line"
        .Body = "Hello World!"

        .Attachments.Add ("C:\Users\Desktop\Today\Home Audio for Planning   (28-3-2013).xlsx")

        .Display
    End With
    On Error GoTo 0

    Set OutMail = Nothing
    Set OutApp = Nothing

    With Application
        .ScreenUpdating = True
        .EnableEvents = True
    End With

End Sub
Run Code Online (Sandbox Code Playgroud)

小智 7

尝试下面的代码:strLocation将动态生成.您可以将此变量传递给附件.生成的文件名将类似于Planning_28-03-2013.xlsx的Home Audio

Sub Test()
    Dim strLocation As String

    Dim OutApp As Object
    Dim OutMail As Object
    Set OutApp = CreateObject("Outlook.Application")
    Set OutMail = OutApp.CreateItem(0)
    With OutMail
        .To = ""
        .CC = ""
        .BCC = ""
        .Subject = "This is the Subject line"
        .Body = "Hello World!"

        strLocation = "C:\Users\Desktop\Today\Home Audio for Planning" & Format(Now(), "_DD-MM-YYYY") & ".xlsx"
        .Attachments.Add (strLocation)
        .Display
    End With
    On Error GoTo 0

    Set OutMail = Nothing
    Set OutApp = Nothing

    With Application
        .ScreenUpdating = True
        .EnableEvents = True
    End With

End Sub
Run Code Online (Sandbox Code Playgroud)