problem in (Set objOutlookRecip = .Recipients.Add(TheAddress))

M

Masoud

Hello i have used the below code for sending mail, it works when Microsoft
outlook is already open but when it is not open and run the code i have
problem in this line.
Set objOutlookRecip = .Recipients.Add(TheAddress)
How i can solve my problem without opening Microsoft outlook.
Thanks.

Option Compare Database

Option Explicit

Sub SendMessages(Optional AttachmentPath)

Dim MyDB As Database
Dim MyRS As Recordset
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 qdf As DAO.QueryDef
Dim prm As DAO.Parameter

Set MyDB = CurrentDb
Set qdf = MyDB.QueryDefs!qryCcmaillisttranstocon
For Each prm In qdf.Parameters
prm.Value = Eval(prm.Name) 'different for prompts
Next prm
Set MyRS = qdf.OpenRecordset(dbOpenDynaset)
MyRS.MoveFirst

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

Do Until MyRS.EOF
' Create the e-mail message.
Set objOutlookMsg = objOutlook.CreateItem(olMailItem)
TheAddress = MyRS![CcAddress]
MsgBox TheAddress
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!frmTranstoCon!EmailAddress)) Then
Else
Set objOutlookRecip =
..Recipients.Add(Forms!frmTranstoCon!EmailAddress)
MsgBox Forms!frmTranstoCon!EmailAddress
objOutlookRecip.TYPE = olCC
End If

' Set the Subject, the Body, and the Importance of the e-mail message.
.Subject = Forms!frmTranstoCon!Subject
MsgBox Forms!frmTranstoCon!Subject
.Body = Forms!frmTranstoCon!Subject
.Importance = olImportanceHigh 'High importance

'Add the attachment to the e-mail message.
If Not IsMissing(AttachmentPath) Then
Set objOutlookAttach = .Attachments.Add(AttachmentPath)
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
 

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