Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Shaun E on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Automatically Sending Outlook Email By Clicking A Button in an Access 1

Status
Not open for further replies.

StarScream

Technical User
Oct 10, 2001
46
US
Automatically Sending Outlook Email By Clicking A Button in an Access 2000

Scenario: Trying to have a button send an email using some information from a form, some information as part of the code.

Here is what I have so far. Can't seem to get it to work.
Code:
Private Sub CmdSend_Click()

Dim olapp As Object
Dim oitem As Object

Set mSubject.Value = "You Have Mail!"
Set mBody.Value = "Message information."
Set ComBcc.Value = ""
Set ComCc.Value = ""
Set ComTo.Value = "pajamas94@home.com"
Set olapp = CreateObject("Outlook.Application")
Set oitem = olapp.CreateItem(0)
With oitem
.Subject = mSubject
.To = ComTo.Value & ";"
.Body = mBody
If ComCc.Value <> &quot;&quot; Then
.cc = ComCc.Value & &quot;;&quot;
End If
If ComBcc.Value <> &quot;&quot; Then
.bcc = ComBcc.Value & &quot;;&quot;
End If
.Send
End With
Set olapp = Nothing
Set oitem = Nothing

Unload Me
End Sub

I keep getting a error everytime. Where is my error? Thanks.

PJ
 
You are using an Unload statement which has a referenced to an invalid object or control. You cannot load or unload an
an object that isn't a loadable object, such as Screen, Printer, or Clipboard. Delete the erroneous statement from your code.

Also, why not Reference the object in your Microsoft Access application and avoid late binding. On the VBA IDE menu, select Tools/Reference. Then select 'Microsoft Outlook 9.0 Object'.

Enter the following code:
Private Sub Command0_Click()

Dim olapp As Outlook.Application
Dim oitem As Outlook.MailItem
Dim sSubject As String
Dim sBody As String
Dim sTo As String
Dim sCC as String
Dim sBCC as String

sSubject = &quot;You Have Mail!&quot;
sBody = &quot;Message information.&quot;
'Set ComBcc.Value = &quot;&quot;
'Set ComCc.Value = &quot;&quot;
sTo = &quot;pajamas94@home.com&quot;
Set olapp = New Outlook.Application
Set oitem = olapp.CreateItem(olMailItem)
With oitem
.Subject = sSubject
.To = sTo
.Body = sBody
If Len(sCC) > 0 Then .CC = sCC
If Len(sBCC) > 0 Then .BCC = sBCC
.Send
End With
Set olapp = Nothing
Set oitem = Nothing
Unload Me

End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top