Sending Email from Form

M

magmike

I'm pretty certain the following will work:

Private Sub Command50_Click()
Dim stURL As String
stURL = "mailto:" & & "?Subject=" & [Subject] & "?Body=" &
[Body]
Application.FollowHyperlink stURL
End Sub

....however, I'd like to add attachments. How could I do this? (and is
my code correct up there?)

Thanks in advance!
magmike
 
M

magmike

FYI - this is what I use:

Sub SendMessage(DisplayMsg As Boolean, Optional AttachmentPath)

    Dim objOutlook As Outlook.Application
    Dim objOutlookMsg As Outlook.MailItem
    Dim objOutlookRecip As Outlook.Recipient
    Dim objOutlookAttach As Outlook.Attachment

    Dim Mrecip As String
    Dim Mrecname As String
    Mrecip = Forms!ordersfrm.EmailAdd
    MrecipName = Forms!ordersfrm.CustID.Column(3)

    DoCmd.SetWarnings off

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

'Create the message.

Set objOutlookMsg = objOutlook.CreateItem(olMailItem)

With objOutlookMsg
    ' Add the To receipient(s) to the message.
    Set objOutlookRecip = .Recipients.Add(Mrecip)

    ' Set the Subject and Body of the message.
    .Subject = "Your Order Number " & Forms!ordersfrm.CustID.Column(4) & " "
& Forms!ordersfrm.CustID.Column(3)
    .Body = vbCrLf & _
    "Thank you for your recent order. " & _
    vbCrLf & vbCrLf & "Please print the attachment (total of 3 pages).. Sign
two of the pages and fax back toll free to (877)000-0000 using the cover
sheet provided. " & vbCrLf & vbCrLf & _
    "Further, if you can arrange to return by email . . . ." & vbCrLf&
vbCrLf & _
    "Your order will not continue to be processed until your signed invoice
is received via fax." & vbCrLf & vbCrLf & _
    "Kind regards, " & vbCrLf & vbCrLf & _
    "Sales Guy" & vbCrLf & _
    "Process Department" & vbCrLf & _
    "My Company" & vbCrLf & _
    "(877)000-0000 Office" & vbCrLf & _
    "(877)000-0000 Fax" & vbCrLf & _
    "www.mycompany.com" & vbCrLf & vbCrLf & vbCrLf

    ' Add attachments to the message.
    If Not IsMissing(AttachmentPath) Then
        Set objOutlookAttach = .Attachments.Add(AttachmentPath)
    End If

    ' Resolve Recipient's name.
    For Each objOutlookRecip In .Recipients
        objOutlookRecip.Resolve
    Next

    ' Should we display the message before sending?
    If DisplayMsg Then
        .Display
    Else
        .Save
        .Send
    End If

End With

    Set objOutlook = Nothing

End Sub

Bonniehttp://www.dataplus-svc.com
I'm pretty certain the following will work:
Private Sub Command50_Click()
Dim stURL As String
stURL = "mailto:" & & "?Subject=" & [Subject] & "?Body=" &
[Body]
Application.FollowHyperlink stURL
End Sub[/QUOTE]
[QUOTE]
...however, I'd like to add attachments. How could I do this? (and is
my code correct up there?)[/QUOTE]
[QUOTE]
Thanks in advance!
magmike[/QUOTE]
[/QUOTE]

Wow! I used this code (modified of course) and am getting all sorts of
errors! Here is what I have modified it to:

Private Sub SendMessage_Click(DisplayMsg As Boolean, Optional
AttachmentPath)

Dim objOutlook As Outlook.Application
Dim objOutlookMsg As Outlook.MailItem
Dim objOutlookRecip As Outlook.Recipient
Dim objOutlookAttach As Outlook.Attachment

Dim Mrecip As String
Mrecip = Forms!ProspectForm.EmailAddress

DoCmd.SetWarnings off

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

'Create the message.
Set objOutlookMsg = objOutlook.CreateItem(olMailItem)

With objOutlookMsg
' Add the To receipient(s) to the message.
Set objOutlookRecip = .Recipients.Add(Mrecip)

' Set the Subject and Body of the message.
.Subject = [NOTES]
.Body = vbCrLf & _
[LetterBody]

' Add attachments to the message.
If Not IsMissing(AttachmentPath) Then
Set objOutlookAttach = .Attachments.Add(AttachmentPath)
End If

' Resolve Recipient's name.
For Each objOutlookRecip In .Recipients
objOutlookRecip.Resolve
Next

' Should we display the message before sending?
If DisplayMsg Then
.Display
Else
.Save
.Send
End If

End With

Set objOutlook = Nothing

End Sub

I'm getting an error about "NewMonth" and "NewYear" on the opening of
the form, and then when I click on the button I get an error as well.
They all end saying something about an Ambiguous name
"SendMessage_Cick"
 
M

magmike

Why do you name your button Command50?????



I'm pretty certain the following will work:
Private Sub Command50_Click()
Dim stURL As String
stURL = "mailto:" & & "?Subject=" & [Subject] & "?Body=" &
[Body]
Application.FollowHyperlink stURL
End Sub[/QUOTE]
[QUOTE]
...however, I'd like to add attachments. How could I do this? (and is
my code correct up there?)[/QUOTE]
[QUOTE]
Thanks in advance!
magmike- Hide quoted text -[/QUOTE]

- Show quoted text -[/QUOTE]

That was the name automatically given to it by Access. I just didn't
rename it before pasting the code.
 
M

magmike

FYI - this is what I use:
Sub SendMessage(DisplayMsg As Boolean, Optional AttachmentPath)
    Dim objOutlook As Outlook.Application
    Dim objOutlookMsg As Outlook.MailItem
    Dim objOutlookRecip As Outlook.Recipient
    Dim objOutlookAttach As Outlook.Attachment
    Dim Mrecip As String
    Dim Mrecname As String
    Mrecip = Forms!ordersfrm.EmailAdd
    MrecipName = Forms!ordersfrm.CustID.Column(3)
    DoCmd.SetWarnings off
' Create the outlook session.
Set objOutlook = CreateObject("Outlook.Application")
'Create the message.
Set objOutlookMsg = objOutlook.CreateItem(olMailItem)
With objOutlookMsg
    ' Add the To receipient(s) to the message.
    Set objOutlookRecip = .Recipients.Add(Mrecip)
    ' Set the Subject and Body of the message.
    .Subject = "Your Order Number " & Forms!ordersfrm.CustID.Column(4) & " "
& Forms!ordersfrm.CustID.Column(3)
    .Body = vbCrLf & _
    "Thank you for your recent order. " & _
    vbCrLf & vbCrLf & "Please print the attachment (total of 3 pages). Sign
two of the pages and fax back toll free to (877)000-0000 using the cover
sheet provided. " & vbCrLf & vbCrLf & _
    "Further, if you can arrange to return by email . . . ." & vbCrLf &
vbCrLf & _
    "Your order will not continue to be processed until your signedinvoice
is received via fax." & vbCrLf & vbCrLf & _
    "Kind regards, " & vbCrLf & vbCrLf & _
    "Sales Guy" & vbCrLf & _
    "Process Department" & vbCrLf & _
    "My Company" & vbCrLf & _
    "(877)000-0000 Office" & vbCrLf & _
    "(877)000-0000 Fax" & vbCrLf & _
    "www.mycompany.com" & vbCrLf & vbCrLf & vbCrLf
    ' Add attachments to the message.
    If Not IsMissing(AttachmentPath) Then
        Set objOutlookAttach = .Attachments.Add(AttachmentPath)
    End If
    ' Resolve Recipient's name.
    For Each objOutlookRecip In .Recipients
        objOutlookRecip.Resolve
    Next
    ' Should we display the message before sending?
    If DisplayMsg Then
        .Display
    Else
        .Save
        .Send
    End If
    Set objOutlook = Nothing
End Sub

magmike said:
I'm pretty certain the following will work:
Private Sub Command50_Click()
Dim stURL As String
stURL = "mailto:" & & "?Subject=" & [Subject] & "?Body="&
[Body]
Application.FollowHyperlink stURL
End Sub
...however, I'd like to add attachments. How could I do this? (and is
my code correct up there?)
Thanks in advance!
magmike[/QUOTE][/QUOTE]

Wow! I used this code (modified of course) and am getting all sorts of
errors! Here is what I have modified it to:

Private Sub SendMessage_Click(DisplayMsg As Boolean, Optional
AttachmentPath)

    Dim objOutlook As Outlook.Application
    Dim objOutlookMsg As Outlook.MailItem
    Dim objOutlookRecip As Outlook.Recipient
    Dim objOutlookAttach As Outlook.Attachment

    Dim Mrecip As String
    Mrecip = Forms!ProspectForm.EmailAddress

    DoCmd.SetWarnings off

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

'Create the message.
Set objOutlookMsg = objOutlook.CreateItem(olMailItem)

With objOutlookMsg
    ' Add the To receipient(s) to the message.
    Set objOutlookRecip = .Recipients.Add(Mrecip)

    ' Set the Subject and Body of the message.
    .Subject = [NOTES]
    .Body = vbCrLf & _
    [LetterBody]

    ' Add attachments to the message.
    If Not IsMissing(AttachmentPath) Then
        Set objOutlookAttach = .Attachments.Add(AttachmentPath)
    End If

    ' Resolve Recipient's name.
    For Each objOutlookRecip In .Recipients
        objOutlookRecip.Resolve
    Next

    ' Should we display the message before sending?
    If DisplayMsg Then
        .Display
    Else
        .Save
        .Send
    End If

End With

    Set objOutlook = Nothing

End Sub

I'm getting an error about "NewMonth" and "NewYear"  on the opening of
the form, and then when I click on the button I get an error as well.
They all end saying something about an Ambiguous name
"SendMessage_Cick"- Hide quoted text -

- Show quoted text -[/QUOTE]

I found the cause of thos errors - there was another sub in the way.
Fixed that and realized the next error was because of the definitions
in parenthesis. Eliminated that (I knew this would cause it's own
error) and the form opens now without error, AND when I press the
button it starts to run it - opens Outlook, ask me for permission to
access address book, then I get an error from Access saying "Method
'Add' of object 'Attachments' failed".

I'm a hack, so I apologize for this next question, but starting the
code as you did "Sub SendMessage(stuff...)" how do I invoke that on
the pushing of my button?

Thanks!
 
M

magmike

Here's how I do it:

Private Sub E_Mail_Click()
On Error GoTo Err_E_Mail_Click
Dim morder As String
Dim minvoice As String

minvoice = "C:\yourfolder\Invoice.pdf"

If IsNull(Forms!ordersfrm.EmailAdd) Then
    MsgBox "There is no email address for this customer!"
Else
    DoCmd.OpenReport "Invoice", acViewNormal
    SendMessage True, minvoice
End If

Exit_E_Mail_Click:
    Exit Sub

Err_E_Mail_Click:
If IsNull(Forms!ordersfrm.EmailAdd) Then
    MsgBox "There is no email address for this customer!"
End If
    'MsgBox Err.Description
    Resume Exit_E_Mail_Click

End Sub

I open the invoice AcViewNormal which sends to print and the default printer
is to pdf.  If you are in Access 2007 I believe it is built in or you can use
CutePDF (which is a free utility) if you are in an earlier version.  Or
attach something else.

Bonniehttp://www.dataplus-svc.com




On Aug 21, 10:17 am, "bhicks11 via AccessMonster.com" <u44327@uwe>
wrote:
[quoted text clipped - 151 lines]
- Show quoted text -
I found the cause of thos errors - there was another sub in the way.
Fixed that and realized the next error was because of the definitions
in parenthesis. Eliminated that (I knew this would cause it's own
error) and the form opens now without error, AND when I press the
button it starts to run it - opens Outlook, ask me for permission to
access address book, then I get an error from Access saying "Method
'Add' of object 'Attachments' failed".
I'm a hack, so I apologize for this next question, but starting the
code as you did "Sub SendMessage(stuff...)" how do I invoke that on
the pushing of my button?

So where do I put the sub? Anywhere?
 
M

magmike

Forgot to mention:  my button is E_Mail and this is in the OnClick event in
case you couldn't tell that.

Bonniehttp://www.dataplus-svc.com




Here's how I do it:
Private Sub E_Mail_Click()
On Error GoTo Err_E_Mail_Click
Dim morder As String
Dim minvoice As String
minvoice = "C:\yourfolder\Invoice.pdf"
If IsNull(Forms!ordersfrm.EmailAdd) Then
   MsgBox "There is no email address for this customer!"
Else
   DoCmd.OpenReport "Invoice", acViewNormal
   SendMessage True, minvoice
End If
Exit_E_Mail_Click:
   Exit Sub
Err_E_Mail_Click:
If IsNull(Forms!ordersfrm.EmailAdd) Then
   MsgBox "There is no email address for this customer!"
End If
   'MsgBox Err.Description
   Resume Exit_E_Mail_Click
I open the invoice AcViewNormal which sends to print and the default printer
is to pdf.  If you are in Access 2007 I believe it is built in or you can use
CutePDF (which is a free utility) if you are in an earlier version.  Or
attach something else.
On Aug 21, 10:17 am, "bhicks11 via AccessMonster.com" <u44327@uwe>
wrote:
[quoted text clipped - 15 lines]

Okay. I got it now!

How could I modify the sub to ensure that my default signature is also
included?
Also, can I bypass the permission to access request from Outlook?
 
M

magmike

If using Outlook (any version), try this code which has been working for the
last 10+ years:

http://www.datastrat.com/Code/OutlookEmail.txt

Remember to set a reference to Outlook in your code window:

Tools >>> References

--
Arvin Meyer, MCP, MVPhttp://www.datastrat.comhttp://www.mvps.org/accesshttp://www.accessmvp.com




I'm pretty certain the following will work:
Private Sub Command50_Click()
Dim stURL As String
stURL = "mailto:" & & "?Subject=" & [Subject] & "?Body=" &
[Body]
Application.FollowHyperlink stURL
End Sub[/QUOTE]
[QUOTE]
...however, I'd like to add attachments. How could I do this? (and is
my code correct up there?)[/QUOTE]
[QUOTE]
Thanks in advance!
magmike- Hide quoted text -[/QUOTE]

- Show quoted text -[/QUOTE]

-- How would I go about ensuring my default signature is being used
(because it's not)?
-- Also, Is there a way to bypass Outlook's asking for permission to
allow outside access to Outlook?
 
A

Arvin Meyer [MVP]

-- How would I go about ensuring my default signature is being used
(because it's not)?
-- Also, Is there a way to bypass Outlook's asking for permission to
allow outside access to Outlook?

I don't see any signature property, so either you can ask in an Outlook
newsgroup, or recreate it as a string:

Dim strSIG As String
strSIG = "Arvin Meyer" & vbCrLf & "Microsoft Access MVP" & vbCrLf &
"Something Else"

and replace this line:
..body = "The body doesn't matter, just the attachment"

with:
..body = strSIG

You can bypass the Outlook security prompts with a dll named Redemption:

http://www.dimastr.com/redemption/
 
A

Arvin Meyer [MVP]

Yes it does, because that's built into Outlook. I use Redemption which has
many more features as well to deal with the warnings. Redemption is free for
personal use and $100 (at least that's what we paid for it) for enterprise
use (in our case 1600 computers)

http://www.dimastr.com/redemption/
--
Arvin Meyer, MCP, MVP
http://www.datastrat.com
http://www.mvps.org/access
http://www.accessmvp.com


bhicks11 via AccessMonster.com said:
Hi Arvin,

Does your example generate the warning? I think it's cleaner.

Bonnie
http://www.dataplus-svc.com
If using Outlook (any version), try this code which has been working for
the
last 10+ years:

http://www.datastrat.com/Code/OutlookEmail.txt

Remember to set a reference to Outlook in your code window:

Tools >>> References
I'm pretty certain the following will work:
[quoted text clipped - 10 lines]
Thanks in advance!
magmike
 
T

Tony Toews [MVP]

Now all the replies assume you and your users all have Outlook
installed and have the same version of Outlook. These assumptions
will, at some future time, come back to haunt you. <smile>

So once you have all the code working to your satisfaction read the
following.

Late binding means you can safely remove the reference and only have
an error when the app executes lines of code in question. Rather than
erroring out while starting up the app and not allowing the users in
the app at all. Or when hitting a mid, left or trim function call.

This also is very useful when you don't know version of the external
application will reside on the target system. Or if your organization
is in the middle of moving from one version to another.

For more information including additional text and some detailed links
see the "Late Binding in Microsoft Access" page at
http://www.granite.ab.ca/access/latebinding.htm

Tony
--
Tony Toews, Microsoft Access MVP
Please respond only in the newsgroups so that others can
read the entire thread of messages.
Microsoft Access Links, Hints, Tips & Accounting Systems at
http://www.granite.ab.ca/accsmstr.htm
Tony's Microsoft Access Blog - http://msmvps.com/blogs/access/
 

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

Similar Threads


Top