Outlook Message via Access - breaking up recipient list

User avatar
Carol W.
3StarLounger
Posts: 390
Joined: 26 Jan 2010, 16:02
Location: Las Vegas, NV

Outlook Message via Access - breaking up recipient list

Post by Carol W. »

I currently have a VBA module that is used to send a single Outlook message to a large group of members. Following is the code:

Code: Select all

Option Compare Database

Public Sub SendMessage()
Dim strbcc As String
Dim strmsg As String

Dim dbsmember As Database
Dim rstmembers As Recordset

Set dbsmember = CurrentDb()
Set rstmembers = _
        dbsmember.OpenRecordset("qryEmail alone for broadcast messages-combined", dbOpenDynaset)
On Error GoTo err1
'
' added Desert Sage to broadcast messages - Oct. 2007 -- CSW
'

strbcc = ""

rstmembers.MoveFirst

Do While Not rstmembers.EOF
With rstmembers
        If Not IsNull(!strE_MAIL) Then
          strbcc = strbcc & ![strE_MAIL] & ";"
        End If
End With
rstmembers.MoveNext
Loop

Set rstmembers = Nothing

'strmsg = "<p><font face=""Arial"" size=""3"">The message below has been sent by the ALLV Broadcast system. <br><b>Please do not reply to this message.</b> Thank you. <br>______________________________________________________________________</font></p>"
strmsg = "<p><font face=""Arial"" size=""3"">The message below has been sent by the ALLV Broadcast system. <br><b>Please do not reply to this message. Replies to this message will be sent back to the ALLV address and will not reach the intended recipient. <u>To contact the original sender, forward this message to him/her and add your reply to that message.</u></b><br> <br>Thank you. <br>______________________________________________________________________</font></p>"

'DoCmd.SendObject acSendNoObject, , , , , strbcc, "Enter Message Subject Here", strmsg, yes
'DoCmd.SendObject acSendNoObject, , htm, , , strbcc, "Enter subject here", , True, "C:Documents and Settings\Administrator\Desktop\email template.htm"
Dim strEmail, strsubject As String, strBody As String
Dim objOutlook As Outlook.Application
Dim objEmail As Outlook.MailItem

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

'strEmail = " a @ b.c"
'strBody = "Make this <B>bold</B> and <BR>add a line."
'strSubject = "Subject"
With objEmail
    .BCC = strbcc
    .Subject = "Enter Message Subject here"
    .HTMLBody = strmsg
    '.Send 'Will cause warning message
    .Display
End With

Set objEmail = Nothing



err1:
Exit Sub
End Sub


This module has been in use for years. Recently, we have come up against several issues which I believe can be attributed to exceeding the ISP's maximum parameter of some sort. The issues are:
1 - Receiving a message after the message goes into the Outlook Sent Items folder. See attached.
earthlink send error.jpg
2 - Messages are received twice although there is only one entry in the Sent Items folder.

As a test, I deleted all email addresses that begin with A and B, thereby reducing the number of recipients. Including the A's and B's, there were 351 recipients. I didn't count the reduced list but I would guess about 30-40 addresses were eliminated. Results: Without the A's and B's, (1) no error message appeared and (2) message was received once, as expected.

So, what I would like to do is modify the above code to send the message out in groups of, say, 200 (I'd make this a parameter) without the user having to enter the message more than once.

Any suggestions would be greatly appreciated.
You do not have the required permissions to view the files attached to this post.
Carol W.

User avatar
HansV
Administrator
Posts: 78556
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Outlook Message via Access - breaking up recipient list

Post by HansV »

See the attached text file.
Code.txt
BTW, there are ways to suppress the warning message that Outlook displays when you try to send a message from VBA, for example the free utility ClickYes.
You do not have the required permissions to view the files attached to this post.
Best wishes,
Hans

User avatar
Carol W.
3StarLounger
Posts: 390
Joined: 26 Jan 2010, 16:02
Location: Las Vegas, NV

Re: Outlook Message via Access - breaking up recipient list

Post by Carol W. »

Hans,

Thanks for the speedy reply.

Two things:

1 - I don't think I made it clear that the user enters a variable message after the line that says
Thank you.
______________________________________________________________________
For example, the user might enter "The building will be closed tomorrow for repairs". I would like for the user to not have to enter the subject line and their message text more than once. When I tried the code you provided, I had to enter both items in each Outlook mail window that popped up.

2 - When the first Outlook mail window popped up, the attached "Do you want to save changes?" message appeared. I clicked Cancel and the first window stayed open so that I was able to enter the subject line and message body but my users would never be able to deal with this.
outlook mail window.jpg
BTW, I already have ClickYes installed on the user's computer for another application. It works well.
Thanks again.
You do not have the required permissions to view the files attached to this post.
Carol W.

User avatar
HansV
Administrator
Posts: 78556
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Outlook Message via Access - breaking up recipient list

Post by HansV »

1. Change the part that sets the message and subject to

Code: Select all

  strSubject = InputBox("Please enter the subject for the e-mail message.")
  strMsg = InputBox("Please enter custom text to be added to  the e-mail message."
  strMsg = "<p><font face=""Arial"" size=""3"">The message below has been sent " & _
    "by the ALLV Broadcast system. <br><b>Please do not reply to this message. " & _
    "Replies to this message will be sent back to the ALLV address and will " & _
    "not reach the intended recipient. <u>To contact the original sender, " & _
    "forward this message to him/her and add your reply to that message.</u></b>" & _
    "<br> <br>Thank you. <br>" & strMsg & "</font></p>"
2. Does it help if you add a line

Code: Select all

      MsgBox "Please click OK when the e-mail has been sent.", vbInformation
immediately below .Display?
Best wishes,
Hans

User avatar
Carol W.
3StarLounger
Posts: 390
Joined: 26 Jan 2010, 16:02
Location: Las Vegas, NV

Re: Outlook Message via Access - breaking up recipient list

Post by Carol W. »

Perfect! :clapping:

Thanks, as always.
Carol W.

User avatar
Carol W.
3StarLounger
Posts: 390
Joined: 26 Jan 2010, 16:02
Location: Las Vegas, NV

Re: Outlook Message via Access - breaking up recipient list

Post by Carol W. »

Hans,

One more question:

Why did the insertion of the msgbox statement immediately after the last .Display statement eliminate the "Do you want to save ...?" pop-up box?

Just curious.

Thanks.
Carol W.

User avatar
HansV
Administrator
Posts: 78556
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Outlook Message via Access - breaking up recipient list

Post by HansV »

The popup question actually occurs in the next iteration of the loop, when a new message is created using

Set objEmail = objOutlook.CreateItem(olMailItem)

At that point, Outlook asks what it should do with the not-yet-sent previous message. Displaying the message box gives you the time to switch to the e-mail and to send (or discard) it.
Best wishes,
Hans

User avatar
Carol W.
3StarLounger
Posts: 390
Joined: 26 Jan 2010, 16:02
Location: Las Vegas, NV

Re: Outlook Message via Access - breaking up recipient list

Post by Carol W. »

One more question (sorry :smile:)

Is there a limit on the size of the string returned by the InputBox command? Sometimes our messages are a bit wordy.

If there is a limit, is there an alternative to using the InputBox command?

Thanks.
Carol W.

User avatar
HansV
Administrator
Posts: 78556
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Outlook Message via Access - breaking up recipient list

Post by HansV »

The string returned by InputBox can be at most 254 characters long.

An elegant workaround is to place text boxes on a form for subject and custom body text, and to refer to these instead of using InputBox. Post back if you need details.
Best wishes,
Hans

JohnH
3StarLounger
Posts: 287
Joined: 09 Mar 2010, 23:16
Location: Canberra Australia

Re: Outlook Message via Access - breaking up recipient list

Post by JohnH »

I have used loop code like this for quite a while, but it only solves half the problem. It reduces the number of addresses within each message, but does not change the total messages sent.
ISPs often have a limit on the total number of messages that can be sent within a given time period.

What is needed is some sort of delay mechanism, so the first 50 (?) messages are sent now, then another 50 every hour until the end. Can you add a "sent at" value to a message so Outlook handles the delay?
Regards

John

User avatar
HansV
Administrator
Posts: 78556
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Outlook Message via Access - breaking up recipient list

Post by HansV »

The MailItem object has a property DeferredDeliveryTime that can be set to specify the delivery time, but I don't know if that will help.
Best wishes,
Hans

JohnH
3StarLounger
Posts: 287
Joined: 09 Mar 2010, 23:16
Location: Canberra Australia

Re: Outlook Message via Access - breaking up recipient list

Post by JohnH »

Thanks ...I will give it a try.
Regards

John

User avatar
Carol W.
3StarLounger
Posts: 390
Joined: 26 Jan 2010, 16:02
Location: Las Vegas, NV

Re: Outlook Message via Access - breaking up recipient list

Post by Carol W. »

Hans,

As you suggested, I changed the InputBox commands to text boxes for both the subject and message body fields. I found that I needed to change the "Enter Key Behavior" property on the message body text box to allow the enter key to go to a new line within the text box. So far, so good.

The problem I now have is that when the Outlook message window opens, the text entered and programatically inserted into the Outlook window does not reflect my carriage returns that looked perfect in the Access text box.

Are there any solutions that you can think of?

BTW, this appears to have solved the original ISP related problems - i.e. no ISP message when the (two generated) messages go into the Sent Items folder and only one message received.

Thanks!
Carol W.

JohnH
3StarLounger
Posts: 287
Joined: 09 Mar 2010, 23:16
Location: Canberra Australia

Re: Outlook Message via Access - breaking up recipient list

Post by JohnH »

Is Outlook sending the messages as plain text or HTML?

If you are using HTML, then change the Text Format of the message text box to Rich Text, and Access will format the message as HTML. This also allows you to add other formatting to the message.
But to programmatically put some text into the text box you need to put in some basic HTML.. <P> or <br> for instance.
Regards

John

User avatar
Carol W.
3StarLounger
Posts: 390
Joined: 26 Jan 2010, 16:02
Location: Las Vegas, NV

Re: Outlook Message via Access - breaking up recipient list

Post by Carol W. »

The Outlook messages are in HTML format. Changing the Access text box text format to Rich Text did the trick.

:thankyou:
Carol W.

User avatar
Carol W.
3StarLounger
Posts: 390
Joined: 26 Jan 2010, 16:02
Location: Las Vegas, NV

Re: Outlook Message via Access - breaking up recipient list

Post by Carol W. »

Oops! Spoke too soon.

The text format property is available in my Access 2010 (and selecting RTF works) but is not available in the user's Access 2003. :groan:

Any suggestions?
Carol W.

JohnH
3StarLounger
Posts: 287
Joined: 09 Mar 2010, 23:16
Location: Canberra Australia

Re: Outlook Message via Access - breaking up recipient list

Post by JohnH »

Yes Rich Text is only available in 2007 and 2010.

You can test for the version when you load the form.

Code: Select all

If Application.Version > 11 Then
        Me.txtMessage.TextFormat = acTextFormatHTMLRichText
      Else
        Me.txtMessage.TextFormat = acTextFormatPlain
     End If
Then when you send the message you could set the BodyFormat of the message to olFormatHTML if the version is >11 and olFormatPLainText otherwise. PlainText messages would recognise the carriage returns (I think).
Regards

John

User avatar
HansV
Administrator
Posts: 78556
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Outlook Message via Access - breaking up recipient list

Post by HansV »

With a normal text box, you could try this:

strMsg = Replace(Me.txtCustomText, vbCrLf, "<br>")
strMsg = "<p><font face ...

This will replace line breaks in the contents of the text box with <br> tags.
Best wishes,
Hans

JohnH
3StarLounger
Posts: 287
Joined: 09 Mar 2010, 23:16
Location: Canberra Australia

Re: Outlook Message via Access - breaking up recipient list

Post by JohnH »

Using Rich Text for message, when possible, allows you to format the message, and have that carry through to the email.
RichText.gif
You do not have the required permissions to view the files attached to this post.
Regards

John

User avatar
Carol W.
3StarLounger
Posts: 390
Joined: 26 Jan 2010, 16:02
Location: Las Vegas, NV

Re: Outlook Message via Access - breaking up recipient list

Post by Carol W. »

This time I waited until I had installed the updated VBA code on the user's machine before I posted the results. :smile:

Success using the Replace statement! Thanks, Hans.

John, thanks for your suggested code to check the Access version number but since the code will only be running under Access 2003, I'll pass on installing that for now. I wish I could have used the RTF technique but . . .
Carol W.