S
serviceman via AccessMonster.com
Ok gang,
I've been getting wonderful advice and tips from this site, and now I have
something to give back (maybe!):
The following script to send a single email was found on an MSDN tech page,
but for the life of me I can't find the link (damned that delete history
button! ) I added the loop portion and changed the file lookup for
attachments a bit.
The following code will loop through a query and send an email to each email
address WITHOUT ANY SECURITY WARNINGS. This apparently works only with Office
XP...
HOWEVER.... (Knew that was coming, right?)
The script relies on the dreaded sendkeys command to send each email, and of
course it is a bit Kludgey...
The only issues I've seen with this script is the sendkey problems below:
1) Sendkeys absolutely hates Word as the editor. It will work 1 in 10 times.
2) When outlook is not using Word as the editor, all but the last email seem
to send 90% of the time, and the other 10% of the time it works fine. Why it
doesn't send the keystrokes to the last email is beyond me.
If anyone can come up with a fix for the sendkey portion, This will probably
be a very useful piece of code!
This is written using ADODB (we're on SQL server) but can use DAO recordset
as well. At present I have a form with text fields for subject,message, and
file path to attach that I pass in, and the email addresses are passed in
from the 1st query column (rs(0)).
Private Sub Command0_Click()
On Error GoTo Err_Command0_Click
Dim objOutlook As New Outlook.Application
Dim objMail As MailItem
Dim rs As ADODB.Recordset
'<Insert ISNULL or IF statements here to make sure subject,message and query
are not empty or null>
Set rs = New ADODB.Recordset
rs.Open "SELECT * FROM <your table> WHERE <your filter criteria>",
CurrentProject.Connection, adOpenDynamic, adLockOptimistic
With rs
Do Until rs.EOF
Set objOutlook = New Outlook.Application
Set objMail = objOutlook.CreateItem(olMailItem)
EmailAddr = rs(0)
'CopyEmailAddr = <whatever field you want to pass>
Subj = <whatever field you want to pass>
Body = <whatever field you want to pass>
PathName = <whateverfield you want to pass>
With objMail
.To = EmailAddr
.cc = CopyEmailAddr
.Subject = Subj
.Body = Body
.NoAging = True
If IsNull(PathName) = False Then
.Attachments.Add PathName
End If
.Display
End With
'Here is the problem area
SendKeys "%{s}", True 'send the email without prompts
'End of problem area
rs.MoveNext
Loop
Set objMail = Nothing
Set objOutlook = Nothing
End With
Exit_Command0_Click:
Exit Sub
Err_Command0_Click:
'<I took out some custom error msgs unique to my app here>
MsgBox Err.Description
End If
Resume Exit_Command0_Click
End Sub
If anyone can add to this, post it here so we can all benefit (me too!).
Andy
I've been getting wonderful advice and tips from this site, and now I have
something to give back (maybe!):
The following script to send a single email was found on an MSDN tech page,
but for the life of me I can't find the link (damned that delete history
button! ) I added the loop portion and changed the file lookup for
attachments a bit.
The following code will loop through a query and send an email to each email
address WITHOUT ANY SECURITY WARNINGS. This apparently works only with Office
XP...
HOWEVER.... (Knew that was coming, right?)
The script relies on the dreaded sendkeys command to send each email, and of
course it is a bit Kludgey...
The only issues I've seen with this script is the sendkey problems below:
1) Sendkeys absolutely hates Word as the editor. It will work 1 in 10 times.
2) When outlook is not using Word as the editor, all but the last email seem
to send 90% of the time, and the other 10% of the time it works fine. Why it
doesn't send the keystrokes to the last email is beyond me.
If anyone can come up with a fix for the sendkey portion, This will probably
be a very useful piece of code!
This is written using ADODB (we're on SQL server) but can use DAO recordset
as well. At present I have a form with text fields for subject,message, and
file path to attach that I pass in, and the email addresses are passed in
from the 1st query column (rs(0)).
Private Sub Command0_Click()
On Error GoTo Err_Command0_Click
Dim objOutlook As New Outlook.Application
Dim objMail As MailItem
Dim rs As ADODB.Recordset
'<Insert ISNULL or IF statements here to make sure subject,message and query
are not empty or null>
Set rs = New ADODB.Recordset
rs.Open "SELECT * FROM <your table> WHERE <your filter criteria>",
CurrentProject.Connection, adOpenDynamic, adLockOptimistic
With rs
Do Until rs.EOF
Set objOutlook = New Outlook.Application
Set objMail = objOutlook.CreateItem(olMailItem)
EmailAddr = rs(0)
'CopyEmailAddr = <whatever field you want to pass>
Subj = <whatever field you want to pass>
Body = <whatever field you want to pass>
PathName = <whateverfield you want to pass>
With objMail
.To = EmailAddr
.cc = CopyEmailAddr
.Subject = Subj
.Body = Body
.NoAging = True
If IsNull(PathName) = False Then
.Attachments.Add PathName
End If
.Display
End With
'Here is the problem area
SendKeys "%{s}", True 'send the email without prompts
'End of problem area
rs.MoveNext
Loop
Set objMail = Nothing
Set objOutlook = Nothing
End With
Exit_Command0_Click:
Exit Sub
Err_Command0_Click:
'<I took out some custom error msgs unique to my app here>
MsgBox Err.Description
End If
Resume Exit_Command0_Click
End Sub
If anyone can add to this, post it here so we can all benefit (me too!).
Andy