Page 2 of 2 FirstFirst 12
Results 16 to 26 of 26
  1. #16
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,641
    Can you post the db, or a representative sample?
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  2. #17
    Juanna_v is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Feb 2011
    Posts
    18
    Here it is. Thanks. I'm a bit desperate now. No matter what I try to do It always stops somewhere

  3. #18
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,641
    Okay, try this. Note that I'm not a fan of creating/deleting objects all the time, so I created a dummy query named "q_temp" that will remain in the database and be reused (you need to create this). You can put that other stuff back if you want. This created two emails and left them open for editing. It's not done, but it's working, so you can smooth out the wrinkles:

    Code:
      Dim qdf As DAO.QueryDef
      Dim dbs As DAO.Database
      Dim rstMgr As DAO.Recordset
      Dim strSQL As String, strTemp As String, strMgr As String
      Dim objOutlook As Outlook.Application
      Dim objOutlookMsg As Outlook.MailItem
      Dim objOutlookRecip As Outlook.Recipient
      Dim objOutlookAttach As Outlook.Attachment
      Dim DisplayMsg As Boolean
      Dim varTo As Variant
      Dim stWhere As String
    
      On Error GoTo Err_SendMessage
    
      Const strQName As String = "zExportQuery"
    
      Set dbs = CurrentDb
      Set qdf = dbs.QueryDefs("q_temp")
    
      strSQL = "SELECT DISTINCT ManagerID, ManagerEmail FROM EmployeesTable;"
      Set rstMgr = dbs.OpenRecordset(strSQL, dbOpenDynaset, dbReadOnly)
    
      Do While rstMgr.EOF = False
    
        strMgr = DLookup("ManagerID", "ManagersTable", _
                         "ManagerID = " & rstMgr!ManagerID.Value)
    
        strSQL = "SELECT * FROM EmployeesTable WHERE " & _
                 "ManagerID = " & rstMgr!ManagerID.Value & ";"
    
    
        qdf.SQL = strSQL
        qdf.Close
        ' Replace C:\FolderName\ with actual path
        DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, _
                                  "q_temp", "C:\AccessAp\" & strMgr & Format(Now(), _
                                                                             "ddMMMyyy_hhnn") & ".xls"
    
        ' Create the Outlook session.
        Set objOutlook = CreateObject("Outlook.Application")
    
        ' Create the message.
        Set objOutlookMsg = objOutlook.CreateItem(olMailItem)
    
        With objOutlookMsg
          ' Set the Subject, Body, and Importance of the message.
          .Subject = "Subject"
          .Body = "Message"
          .To = rstMgr!ManagerEmail
          '.Importance = olImportanceHigh 'High importance
    
          Set objOutlookAttach = .Attachments.Add("C:\AccessAp\" & strMgr & Format(Now(), _
                                                                                   "ddMMMyyy_hhnn") & ".xls")
          ' Resolve each Recipient's name.
          For Each objOutlookRecip In .Recipients
            objOutlookRecip.Resolve
          Next
    
          .Display
    
        End With
    
        rstMgr.MoveNext
      Loop
    
    Exit_SendMessage:
    
      Set qdf = Nothing
      Set objOutlook = Nothing
      rstMgr.Close
      Set rstMgr = Nothing
      dbs.Close
      Set dbs = Nothing
      Exit Sub
    
    Err_SendMessage:
      MsgBox Error$
      Resume Exit_SendMessage
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  4. #19
    Juanna_v is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Feb 2011
    Posts
    18
    Thank u very much! You've been so much helpful. Still I can not try the code since I don't have outlook at home and have to wait until tomorrow so I can test. For the moment the only error that comes up is an outlook app error so I guess that everything else is ok. I'll know for sure tomorrow. Once again thank you very much for your assistance and for your time spend on this.

  5. #20
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,641
    Happy to help! It would error since you don't have Outlook, as there's a reference set to Outlook. Try it tomorrow, and see what still needs tweaking.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  6. #21
    Juanna_v is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Feb 2011
    Posts
    18
    Well, how many thanks can i say for this!!! It works perfect. Exactly what I was looking for. Two more things if you can assist me also. How can I write in the body msg a text that I keep in a txtBox on a form named lets say "FrmMessage1").

    and the 2nd one request is minor but I would appreciate if you could reply on this also. When I run the code all emails I have to send open the same time so I have to handle with many emails. Is it possible for the code to wait until I send the first email and then the 2nd appear ?

  7. #22
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,641
    Glad it's working for you. For the first, this type of thing:

    .Body = Forms!FormName.TextboxName

    It would probably be more common to just send them all out with

    .Send

    instead of

    .Display

    I'm not sure how to wait for the user to actually send the email offhand. You could put a message box in after the .Display, which would stop the code until the user responded it.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  8. #23
    Juanna_v is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Feb 2011
    Posts
    18
    Hi again. I think that the solution for a yes/no msg box is will do my work.

    Do you think the following syntax is ok? Unfortunately since I don't have outlook I cannot test the whole procedure.
    ----------
    .Display
    Dim Response As Integer
    Response = MsgBox(prompt:="To continue press 'Yes' For exit 'No'.", Buttons:=vbYesNo)
    ' if user selects yes, i would like to macro to continue running
    If Response = vbYes Then
    Resume
    Else
    ' if the user selects no, I would like the macro to stop and the user be returned to the main form

    End If

    End With

    rstMgr.MoveNext
    Loop

  9. #24
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,641
    You don't need to specify "Resume", as code will continue unless instructed otherwise. Try:

    Code:
    If Response = vbNo Then
      GoTo Exit_SendMessage
    End If
    If they answer yes, code will continue on. You don't want to just exit the sub, you want to go to the exit point so all your variables get cleaned up.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  10. #25
    Juanna_v is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Feb 2011
    Posts
    18

    Thumbs up

    What can I say? So many many thanks! i Forgot to mention that I loved your suggestion to have a permanent query in the DB instead of deleting it all he time. RESPECT!!!

  11. #26
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,641
    LOL! Glad we got it working for you.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

Page 2 of 2 FirstFirst 12
Please reply to this thread with any new information or opinions.

Similar Threads

  1. rename report and email
    By maddaddy in forum Reports
    Replies: 11
    Last Post: 08-12-2011, 06:17 AM
  2. rename ole object
    By rbg in forum Access
    Replies: 0
    Last Post: 11-29-2010, 03:13 AM
  3. docmd.rename wierd results
    By hyperionfall in forum Programming
    Replies: 1
    Last Post: 03-04-2010, 01:18 PM
  4. VB to copy a value from a form to a report
    By cwwaicw311 in forum Programming
    Replies: 1
    Last Post: 03-01-2010, 10:45 PM
  5. Transfer SpreadSheet Command and Rename
    By fintan06 in forum Import/Export Data
    Replies: 2
    Last Post: 10-15-2009, 08:00 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums