Add attachment with varying date in file name to Outlook mail

前端 未结 5 1501
无人共我
无人共我 2020-12-10 23:25

I have an Excel file named \"Home Audio for Planning (28-3-2013).

The date will change every day but the text will be the same.

How do I attach those files t

相关标签:
5条回答
  • 2020-12-11 00:06

    Easy,

      .Attachments.Add ("C:\Users\Desktop\Today\Home Audio for Planning (" & FORMAT(DATE,DD-MM-YYYY)") 
    
    0 讨论(0)
  • 2020-12-11 00:19

    Try below code : strLocation will be generated dynamically. You can just pass this variable to your attachments. File name generated would be like Home Audio for 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
    
    0 讨论(0)
  • 2020-12-11 00:24
    With OutMail
        .To = ""
        .BodyFormat = olFormatHTML  '---Default
        .Attachments.Add ("C:\Users\Desktop\Test.txt")
        .Display
    End With
    

    If not.BodyFormat = olFormatHTMLfile will be attached in the mail body

    0 讨论(0)
  • 2020-12-11 00:26

    Did you try to change the attachemnt name dynamic. For ex;

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

    and you can set the time variable before to match the date of the date in the required format.

    Cheers

    0 讨论(0)
  • 2020-12-11 00:27

    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

    0 讨论(0)
提交回复
热议问题