# How to email to a email address inside a textbox



## Pookiemeister

I have button on my form that I would like use to open outlook to send an email to the person inside the textbox which is labeled "Email", as a start. Thank you.


----------



## welshgasman

Look at the SendObject command if it is a simple email, else outlook automation would be needed.

This link has both
DoCmd.SendObject method (Access)

Found with a simple Google sendobject email outlook - Google Search


----------



## Pookiemeister

Thank you for the quick response but I noticed this doesn't work on older versions of Outlook because I am running MS Office 2010 at home but MS Office 365 at work. When I run this code on my home computer, I get the following error message: Error Message "This action is not supported while an older version of Outlook is running." How can I get this to work in Office 2010 on both versions. Thank you again.



		VBA Code:
__


Dim olApp As Outlook.Application
   Dim MI As Outlook.MailItem
   Dim sPath, sFileName, CurrentTime, Salutation As String
   Dim sFileLocation As String
   Dim wbAnswer, EmilAnswer As Integer

   CurrentTime = Format(Now, "hh:mm:ss")
    If CurrentTime > "05:00:00" And CurrentTime < "12:00:59" Then
        Salutation = "Good morning "
    ElseIf CurrentTime > "12:01:00" And CurrentTime < "16:59:59" Then
        Salutation = "Good afternoon "
    ElseIf CurrentTime > "17:00:00" And CurrentTime < "20:59:59" Then
        Salutation = "Good evening "
    Else
        Salutation = "Dear "
    End If

        Set olApp = New Outlook.Application
        Set MI = olApp.CreateItem(olMailItem)

      DoCmd.SendObject

        With MI
            .To = EmailContact1
            .CC = ""
            .Subject = "Purchase Order Number: " & PONumber
            .Body = Salutation & FirstName1 & "," & vbNewLine & vbNewLine
            .Display
        End With
        Set MI = Nothing
        Set olApp = Nothing


----------



## welshgasman

Change to late binding?


----------



## JEC

Here you go



		VBA Code:
__


Sub jec()
 Dim Salutation as String
 Select Case Time
    Case Is < "12:00:59": Salutation = "Good morning "
    Case Is < "16:59:59": Salutation = "Good afternoon "
    Case Is < "20:59:59": Salutation = "Good evening "
    Case Else: Salutation = "Dear "
 End Select
 With CreateObject("outlook.application").createitem(0)
    .To = "test@gmail.com"
    .Subject = "test"
    .body = Salutation
    .display
 End With
End Sub


----------



## Pookiemeister

@JEC 

Will this method also work with newer versions of Outlook, like in Office 365? Thank you.


----------



## JEC

Yes it will work


----------



## Pookiemeister

Next question which I just thought about, what if the user uses outlook online?


----------



## JEC

That would not be possible to automate with this object class.
But if the user also has outlook desktop on his/her pc, it should still work.


----------



## Pookiemeister

Ok. I thought so. Thank you again.


----------



## Pookiemeister

I have button on my form that I would like use to open outlook to send an email to the person inside the textbox which is labeled "Email", as a start. Thank you.


----------



## JEC

You're welcome!


----------



## Pookiemeister

@JEC

One more thing about your code above. The following shortened code does not work as expected. It always goes to the Case Else condition there needs to be an AND with Case Is like in the original code but Case does not allow an AND.



		VBA Code:
__


Select Case Time
    Case Is < "12:00:59": Salutation = "Good morning "
    Case Is < "16:59:59": Salutation = "Good afternoon "
    Case Is < "20:59:59": Salutation = "Good evening "
    Case Else: Salutation = "Dear "
 End Select


----------



## JEC

Here it works. You don't need an And.
Same result this way?



		VBA Code:
__


Select Case Time
    Case Is < TimeValue("12:00:59"): Salutation = "Good morning "
    Case Is < TimeValue("16:59:59"): Salutation = "Good afternoon "
    Case Is < TimeValue("20:59:59"): Salutation = "Good evening "
    Case Else: Salutation = "Dear "
 End Select


----------



## welshgasman

Pookiemeister said:


> @JEC
> 
> One more thing about your code above. The following shortened code does not work as expected. It always goes to the Case Else condition there needs to be an AND with Case Is like in the original code but Case does not allow an AND.
> 
> 
> 
> VBA Code:
> __
> 
> 
> Select Case Time
> Case Is < "12:00:59": Salutation = "Good morning "
> Case Is < "16:59:59": Salutation = "Good afternoon "
> Case Is < "20:59:59": Salutation = "Good evening "
> Case Else: Salutation = "Dear "
> End Select


Works for me also at 16:29 with Good afternoon ?


----------

