send email using outlook

R

Richard

Hi

I am using access to automate sending emails using outlook. How do I send
emails without opening Outlook or can I?

Many thanks in advance
Richard
 
J

james.igoe

I have not used the following code - I usually use the Outlook Express
client since my work is always on corporate standard clients with
Outlook set - but it seems easy enough; you would only need to automate
collection/setting of e-mail strings:

**Module (space formatting added to make it easier to read):

Public Declare Function ShellExecute Lib "shell32.dll"
Alias "ShellExecuteA"
(ByVal hwnd As Long,
ByVal lpOperation As String,
ByVal lpFile As String,
ByVal lpParameters As String,
ByVal lpDirectory As String,
ByVal nShowCmd As Long)
As Long

'**Code attached to a button:

Dim stext As String

'Hard coded parts of the e-mail, stripped down to the minimum
stext = "mailto:[email protected]?"
stext = stext & "&Subject=" & "Document attached"
stext = stext & "&Body=" & "Please find the document attached"

'Launch default e-mail
Call ShellExecute(hwnd, "open", stext, vbNullString, vbNullString,
SW_SHOWNORMAL)
 
R

Richard

Hi James

I have a code to automate using Outlook Express but I can't seem to be able
to set attachments. Does you code allow you to set attachments?

Thanks and regards
Richard
 
R

Richard

Hi James

I have a code to automate using Outlook Express but I can't seem to be able
to set attachments. Does you code allow you to set attachments?

Thanks and regards
Richard
 
J

james.igoe

I haven't used the code provided; I wrote that in the first sentence.
I do have Outlook code, and to send mail one needs to replace .Display
with .Send in the with area. This code will allow attachments:


Sub RequestAccess()

Dim objOutlookApp As Object
Dim objOutlookMail As Object

Set objOutlookApp = CreateObject("Outlook.Application")
Set objOutlookMail = objOutlookApp.CreateItem(0)

Dim strRecipients As String
Dim CurrentUser As String

strRecipients = "[Put you recipients here]"

With objOutlookMail
.To = strRecipients
.Subject = "Request..."
.Display
End With

'releases resources from outlook and associated components
If Not objOutlookApp Is Nothing Then

objOutlookApp.Quit

If Not objOutlookMail Is Nothing Then
Set objOutlookMail = Nothing
End If

Set objOutlookApp = Nothing

End If

Exit Sub

ErrorTrap:

MsgBox Err.Number & " : " & Err.Description, vbOKOnly, "Error"

'releases resources from outlook and associated components
If Not objOutlookApp Is Nothing Then

objOutlookApp.Quit

If Not objOutlookMail Is Nothing Then
Set objOutlookMail = Nothing
End If

Set objOutlookApp = Nothing

End If

End Sub
 
J

james.igoe

I haven't used the code provided; I wrote that in the first sentence.
I do have Outlook code, and to send mail one needs to replace .Display
with .Send in the with area. This code will allow attachments:


Sub RequestAccess()

Dim objOutlookApp As Object
Dim objOutlookMail As Object

Set objOutlookApp = CreateObject("Outlook.Application")
Set objOutlookMail = objOutlookApp.CreateItem(0)

Dim strRecipients As String
Dim CurrentUser As String

strRecipients = "[Put you recipients here]"

With objOutlookMail
.To = strRecipients
.Subject = "Request..."
.Display
End With

'releases resources from outlook and associated components
If Not objOutlookApp Is Nothing Then

objOutlookApp.Quit

If Not objOutlookMail Is Nothing Then
Set objOutlookMail = Nothing
End If

Set objOutlookApp = Nothing

End If

Exit Sub

ErrorTrap:

MsgBox Err.Number & " : " & Err.Description, vbOKOnly, "Error"

'releases resources from outlook and associated components
If Not objOutlookApp Is Nothing Then

objOutlookApp.Quit

If Not objOutlookMail Is Nothing Then
Set objOutlookMail = Nothing
End If

Set objOutlookApp = Nothing

End If

End Sub
 
R

Richard

Thanks James

Appreciate your help and time

Richard

I haven't used the code provided; I wrote that in the first sentence.
I do have Outlook code, and to send mail one needs to replace .Display
with .Send in the with area. This code will allow attachments:


Sub RequestAccess()

Dim objOutlookApp As Object
Dim objOutlookMail As Object

Set objOutlookApp = CreateObject("Outlook.Application")
Set objOutlookMail = objOutlookApp.CreateItem(0)

Dim strRecipients As String
Dim CurrentUser As String

strRecipients = "[Put you recipients here]"

With objOutlookMail
.To = strRecipients
.Subject = "Request..."
.Display
End With

'releases resources from outlook and associated components
If Not objOutlookApp Is Nothing Then

objOutlookApp.Quit

If Not objOutlookMail Is Nothing Then
Set objOutlookMail = Nothing
End If

Set objOutlookApp = Nothing

End If

Exit Sub

ErrorTrap:

MsgBox Err.Number & " : " & Err.Description, vbOKOnly, "Error"

'releases resources from outlook and associated components
If Not objOutlookApp Is Nothing Then

objOutlookApp.Quit

If Not objOutlookMail Is Nothing Then
Set objOutlookMail = Nothing
End If

Set objOutlookApp = Nothing

End If

End Sub


Hi James

I have a code to automate using Outlook Express but I can't seem to be able
to set attachments. Does you code allow you to set attachments?

Thanks and regards
Richard
 
R

Ron2006

The following will work: (use only the parts of the email message that
you need). These are all of the aspects of outlook email that I have
found so far.

This assums the button name is command0.
If Outlook is NOT open when this is done, then the email simply goes
into the outbox.

==============================
Private Sub Command0_Click()
On Error GoTo Error_Handler

Dim objOutlook As Outlook.Application
Dim objEmail As Outlook.MailItem

Set objOutlook = CreateObject("Outlook.application")
Set objEmail = objOutlook.CreateItem(olMailItem)

With objEmail
.To = "(e-mail address removed)"
.CC = "(e-mail address removed)"
.BCC = "(e-mail address removed)"
.Subject = "Look at this sample attachment"
.body = "The body doesn't matter, just the attachment"
.Attachments.Add "C:\Test.htm"
'.attachments.Add "c:\Path\to\the\next\file.txt"

' ========================================
' next two only if the body is to include HTML

.bodyformat = 2 ' not necessary if no html this makes
it html
' 1 is text 2 is
HTML 3 is RTF
.htmlbody = Chr(13) & Chr(13) & _
"<body>" & _
"<Table>" &_
"<tr>" &_
"<td><b> Date: </b></td>" & _
"<td>" & Date & "</td>" & _
"</tr>" &_
"<tr>" &_
"<td></td>" & _
"<td></td>" & _
"</tr>" &_
" </Table>" &_
"</body>"

'==============================

.Send
'.ReadReceiptRequested
'.Display ' to see the email have have user actually do the
send
' don't use the .Send if you are using
Display


'
End With

Exit_Here:
Set objOutlook = Nothing
Exit Sub

Error_Handler:
MsgBox Err & ": " & Err.Description
Resume Exit_Here

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