Email from Access - TO ends up in BCC

S

SAC

I'm working with some code to send an email.

If I leave the CC and the BCC string blank, the TO field ends up in the BCC
field.

Any odeas?

Here's the code:

Sub SendMessageNew(strTo As String, strCC As String, strBCC As String,
_
strSubject As String, strBody As String, DisplayMsg As Boolean,
AttachMentPath As String)

'Sub SendMessageNew(strTo As String, strCC As String, strBCC As
String, _
' strSubject As String, strBody As String, DisplayMsg As Boolean,
AttachMentPath as String)

Dim objOutlook As Outlook.Application
Dim objOutlookMsg As Outlook.MailItem
Dim objOutlookRecip As Outlook.Recipient
Dim objOutlookAttach As Outlook.Attachment
Dim strRecipTo As String

' Create the Outlook session.
Set objOutlook = CreateObject("Outlook.Application")

' Create the message.
Set objOutlookMsg = objOutlook.CreateItem(olMailItem)

With objOutlookMsg
' Add the To recipient(s) to the message.
Set objOutlookRecip = .Recipients.Add(strTo)
objOutlookRecip.Type = olTo

' Add the CC recipient(s) to the message.
If strCC > "" Then Set objOutlookRecip =
..Recipients.Add(strCC)

objOutlookRecip.Type = olCC

' Add the BCC recipient(s) to the message.
If strBCC > "" Then Set objOutlookRecip =
..Recipients.Add(strBCC)
objOutlookRecip.Type = olBCC

' Set the Subject, Body, and Importance of the message.
.Subject = strSubject
.Body = strBody & vbCrLf & vbCrLf
.Importance = olImportanceHigh 'High importance

' Add attachments to the message.
If Not IsMissing(AttachMentPath) Then
If AttachMentPath > "" Then Set objOutlookAttach =
..Attachments.Add(AttachMentPath)
End If

' Resolve each Recipient's name.
For Each objOutlookRecip In .Recipients
objOutlookRecip.Resolve
Next

' Should we display the message before sending?
If DisplayMsg Then
.Display
Else
.Save
.Send
End If
End With
Set objOutlook = Nothing
End Sub


Thanks.
 
C

Cheryl Fischer

The only other thing I could suggest beyond the responses you received in
the earlier thread on this subject is to consider calling your procedure
with Named Arguments. For example,

SendMessageNew(strTo:="(e-mail address removed)",
strCC:[email protected])

Named Arguments are position-independent and may resolve the problem.
 
D

Douglas J. Steele

It's because you're changing the Type regardless of whether or not you've
got .CC or .BCC values.

Try:

Set objOutlookRecip = .Recipients.Add(strTo)
objOutlookRecip.Type = olTo

' Add the CC recipient(s) to the message.
If Len(strC)C > 0 Then
Set objOutlookRecip = .Recipients.Add(strCC)
objOutlookRecip.Type = olCC
End If

' Add the BCC recipient(s) to the message.
If Len(strBCC) > 0 Then
Set objOutlookRecip =.Recipients.Add(strBCC)
objOutlookRecip.Type = olBCC
End If

(although I'm not sure that's 100% correct, as I don't think you need to Set
for each of the three types)
 
A

Arvin Meyer

Douglas J. Steele said:
It's because you're changing the Type regardless of whether or not you've
got .CC or .BCC values.
(Snipped)

(although I'm not sure that's 100% correct, as I don't think you need to Set
for each of the three types)

That's exactly correct. One does not need to even define the CC or BCC type
unless they are going to be used.
--
Arvin Meyer, MCP, MVP
Microsoft Access
Free Access downloads:
http://www.datastrat.com
http://www.mvps.org/access
 
D

Douglas J. Steele

Arvin Meyer said:
That's exactly correct. One does not need to even define the CC or BCC type
unless they are going to be used.

Thanks, Arvin. I was too lazy to look it up, but it didn't really seem right
to me that you'd need to use a Set statement for an object that had already
been instantiated.

Now that you've shamed me into it <g>, I see from
http://support.microsoft.com/?kbid=161088 that it is correct to use the
multiple Set statements, so my sample code should work.
 
A

Arvin Meyer

Thanks, Arvin. I was too lazy to look it up, but it didn't really seem right
to me that you'd need to use a Set statement for an object that had already
been instantiated.

You don't
Now that you've shamed me into it <g>, I see from
http://support.microsoft.com/?kbid=161088 that it is correct to use the
multiple Set statements, so my sample code should work.

Sure it does, but if you have instaniated Outlook, you can also use this
code from my website. I added the CC and BCC statements and it will run just
fine. To, CC, and BCC are strings and can be one or hundreds of email
addresses:


Dim objOutlook As Outlook.Application
Dim objEmail As Outlook.MailItem

Set objOutlook = CreateObject("Outlook.application")
Set objEmail = objOutlook.CreateItem(olMailItem)

With objEmail
.To = "(e-mail address removed)"
.CC = "(e-mail address removed)"
.BCC = "(e-mail address removed)"
.Subject = "Look at this"
.body = "The body doesn't matter"
.Display
.Send
End With

Notice the only thing I needed to Dim and Set were the Outlook object and
the MailItem.
--
Arvin Meyer, MCP, MVP
Microsoft Access
Free Access downloads:
http://www.datastrat.com
http://www.mvps.org/access
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top