Problem sending 2 emails from Access

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have the following code to send two seperate emails:

***Start Code***
'1St Email
Dim olApp As Outlook.Application
Dim objMail As Outlook.MailItem
Set olApp = Outlook.Application
Set objMail = olApp.CreateItem(olMailItem)
With objMail
.To = "(e-mail address removed)"
.Subject = "Test subject1"
.BodyFormat = olFormatPlain
.Body = "Narrative in here."
.Attachments.Add "Tess1.pdf"
.Send
End With

'2nd Email
'Dim olApp As Outlook.Application
'Dim objMail As Outlook.MailItem
'Set olApp = Outlook.Application
'Set objMail = olApp.CreateItem(olMailItem)
With objMail
.To = "(e-mail address removed)"
.Subject = "Test subject2"
.BodyFormat = olFormatPlain
.Body = "Narrative in here."
.Attachments.Add "Test2.pdf"
.Send
End With
***End Code***

When I comment out the code as shown Access habgs, when I leave it in I get
this message:
Duplicate declaration in current scope.

I have tried various other syntax but cannot seem to get it right. Can you
help?

Thanks in advance.
 
Hi,

The error message is correct, because you declare a variable twice.

I think you may put the code that send a mail into one function. Create
another function and call first function twice to send 2 mails.

Public function SendMail(strTo as string, strSubject as string, strBody as
string, strAttach as string)
Dim olApp As Outlook.Application
Dim objMail As Outlook.MailItem
Set olApp = Outlook.Application
Set objMail = olApp.CreateItem(olMailItem)
With objMail
.To = strTo
.Subject = strSubject
.BodyFormat = olFormatPlain
.Body = strBody
.Attachments.Add strAttach
.Send
End With

set objMail = nothing
set olApp = nothing

End function

Public function Send2mails()
SendMail "(e-mail address removed)","Test subject 1", "Narrative in here.",
"Tess1.pdf"
SendMail "(e-mail address removed)","Test subject 2", "Narrative in here.",
"Tess2.pdf"

End function

Regards
Tran Hong Quang
 
Back
Top