Login | Register   
RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX

By submitting your information, you agree that devx.com may send you DevX offers via email, phone and text message, as well as email offers about other products and services that DevX believes may be of interest to you. DevX will process your information in accordance with the Quinstreet Privacy Policy.

Tip of the Day
Language: VB6
Expertise: Beginner
Apr 20, 2005



Building the Right Environment to Support AI, Machine Learning and Deep Learning

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

      If MAPISession1.SessionID <> 0 Then

         With MAPIMessages1
            .SessionID = MAPISession1.SessionID

            '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@adress.here"
            '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
   'If they say no
   End If
End Sub

Function 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 lRow
End With
   Close #fID
End Function
Sander Kleykens
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date