I
Iona
Hi Duane, again, can I have some minor help... below is my 'send lots
of emails to outlook' code:
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
Now. problem is If outlook is not activelly open on the screen, the
emails go to outlooks outbook but do not send. They send beauitfully
if its open already, they also send beautifully if you include a
..display before .send, however who wants to look at umpteem copies of
the same email. Do you have any suggestions to remedy this? Other
than checking to see if outlook is open and prompting user to open
before clicking send I am stymied. Is it hard to programmically open
outlook (not just create an object etc)?
any help appreciated.
kind regards
iona
of emails to outlook' code:
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
Now. problem is If outlook is not activelly open on the screen, the
emails go to outlooks outbook but do not send. They send beauitfully
if its open already, they also send beautifully if you include a
..display before .send, however who wants to look at umpteem copies of
the same email. Do you have any suggestions to remedy this? Other
than checking to see if outlook is open and prompting user to open
before clicking send I am stymied. Is it hard to programmically open
outlook (not just create an object etc)?
any help appreciated.
kind regards
iona