473,466 Members | 1,395 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

New Outlook Email with BCC field populated with Emails from Query

I'm trying to create a new email message from Outlook based on a query
called Q_Email with the BCC field populated with the email address
field in the query.

When i run this code, the query ressults pop up with a message saying
Object Required.

Thoughts?

Private Sub BCCMail_Click()
On Error GoTo Err_BCCMail_Click

Dim MyOutlook As New Outlook.Application
Dim MyMail As Outlook.MailItem
Dim StrBCC As String
Dim stDocName As String

stDocName = "Q_Email"
DoCmd.OpenQuery stDocName, acNormal, acEdit
With rst
Do Until .EOF
StrBCC = StrBCC & ![EmailAddress] & ";"
Loop
..Close
End With
StrBCC = Left(StrBCC, Len(StrBCC) - 1)

Set MyOutlook = New Outlook.Application
Set MyMail = MyOutlook.CreateItem(olMailItem)
MyMail.BCC = StrBCC
MyMail.Display

Set MyOutlook = Nothing
Set rst = Nothing

Exit_BCCMail_Click:
Exit Sub

Err_BCCMail_Click:
MsgBox Err.Description
Resume Exit_BCCMail_Click

End Sub
Nov 17 '08 #1
7 4677
Hi,

You never create a recordset object for your loop. Rather than open a
query, you need to open a recordset based on the query. Make sure you
have a reference to the DAO object library (Tools References at the
top of a VBA code window) and add the following declaration at the top
of your code:

Dim rst as DAO.Recordset

Then replace your line "DoCmd.OpenQuery stDocName, acNormal, acEdit"
with the following:

Set rst = CurrentDb.OpenRecordset(stDocName,dbOpenForwardOnl y)

This fills the recordset with the records from your query and allows
you to step through them from front to back.

Inside your loop, you need to advance from one record to the next, so
add the following line of code before your Loop line:

..MoveNext

I think it should work after that.

Cheers,
Simon

On Nov 17, 9:04*am, mic...@gmail.com wrote:
I'm trying to create a new email message from Outlook based on a query
called Q_Email with the BCC field populated with the email address
field in the query.

When i run this code, the query ressults pop up with a message saying
Object Required.

Thoughts?

Private Sub BCCMail_Click()
On Error GoTo Err_BCCMail_Click

Dim MyOutlook As New Outlook.Application
Dim MyMail As Outlook.MailItem
Dim StrBCC As String
Dim stDocName As String

* *stDocName = "Q_Email"
DoCmd.OpenQuery stDocName, acNormal, acEdit
With rst
Do Until .EOF
StrBCC = StrBCC & ![EmailAddress] & ";"
Loop
.Close
End With
StrBCC = Left(StrBCC, Len(StrBCC) - 1)

Set MyOutlook = New Outlook.Application
Set MyMail = MyOutlook.CreateItem(olMailItem)
MyMail.BCC = StrBCC
MyMail.Display

Set MyOutlook = Nothing
Set rst = Nothing

Exit_BCCMail_Click:
* * Exit Sub

Err_BCCMail_Click:
* * MsgBox Err.Description
* * Resume Exit_BCCMail_Click

End Sub
Nov 18 '08 #2
Thanks for your help!

Is there any way to write code to make sure users
have a reference to the DAO object library?

Having to adjust the references on users access probably isn't ideal?

Also, if i have this running based on selections from a multiselect
picklist on a form, would i be best to copy this code to a module and
have the vba code run through the selections, then the module? or
would i be best to combine the code for the selections then run this?
Nov 18 '08 #3
where abouts in that code would i place WhereCondition:=strWhere,
OpenArgs:=strDescrip
Nov 18 '08 #4
On Nov 18, 7:25*am, mic...@gmail.com wrote:
where abouts in that code would i place WhereCondition:=strWhere,
OpenArgs:=strDescrip
Regarding the reference to the DAO object library, if you are
distributing the db as a file, then it will be set when you send it to
folks. References are on a per Access file basis, not for the whole
of MS Access. That said, you get some default references that depend
on your version of Access.

It's not clear from your post what you are selecting in your
multiselect picklist. If you can elaborate on that, I might be able
to help.

Also, where are you getting the WhereCondition and OpenArgs from? The
code you presented appears to be from a button click.

Simon
Nov 18 '08 #5
mi****@gmail.com wrote:
>Thanks for your help!

Is there any way to write code to make sure users
have a reference to the DAO object library?
Just add the DAO reference. Note though that if you're using ADO you can also create
a ADO recordset.

If You're using Access 2000 or newer and you have references to both DAO (Microsoft
DAO 3.6 Object Library or newer) and ADO (Microsoft ActiveX Data Objects 2.x Library)

When you have both references, you'll find that you'll need to "disambiguate" certain
declarations, because objects with the same names exist in the 2 models. For example,
to ensure that you get a DAO recordset, you'll need to use Dim rsCurr as
DAO.Recordset (to guarantee an ADO recordset, you'd use Dim rsCurr As
ADODB.Recordset)

The list of objects with the same names in the 2 models is Connection,
Error, Errors, Field, Fields, Parameter, Parameters, Property, Properties
and Recordset
>Having to adjust the references on users access probably isn't ideal?
DAO is fine as it's on everyones machine who has Access installed.
>Also, if i have this running based on selections from a multiselect
picklist on a form, would i be best to copy this code to a module and
have the vba code run through the selections, then the module? or
would i be best to combine the code for the selections then run this?
You might as well leave the code behind the form if it's only going to be used on
that form and is form specfiic.

Tony
--
Tony Toews, Microsoft Access MVP
Please respond only in the newsgroups so that others can
read the entire thread of messages.
Microsoft Access Links, Hints, Tips & Accounting Systems at
http://www.granite.ab.ca/accsmstr.htm
Tony's Microsoft Access Blog - http://msmvps.com/blogs/access/
Nov 18 '08 #6
Yes, I've got users selecting a button on a form and i want an email
to be created to all users who fall in to the selected Category and
Countries selected. I have copied code i have that works for the
report and pasted it to this code, but obviously i need to set the
strWhere and strDescrib for the email to be based on.

Private Sub BCCEmail_Click()
On Error GoTo Err_BCCMail_Click

Dim varItem As Variant
Dim strDescrip As String
Dim lngLen As Long
Dim strDelim As String
Dim strCategorySelect As String
Dim strCountrySelect As String
Dim strWhere As String
Dim stDocName As String
Dim MyOutlook As New Outlook.Application
Dim MyMail As Outlook.MailItem
Dim StrBCC As String
Dim rst As DAO.Recordset

stDocName = "Q_Email"

With Me.CategorySelect
For Each varItem In .ItemsSelected
If Not IsNull(varItem) Then
strCategorySelect = strCategorySelect &
strCategorySelect & strDelim & .ItemData(varItem) & strDelim & ","
strDescrip = strDescrip & """" & .Column(1, varItem)
& """, "
End If
Next
End With

lngLen = Len(strCategorySelect) - 1
If lngLen 0 Then
strCategorySelect = "[CategoryID] IN (" & Left$
(strCategorySelect, lngLen) & ")"
lngLen = Len(strDescrip) - 2
If lngLen 0 Then
strDescrip = "Categories: " & Left$(strDescrip, lngLen)
End If
End If

With Me.CountrySelect
For Each varItem In .ItemsSelected
If Not IsNull(varItem) Then
strCountrySelect = strCountrySelect & strCountrySelect
& strDelim & .ItemData(varItem) & strDelim & ","
strDescrip = strDescrip & """" & .Column(1, varItem)
& """, "
End If
Next
End With

lngLen = Len(strCountrySelect) - 1
If lngLen 0 Then
strCountrySelect = "[CountryID] IN (" & Left$
(strCountrySelect, lngLen) & ")"
lngLen = Len(strDescrip) - 2
If lngLen 0 Then
strDescrip = "Categories: " & Left$(strDescrip, lngLen)
End If
End If
If strCategorySelect "" Then strWhere = strWhere &
strCategorySelect & " And "
If strCountrySelect "" Then strWhere = strWhere &
strCountrySelect & " And "
If strWhere "" Then strWhere = Left(strWhere, Len(strWhere)
- 5)

Set rst = CurrentDb.OpenRecordset(stDocName, dbOpenForwardOnly)
With rst
Do Until .EOF
StrBCC = StrBCC & ![EmailAddress] & ";"
..MoveNext
Loop
..Close
End With
StrBCC = Left(StrBCC, Len(StrBCC) - 1)

Set MyOutlook = New Outlook.Application
Set MyMail = MyOutlook.CreateItem(olMailItem)
MyMail.BCC = StrBCC
MyMail.Display

Set MyOutlook = Nothing
Set rst = Nothing

Exit_BCCMail_Click:
Exit Sub

Err_BCCMail_Click:
MsgBox Err.Description
Resume Exit_BCCMail_Click

End Sub
Nov 19 '08 #7
Any clues?

Can you set a record set to a Where condition?
Nov 20 '08 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: jason | last post by:
I could sure use some conceptualization and query help with a Page Watch System I am building in Access 2000 and Asp. I need to cycle through databae and generate a compiliation query email that...
2
by: Alexander | last post by:
To all, A tall order, but any help on any segment of this would be appreciated: I have a table that needs to be populated with information contained in an email attachment. 1. Is it...
4
by: PitchBlackSheep | last post by:
I have an office of Real Estate agents who use our (single) Access 2000 database to keep track of clients. Our agents email our clients everyday using Outlook 2000. I'm trying to find a way to...
3
by: Steve | last post by:
Re: Outlook Express I'd like to get a log of the date and subject of all emails in my inbox received from sender@xyz.com and a log of the date and subject of all emails in my sentbox sent to...
3
by: Strasser | last post by:
In Access2000 mass emailing worked perfectly (very powerful tool!). Doesn't work when using XP version of both Access and Outlook, even though I checked the box to ensure that I was sending the...
1
by: Michele | last post by:
Hi, I need to send the same Email to different people. I'm using Outlook XP and VB.Net. I tryed with the following code: Dim oOutL As Outlook.Application Dim oMail As Outlook._MailItem oOutL...
1
by: Phil Stanton | last post by:
I have a Yacht Club Db with names addresse phone nos, emails etc. I want to export them to Outlook. No problem in getting them into the contact folder. My problem is I have a folder within the...
7
by: Dean Spencer | last post by:
Can anyone help? I am importing Emails from Outlook using the following code: Public Function ScanInbox(SubjectLine As String) Dim TempRst As Recordset Dim OlApp As Outlook.Application Dim...
18
by: flynjack | last post by:
I have a table named "tblError". The table includes the following columns: (1) Employee ID (2) Error Code (3) Email Address (4) Email Sent (Check box from Yes or No, Yes if checked. I have a...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.