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 
Share the Post:
Share on facebook
Share on twitter
Share on linkedin


The Latest

microsoft careers

Top Careers at Microsoft

Microsoft has gained its position as one of the top companies in the world, and Microsoft careers are flourishing. This multinational company is efficiently developing popular software and computers with other consumer electronics. It is a dream come true for so many people to acquire a high paid, high-prestige job

your company's audio

4 Areas of Your Company Where Your Audio Really Matters

Your company probably relies on audio more than you realize. Whether you’re creating a spoken text message to a colleague or giving a speech, you want your audio to shine. Otherwise, you could cause avoidable friction points and potentially hurt your brand reputation. For example, let’s say you create a

chrome os developer mode

How to Turn on Chrome OS Developer Mode

Google’s Chrome OS is a popular operating system that is widely used on Chromebooks and other devices. While it is designed to be simple and user-friendly, there are times when users may want to access additional features and functionality. One way to do this is by turning on Chrome OS