Query an email address for the "To" line in an email (1 Viewer)

ethan.geerdes

Registered User.
Local time
Yesterday, 18:21
Joined
Jun 4, 2015
Messages
116
Hello everyone,

I have a button on my form that will open up outlook and get an email ready to send, but I want it to auto the information that was selected on the form. Essentially it is used as a notification.

Code:
Dim olApp As Outlook.Application
   Dim objMail As Outlook.MailItem
    Set olApp = Outlook.Application
   
    Set objMail = olApp.CreateItem(olMailItem)

    With objMail
        .Subject = "assignment"
        '.To =
        .Body =  
        .Display
    End With

I would like for the "To" field be auto filled with the person I choose in a combobox. ( I have a table with all the information and the combobox is querying that table)

I currently have it commented out because I don't know what I should do for the query and have been manually typing it.

I am not sure if it is even possible but that is why I bring it up to the great people on this wonderful forum.
 

MarkK

bit cruncher
Local time
Yesterday, 18:21
Joined
Mar 17, 2004
Messages
8,186
So the problem is with a query supplying data to a combo, not with creating an email?
 

ethan.geerdes

Registered User.
Local time
Yesterday, 18:21
Joined
Jun 4, 2015
Messages
116
what I tried was this initially was this

Code:
 Dim olApp As Outlook.Application
   Dim objMail As Outlook.MailItem
    Set olApp = Outlook.Application
   
    Set objMail = olApp.CreateItem(olMailItem)

    With objMail
        .Subject =
        .To =  Me.Combo10
        .Body = 
        .Display
    End With
 

ethan.geerdes

Registered User.
Local time
Yesterday, 18:21
Joined
Jun 4, 2015
Messages
116
Well, I was getting an error but I just got it to work. I had the column missing and now I'm going to try and get the body to auto populate. thank you.
 

ethan.geerdes

Registered User.
Local time
Yesterday, 18:21
Joined
Jun 4, 2015
Messages
116
So I got the "To" part to work but I can't get the body to pull up information from the form.
 

ethan.geerdes

Registered User.
Local time
Yesterday, 18:21
Joined
Jun 4, 2015
Messages
116
So I got the "To" part to work but I can't get the body to pull up information from the form.
 

ethan.geerdes

Registered User.
Local time
Yesterday, 18:21
Joined
Jun 4, 2015
Messages
116
I can't seem to get the body portion to work though. It wont populate with the field I want.

Code:
.Body = Me.Combo14.Column(5)

Error Received: There is an invalid use of a null value

Then I tried this:

Code:
 .Body = Me.Combo14.Column(5) "Follow the link to your assignment"

And I get an unrecognized expression error.
 

Minty

AWF VIP
Local time
Today, 02:21
Joined
Jul 26, 2013
Messages
10,371
Set variables up to hold your data before referencing outlook. Something like;

Dim strBody as String

strBody = Me.Combo14.Column(5) & " Follow the link to your assignment"
debug.print strBody ' This will let you see what you are forming in the string

then later

.Body = strBody

There are certain things that can't be referenced within the Outlook construct.
 

ethan.geerdes

Registered User.
Local time
Yesterday, 18:21
Joined
Jun 4, 2015
Messages
116
I figured out how to do it. For everyone else that is reading this; for the body of the email, if you put quotations around the words that you want to be said and then concatenate it with the expression of the field you want to show up. Example is shown below.

Code:
 Dim olApp As Outlook.Application
   Dim objMail As Outlook.MailItem
    Set olApp = Outlook.Application
   
    Set objMail = olApp.CreateItem(olMailItem)

    With objMail
        .Subject = "Subject"
        .To = Me.Combo10.Column(4)
        .CC = "email address or distribution group"
        .Body = "text. " & Me.Combo14.Column(4) & " Text"
        .Recipients.ResolveAll
        .Display
        
    End With

This code will open up Outlook, open up a new email and auto populate it with whatever you input.
 

Users who are viewing this thread

Top Bottom