Results 1 to 9 of 9
  1. #1
    WhiskyLima is offline Advanced Beginner
    Windows 8 Access 2010 32bit
    Join Date
    Oct 2013
    Location
    UK
    Posts
    32

    Send Emails from outlook

    Hey guys, so here's my latest issue. I have a database where I would like to send emails from outlook. So I had a read around and found out how to create your own buttons with vba in outlook. Which is great so I set to work on this. The code below is initiated when a user form opens .



    Code:
        Dim dbs As DAO.Database
        Set dbs = DAO.OpenDatabase("c:\mrl\frontend.accdb")
        Dim i As Integer
        
        Dim rst As DAO.Recordset
        Set rst = dbs.OpenRecordset("SELECT [Contact ID], [First Name], [Last Name] FROM Contacts")
        rst.MoveFirst
        i = 0
        With Me.cmb
             .Clear
             Do
                If Not IsNull(rst.Fields("[Contact ID]")) Then
                 .AddItem
                 .List(i, 2) = rst.Fields("[Contact ID]")
                 .List(i, 1) = Nz(rst.Fields("[First Name]"), "")
                 .List(i, 0) = Nz(rst.Fields("[Last Name]"), "")
                 i = i + 1
                 rst.MoveNext
                 End If
             Loop Until rst.EOF
         End With
        Set dbs = Nothing
        Set rst = Nothing
    Ok great that work, now I have a combobox with the names and id numbers of all of the clients from the database. That's all good. The next thing was to then use this code (below) on a button to tell it to store the details of the selected email as variables. It then passes the content of the email to another function which removes the special characters and passes the result back to the variable (this stops any issues with SQL later). After the user has selected a name from the list, they press the button and it creates a new entry in the database with the details of the email...

    Code:
    Dim olItem As Outlook.MailItem
    Dim sText As String
    Dim sSubject As String
    Dim sFrom As String
    Dim sTo As String
    Dim sDate As Date
    Dim strSQL As String
    Dim dbs As DAO.Database
    Set dbs = DAO.OpenDatabase("c:\mrl\frontend.accdb")
     
    Set olItem = ActiveExplorer.Selection.Item(1)
    sText = olItem.Body
    sSubject = olItem.Subject
    sFrom = olItem.SenderEmailAddress
    sTo = olItem.To
    sDate = olItem.SentOn
    'This section of code removes all of the special characters from the email content to stop it messing with the SQL code.
    ValidateString sText
    strSQL = "INSERT INTO SavedEmails ([Sender Name], [Received], [Subject], [Content], [ClientID]) VALUES ('" & sFrom & "', '" & sDate & "', '" & sSubject & "', '" & sText & "', '" & cmb.Column(2) & "')"
    dbs.Execute strSQL
    Set dbs = Nothing
    Set rst = Nothing
    Ok, still with me? Well that too works fine, however there is an issue, I cant do this while the database is open as it says the database has been placed in a state blah blah, its obviously open and is read only.

    I suppose my question is this, is there are way to do this with the database open? If not, is there any way to tell it to close access when its done because at the moment it leaves access running in the background and wont allow me to open the database until I force the background process (msaccess.exe) to close.

    As always any and all help really is greatly appreciated.

    P.s just so you understand my thinking here, the other route I could have taken is to import the emails from the inbox and then assign them from the database but this is clumsy and means when I have read an email I think is important to a certain client in the database I would then have to go to the database, find the email and then assign it and I don't want that.

    Many, many thanks in advance guys, I really do appreciate it!

  2. #2
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,524
    1 .You dont need that 1st box of code to fill a form. Connect the form to a query and all fields fill themselves. Zero code needed.

    2. I email from the database everyday and it's always open and gives no errors. If you are getting the ACCESS STATE... you must be running too many applications, or trying to edit while running.

    Open outlook (in access)
    run a query that gets the emails
    for each record
    send email
    next
    close outlook.

    I see an insert statement in the 2nd block of code...dont.
    try just emailing. Then you can update afterwards.

  3. #3
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,899
    Is this code in Outlook?
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  4. #4
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2013
    Posts
    7,862
    Quote Originally Posted by June7 View Post
    Is this code in Outlook?
    I think it is.

    Maybe you can use the close and quit methods just before:
    Set dbs = Nothing
    Set rst = Nothing

    Another consideration, in addition to closing things down and tidying up, would be to dedicate a DB that links to the original, c:\mrl\frontend.accdb, DB. The dedicated DB would be only for Outlook.

  5. #5
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,899
    I use this command syntax to insert records to a table. Doesn't matter if the destination db is open or closed:

    CurrentDb.Execute "INSERT INTO Table1 IN '\\servername\path\database.accdb' SELECT * FROM tablename;"

    Another example (this one is going the other way, pulling data from another db but should be adaptable to push data):
    strSourceFields = "ID, [Name], Condition, [_Latest], PCISource, Use, Include, Active, [Date], Area, FAAID"
    strDestFields = "SectionID, BranchName, Condition, Latest, Source, Use, Include, Active, InspectionDate, Area, FAAID"
    DoCmd.SetWarnings False
    DoCmd.RunSQL "DELETE FROM PaverData_InspectionsAllYears"
    DoCmd.RunSQL "DELETE FROM PaverData_MajorMRAllYears"
    DoCmd.RunSQL "INSERT INTO PaverData_InspectionsAllYears(" & strDestFields & ") SELECT " & strSourceFields & " FROM [" & strSourcePath & "].zUser_InspectionsAllYears;"
    DoCmd.RunSQL "INSERT INTO PaverData_MajorMRAllYears(SectionID, BranchName, Use, ConstDate, Include, Active, FAAID) SELECT ID, [Name], Use, [Date], Include, Active, FAAID FROM [" & strSourcePath & "].zUser_MajorMRAllYears;"
    DoCmd.SetWarnings True


    There is no db object instantiated so nothing to close or quit or kill and no object variables to wipe out.

    But I have never coded behind Outlook.
    Last edited by June7; 08-11-2014 at 10:55 AM.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  6. #6
    WhiskyLima is offline Advanced Beginner
    Windows 8 Access 2010 32bit
    Join Date
    Oct 2013
    Location
    UK
    Posts
    32
    Hi June7 Thanks for your input, im going to try the code you suggested to insert the statement without opening the database, that would be perfect. I am indeed coding this from outlook as I don't want the mess that comes with importing all emails into access and then trying to sort them from there. I have a very picky client who would not want the hassle of having to look for the email in the database after already having read it in outlook. Anyway, I'll let you know who it goes!

  7. #7
    WhiskyLima is offline Advanced Beginner
    Windows 8 Access 2010 32bit
    Join Date
    Oct 2013
    Location
    UK
    Posts
    32
    Ok, it doesn't seem to like the CurrentDb.Execute command. Im guessing that's because there is no current database open? Is there another way to push the information into an unopened database? Bearing in mind that this is being executed from outlook? Cheers for all your help, I think this may be the correct path to take

  8. #8
    WhiskyLima is offline Advanced Beginner
    Windows 8 Access 2010 32bit
    Join Date
    Oct 2013
    Location
    UK
    Posts
    32
    Ah ha! Ok I have just realised that of course, currentDb will work if the database is open.. as soon as I wrote it I thought about it and it clicked so yes indeed this does work as long as the database is open, which is perfect! Thank you for you help I really appreciate it! Turns out programming outlook is exactly the same as access!

    Code:
    set dbs = CurrentDb
    strSQL = "INSERT INTO SavedEmails ([Sender Name], [Received], [Subject], [Content], [ClientID]) VALUES ('" & sFrom & "', '" & sDate & "', '" & sSubject & "', '" & sText & "', '" & cmb.Column(2) & "')"
    dbs.Execute strSQL

  9. #9
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,899
    My example CurrentDb.Execute code is running in one db to pass data to a closed db. Since your code is behind Outlook, are you saying the CurrentDb worked? The code recognizes Outlook as a database? Interesting.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Loop Through Query, Send Multiple Emails
    By tdoolittle in forum Reports
    Replies: 6
    Last Post: 05-12-2014, 09:33 PM
  2. Replies: 9
    Last Post: 12-18-2013, 02:49 PM
  3. Programming Access to Send Emails?
    By BMW150 in forum Access
    Replies: 8
    Last Post: 09-17-2013, 06:14 PM
  4. Send Emails Automatically
    By cbrsix in forum Programming
    Replies: 10
    Last Post: 10-19-2012, 10:52 AM
  5. Replies: 2
    Last Post: 04-27-2012, 10:48 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