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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to send Mail From VB With FAQ222-179

Status
Not open for further replies.

BradB

MIS
Jun 21, 2001
237
0
0
US
VB 6 sp5

References
Microsoft Outlook 10.0 Object Library
Microsoft Outlook View Control

Error Message "Argument not optional" Appears and highlights the "call SendMail" for the error

I'm trying to send an email using VB, but with no success.

I cut and pasted the code into my app.

Private Sub cmdSend_Click()
Call SendMail
End Sub
Public Sub SendMail(tSubject As String, tBody As String)

'***********************
'Description: Uses the outlook object to create and send a mail using the passed parameters of tsubject,tbody and tTo
'***********************

Dim oItem As Outlook.MailItem
Dim oitems As Items
Set oOutlook = New Outlook.Application

Set oitems = objInbox.Items

Set oItem = oOutlook.CreateItem(olMailItem)
'
With oItem

tto "Address@juno.com"

tBody = "Testing 1...2...3..."

tSubject = "Test"

.To = tto
.Body = tBody
.Subject = tSubject
.send

End With
 
Oooh!

the follwoing correct must be made.

Change
tto "Address@juno.com"

To
tto = "Address@juno.com"

Still, the same error message appears.
 
Looks like the Call SendMail is looking for the the subject and body to be passed to it as parameters...but for some reason they actually get reset in the SendMail function. Two fixes will work depending on what you want.

1) Erase the code that receives the variables when defining the SendMail function. You will then need to Dim the variables inside your function. It will now look like:

Code:
Public Sub SendMail()
'***********************
'Description: Uses the outlook object to create and send a mail using the passed parameters of tsubject,tbody and tTo
'***********************
Dim tSubject, tBody As String

2) Keep receiving the variables, but send the values to the function that you want. Also change the part of the function that sets the values. Your whole code will look like:

Code:
Private Sub cmdSend_Click()
Call SendMail("This is the subject", "This is the body")
End Sub

Public Sub SendMail(tSubject As String, tBody As String)

'***********************
'Description: Uses the outlook object to create and send a mail using the passed parameters of tsubject,tbody and tTo
'***********************

Dim oItem As Outlook.MailItem
Dim oitems As Items
Set oOutlook = New Outlook.Application

Set oitems = objInbox.Items

Set oItem = oOutlook.CreateItem(olMailItem)
'
With oItem

tto = "Address@juno.com"

.To = tto
.Body = tBody
.Subject = tSubject
.send

End With

Hope that helps!
 
The FAQ doesn't specifiyWhat to do with the varibale "tto". I added it to the Sendmail varible list.

Also, this might be petty, but after the end with...an End Sub needs to be added.

New error message after applying vanvb's suggestion number 2.

Error Message:
Runtime error '424'
Object not found

The error messahe appears on the following line:

==>> Set oitems = objInbox.Items

Here's the new code:

Private Sub cmdSend_Click()
Call SendMail("Address@juno.com", "Test", "Test")
End Sub
Public Sub SendMail(tTo As String, tSubject As String, tBody As String)

'***********************
'Description: Uses the outlook object to create and send a mail using the passed parameters of tsubject,tbody and tTo
'***********************

Dim oItem As Outlook.MailItem
Dim oitems As Items
Set oOutlook = New Outlook.Application

Set oitems = objInbox.Items

Set oItem = oOutlook.CreateItem(olMailItem)

With oItem

.To = tTo
.Body = tBody
.Subject = tSubject
.Send

End With
End Sub
 
I took out that line just to test and it worked fine. Try that.
 
Vanvb,

Could you post the exact code you got to work?

Also, maybe I'm not referencing or have the right components added to make it work.
 
This exact code worked for me after adding a reference to the Microsoft Office Outlook 10 Library.

Code:
Private Sub cmdSend_Click()
Call SendMail("address@whatever.com", "Test", "Test")
End Sub
Public Sub SendMail(tTo As String, tSubject As String, tBody As String)

'***********************
'Description: Uses the outlook object to create and send a mail using the passed parameters of tsubject,tbody and tTo
'***********************

Dim oItem As Outlook.MailItem
Dim oitems As Items
Set oOutlook = New Outlook.Application
Set oItem = oOutlook.CreateItem(olMailItem)

With oItem

.To = tTo
.Body = tBody
.Subject = tSubject
.Send

End With
End Sub

Hope this does the same for you
 
I don't now what was wrongbefore , but starting over and pasting your code made it work.

Thanks!
 
BradB

Here is code I use to send information from a for with combo boxes text and memo fields.

Dim emailto As String
Dim messagetext As String
Dim subjecttext As String
'Declare variables
'
If vbYes = MsgBox("Are you ready to send your parts List?", vbYesNo, "SED Maintenance 2003") Then
'Check to see if user is ready to send
'
emailto = Me!Combo54
'
messagetext = "Lift Number: " & Me!Combo20 & Chr(13) & Chr(10) & Chr(13) & Chr(10) & "Manufacturer: " & Me!Manufacturer & Chr(13) & Chr(10) & Chr(13) & Chr(10) & "Lift Type: " & Me!TypeLift & Chr(13) & Chr(10) & Chr(13) & Chr(10) & "Model Number: " & Me!ModelNumber & Chr(13) & Chr(10) & Chr(13) & Chr(10) & "Parts List: " & Chr(13) & Chr(10) & Chr(13) & Chr(10) & Me!PartsNeeded & Chr(13) & Chr(10) & Chr(13) & Chr(10)
'this code creates a carriage return Chr(10) & Chr(13) & Chr
'(10)
subjecttext = "You have a Parts List from " & Me!Combo48 & "...." & Now()
'Sends the subjet as a formatted line with combo box
'contents and timestamp
DoCmd.SendObject , , , emailto, , , subjecttext, messagetext, False
MsgBox "List Sent to...." & Me!Combo54
Else

End If

Hope this might give you some ideas or help:)



Jimbo[bigsmile]
 
Jimbo62,

FYI, you can also use 'vbcrlf' to replace your carriage return characters.

A little tip to pass on.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top