Jumat, 07 Juli 2017

Re: [MS_AccessPros] Sending Email through Outlook based on a query

 

Khalid-

What's so hard about adding code to open the query and loop through the records?

Sub EmailOutlook()
'strive4peace
   'use early binding to develop
'   Dim appOut As Outlook.Application _
      , oMsg As Outlook.MailItem


   'use late binding to deploy
   Dim appOut As Object _
      , oMsg As Object

   Dim sPathFileAttachment As String _
      , sSubject As String _
      , sBody As String _
      , sMailTo As String

   Dim db As DAO.Database, rst As DAO.Recordset
   Set db = CurrentDb
   Set rst = db.OpenRecordset("qryHappyBirthday")

   Do Until rst.EOF
   
   sMailTo = rst!MemberEmail '<=== I NEED HERE EMAIL ADDRESSES TO BE FILLED ON THE RESULTS OF
                                             '         "qryHappyBirthday"

   sSubject = "Happy Birthday"
   sBody = "Your message..."
   sPathFileAttachment = "C:\Users\tanwe\Desktop\Today for sending email.txt" 'or whatever this is '<== This is                                                                                                        '  an example, i may attach here some image.
   'sPathFileAttachment = "c:\path\file.ext" 'or whatever this is
   
   Set appOut = CreateObject("Outlook.Application")
   Set oMsg = appOut.CreateItem(0) '0=olMailItem

   With oMsg
      '.Importance = olImportanceHigh
      .To = sMailTo
      '.CC = "cc email address"
      '.BCC = "cc email address"
      .Subject = sSubject
      .Body = sBody
      If sPathFileAttachment <> "" Then
         .Attachments.Add sPathFileAttachment
      End If
      
      ' If you want to edit before sending
      .Display
      'otherwise, to just send without looking...
      '.Send
   End With

   rst.MoveNext
   Loop
   
Proc_Exit:

   On Error Resume Next

   'release object variables

   Set appOut = Nothing

   Set oMsg = Nothing

   Exit Sub

   
Proc_Err:

   MsgBox Err.Description, , _

        "ERROR " & Err.Number _

        & "   EmailOutlook"



   Resume Proc_Exit

   Resume

End Sub

John Viescas, Author
Effective SQL
SQL Queries for Mere Mortals 
Microsoft Access 2010 Inside Out
Microsoft Access 2007 Inside Out
Microsoft Access 2003 Inside Out
Building Microsoft Access Applications 
http://www.viescas.com/ 
(Paris, France)




On Jul 7, 2017, at 9:25 PM, khalidtanweerburrah@yahoo.com [MS_Access_Professionals] <MS_Access_Professionals@yahoogroups.com> wrote:



Hi All,



I need to send birthday wish to members whom birthday occurs on the current date. In order to get members birthday on the current date i have a query "qryHappyBirthday" with Sql:



SELECT Members.MembershipType, Members.MembershipTitle, Members.MemberID, Members.SurName, Members.GivenName, Members.GenderStatus, Members.DesignationSOC, Members.Dob, Members.PlaceOfBirth, tblContactNumbers.TelephoneResidence, tblContactNumbers.MobileNo1, tblElectronicContacts.MemberEmail

FROM (Members INNER JOIN tblContactNumbers ON Members.MemberID = tblContactNumbers.MemberID) INNER JOIN tblElectronicContacts ON Members.MemberID = tblElectronicContacts.MemberID


WHERE (((Month([Dob]))=Month(Date())) AND ((Day([Dob]))=Day(Date())));



I have got a module "mod_EmailOutlook" with code:



Option Compare Database

Option Explicit



Sub EmailOutlook()

'strive4peace

   'use early binding to develop

'   Dim appOut As Outlook.Application _

      , oMsg As Outlook.MailItem

   
   'use late binding to deploy

   Dim appOut As Object _

      , oMsg As Object



   Dim sPathFileAttachment As String _

      , sSubject As String _

      , sBody As String _

      , sMailTo As String

   
   sMailTo = "someone@..." '<=== I NEED HERE EMAIL ADDRESSES TO BE FILLED ON THE RESULTS OF

                                             '         "qryHappyBirthday"

   sSubject = "Happy Birthday"

   sBody = "Your message..."

   sPathFileAttachment = "C:\Users\tanwe\Desktop\Today for sending email.txt" 'or whatever this is '<== This is                                                                                                        '  an example, i may attach here some image.

   'sPathFileAttachment = "c:\path\file.ext" 'or whatever this is

   
   Set appOut = CreateObject("Outlook.Application")

   Set oMsg = appOut.CreateItem(0) '0=olMailItem



   With oMsg

      '.Importance = olImportanceHigh

      .To = sMailTo

      '.CC = "cc email address"

      '.BCC = "cc email address"

      .Subject = sSubject

      .Body = sBody

      If sPathFileAttachment <> "" Then

         .Attachments.Add sPathFileAttachment

      End If

      
      ' If you want to edit before sending

      .Display

      'otherwise, to just send without looking...

      '.Send

   End With

   
Proc_Exit:

   On Error Resume Next

   'release object variables

   Set appOut = Nothing

   Set oMsg = Nothing

   Exit Sub

   
Proc_Err:

   MsgBox Err.Description, , _

        "ERROR " & Err.Number _

        & "   EmailOutlook"



   Resume Proc_Exit

   Resume

End Sub

~~~~~~~~~~~~~~~~

Above module after pressing F5 key works perfect, it opens Outlook. I've tested it by putting my own email address and sending and received mail send from Outlook.



I assume (May be i'm wrong) there must be something like DO WHILE EOF and LOOP in the above code, but i have no knowledge in Access/VBA about this.



When all this is set, how i will call this module to run ?. I need to put a button on my Continuous form "Notification3" to call this module. This form displays the output of query "qryHappyBirthday"



Help required please.



Regards,

Khalid







__._,_.___

Posted by: John Viescas <johnv@msn.com>
Reply via web post Reply to sender Reply to group Start a New Topic Messages in this topic (2)

Have you tried the highest rated email app?
With 4.5 stars in iTunes, the Yahoo Mail app is the highest rated email app on the market. What are you waiting for? Now you can access all your inboxes (Gmail, Outlook, AOL and more) in one place. Never delete an email again with 1000GB of free cloud storage.


.

__,_._,___

Tidak ada komentar:

Posting Komentar