Outlook 03 automation from Access 03 minor problem

I

Iona

Hi, I have the below code to send mulitple emails from a query in
Access. It actually all works surprisingly well, except for one small
problem. If the outlook window is open, all the emails are sent
automatically, however if it is not, then they are just sent to the
sendmail folder and wait for the user to click 'send' in Outlook.
Likewise if I add .display before .send, the emails are sent
automatically, however for obvious reason this is not practial. So my
question is, can I programmatically open the Outlook object Window, or
do I need to rely on checking to see if Outlook is running and if not
prompt the user to open it. Any thoughts on this matter would be
greatly appreciated. The code is as:

Private Sub btnSend_Click()

Dim mydb As Database
Dim MyRS As Recordset
Dim inpText As Object
Dim varText As Object
Dim objText As Object
Dim objOutlook As Outlook.Application
Dim objOutlookMsg As Outlook.MailItem
Dim objOutlookRecip As Outlook.Recipient
Dim objOutlookAttach As Outlook.Attachment
Dim TheAddress As String
Dim strBody As String

Set mydb = CurrentDb
Set MyRS = mydb.OpenRecordset("MyQuery")
MyRS.MoveFirst

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

End If
Do Until MyRS.EOF
' Create the e-mail message.
Set objOutlookMsg = objOutlook.CreateItem(olMailItem)
TheAddress = MyRS![EmailName]

With objOutlookMsg
' Add the To recipients to the e-mail message.
Set objOutlookRecip = .Recipients.Add(TheAddress)
objOutlookRecip.Type = olTo

' Add the Cc recipients to the e-mail message.
If (IsNull(Forms!frmMail!CCAddress)) Then
Else
Set objOutlookRecip =
..Recipients.Add(Forms!frmMail!CCAddress)
objOutlookRecip.Type = olCC
End If
Set objText = CreateObject("Scripting.FileSystemObject")
' Set the Subject, the Body, and the Importance of the e-mail
message.

.Subject = Forms!frmMail!Subject

If (IsNull(Forms!frmMail!tbFileInsert)) Then
Else
Set varText =
objText.getfile(Forms!frmMail!tbFileInsert)
Set inpText = varText.OpenAsTextStream(1, -2)
strBody = inpText.readall
inpText.Close
.HTMLBody = strBody
End If

If (IsNull(Forms!frmMail!MainText)) Then
Else
.Body = Forms!frmMail!MainText
End If

.Importance = olImportanceHigh 'High importance

'Add the attachment to the e-mail message.
If (IsNull(Forms!frmMail!AttachmentPath)) Then
Else
Set objOutlookAttach =
..Attachments.Add((Forms!frmMail!AttachmentPath), olByValue, 1)
End If

' Resolve the name of each Recipient.
For Each objOutlookRecip In .Recipients
objOutlookRecip.Resolve
If Not objOutlookRecip.Resolve Then
objOutlookMsg.Display
End If
Next
.Send
End With
MyRS.MoveNext
Loop
Set objOutlookMsg = Nothing
Set objOutlook = Nothing
End Sub

kind regards,
iona
 

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