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

时间:2013-03-28 06:18:12

标签: excel-vba outlook-vba vba excel

我有一个名为“Home Audio for Planning(28-3-2013)”的excel文件 日期将不断变化,但文本将是相同的。那么如何将这些文件附加到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

5 个答案:

答案 0 :(得分:7)

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

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

答案 1 :(得分:1)

易,

  .Attachments.Add ("C:\Users\Desktop\Today\Home Audio for Planning (" & FORMAT(DATE,DD-MM-YYYY)") 

答案 2 :(得分:0)

您是否尝试更改动态附件名称?对于前者;

.Attachments.Add ("C:\Users\Desktop\Today\Home Audio for Planning   (" + timeVariable  + ").xlsx")

您可以设置时间变量,以匹配所需格式的日期日期。

干杯

答案 3 :(得分:0)

Private Sub Application_ItemSend(ByVal Item As Object,Cancel As Boolean)

Dim strSubject As String
Dim StrSub As Integer
Dim AttachCnt As Integer


AttachCnt = Item.Attachments.Count
strSubject = Item.Subject
StrSub = Len(strSubject)
strBody = Item.Body
strBod = InStr(1, UCase(strBody), "ATTACH")
cnsolidateMsg = ""

If strBod <> 0 And AttachCnt = 0 Then
        cnsolidateMsg = cnsolidateMsg & "Attachment is Null." & vbNewLine
End If

If StrSub = 0 Then
    cnsolidateMsg = cnsolidateMsg & "Subject is Empty." & vbNewLine
End If
If UCase(Trim(strSubject)) = "FW:" Then
    cnsolidateMsg = cnsolidateMsg & "Forward mail subject is empty." & vbNewLine
End If
If UCase(Trim(strSubject)) = "RE:" Then
    cnsolidateMsg = cnsolidateMsg & "Reply mail subject is empty." & vbNewLine
End If

If cnsolidateMsg <> Empty Then
    If MsgBox(cnsolidateMsg & vbNewLine & "Are you sure you want to send the Mail?", vbYesNo + vbQuestion + vbMsgBoxSetForeground, "Check for send mail") = vbNo Then
        Cancel = True
    End If
End If

End Sub

答案 4 :(得分:0)

}else
{
return;
}

如果没有With OutMail .To = "" .BodyFormat = olFormatHTML '---Default .Attachments.Add ("C:\Users\Desktop\Test.txt") .Display End With 文件将被附加在邮件正文中