Duane H! minor outlook woe

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
 
D

Duane Hookom

I haven't used any Outlook automation code in years. You might want to post
in an Outlook NG if you don't get an answer here. You can also check the
resources at http://www.slipstick.com/.

--
Duane Hookom
MS Access MVP

Iona said:
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
 
N

Nick 'The database Guy'

Hi Iona,

Why not use the shell command and open Outlook first?

Good luck

Nick
 

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