Sending Emails with Customized Attachment File Names

Sending Emails with Customized Attachment File Names

Suppose you need to make a program to send emails. However, you need the emails to contain attachments, the names of which will be generated when the attachment file has been saved, and which will contain the date and time the file was saved.

Private Sub cmd_submit_Click()   'Dim the strings for use later on   Dim confirm As String   Dim Tijd As String   Dim Datum As String   Dim saveFile As String   'Set the tijd(time) and datum(date) string when its going to be saved.   Tijd = Format$(Time, "hh-nn-ss")   Datum = Format$(Date, "dd-mm-yy")   'Set the path where the file needs to be saved   saveFile = App.Path & "/offerten/Kasten_Time" & Tijd & _      "_Date" & Datum & ".txt"   On Error GoTo mailerr:           'Ask if they are sure to send it   confirm = MsgBox("Are you sure that you want to do this?", vbYesNo + vbInformation)       If confirm = vbYes Then      'If they say Yes      'Save the file(check the function below)      SaveGrid saveFile, MSFlexGrid1         MAPISession1.SignOn      If MAPISession1.SessionID <> 0 Then         With MAPIMessages1            .SessionID = MAPISession1.SessionID            .Compose            'Set the attachmentname, this is the one that will be            ' displayed on the attachment textbox in outlook            .AttachmentName = "Kasten_Time" & Tijd & "_Date" & Datum & ".txt"            'This is the path to the actual file            .AttachmentPathName = App.Path & "/offerten/Kasten_Time" & Tijd & _                "_Date" & Datum & ".txt"            'This is the email adress of the one who is supposed to get the mail            .RecipAddress = "[email protected]"              'This is the name that will be shown in the To: textbox            .RecipDisplayName = "Kris Kleykens"            'This is the subject of the email            .MsgSubject = "Time: " & Tijd & " Date: " & Datum                        'This is the body of the email            .MsgNoteText = Note.txt            '.Send true shows the email before it's sent.            ' False doesn't show this            .Send True         End With      End If                            mailerr:   'If they say no   Else   End If   DoEvents   MAPISession1.SignOffEnd SubFunction SaveGrid(sFileName As String, myFG As String)   Dim sLine As String   Dim fID As Integer   Dim lCol As Long, lRow As Long   Dim sFirstLine As String     sFirstLine = "O," & main.txt_refferte.Text & ",,,,," & _      login.KlantNaam & ",,," & main.Combo1.Text & "," & _      main.Combo2.Text & "," & main.Combo3.Text & "," & _      main.Combo4.Text & ",," & main.Combo5.Text & "," & _      main.Combo6.Text & ",," & main.Combo8.Text & "," & _      main.Combo9.Text & "," & main.Combo10.Text & "," & main.Combo7.Text   fID = FreeFile   Open sFileName For Output As #fID   Print #fID, sFirstLine   ' Dump the grid   With MSFlexGrid1      For lRow = 1 To .Rows - 1         sLine = ""         For lCol = 0 To .Cols - 1            sLine = sLine & "," & .TextMatrix(lRow, lCol)         Next lCol         ' All kind of prefixes possible         ' 1:         Print #fID, "A," & Mid$(sLine, 2)         ' Or:         ' Print #fID, "row:" & lRow & "=" & Mid$(sLine, 2)      Next lRowEnd With   Close #fIDEnd Function 
See also  5 Benefits of Using Time Tracking Software

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist