Stars
asked on
Automated Email within VB
I am urgently looking for code that is able to send automatic email in VB6 when certain conditions are met, example is where stock level drops below a minimum quantity. I want it to happen automatic without the users knowing it.
Thanks
Thanks
Alternatively, if you are using Outlook as your e-mail program, Create a Reference to Outlook 98 Library, and use the following:
This button event simply sets the recipient and calls the sub. Obviously for your use you would be testing the stock level (on a timer say) and then calling the sub.
Private Sub Command1_Click()
Dim strTo() As String
Dim strFile() As String
'For the purpose of this I have set the recipient tojust one address and no file attachments, ccs or bccs
ReDim strTo(0)
ReDim strFile(0)
strTo(0) = "email@email.com" 'set this to who you want the e-mail recipient to be
Call SendMessage("Subject", "", "", strTo(), "Body", strFile(), True)
End Sub
Sub SendMessage(strSubject As String, strCopyTo As String, strBCCTo As String, _
strRecipient() As String, strBody As String, strFile() As String, _
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 intCounter As Integer
'Start Outlook
Set objOutlook = CreateObject("Outlook.Appl ication")
'This is the message section
Set objOutlookMsg = objOutlook.CreateItem(olMa ilItem)
With objOutlookMsg
For intCounter = 0 To UBound(strRecipient)
If strRecipient(intCounter) <> "" Then
' Add the To recipient(s) to the message.
Set objOutlookRecip = .Recipients.Add(strRecipie nt(intCoun ter))
objOutlookRecip.Type = olTo
End If
Next intCounter
' Add the CC recipient(s) to the message.
If strCopyTo <> "" Then
Set objOutlookRecip = .Recipients.Add(strCopyTo)
objOutlookRecip.Type = olCC
End If
' Add the BCC recipient(s) to the message.
If strBCCTo <> "" Then
Set objOutlookRecip = .Recipients.Add(strBCCTo)
objOutlookRecip.Type = olBCC
End If
' Set the Subject, Body, and Importance of the message.
.Subject = strSubject
.Body = strBody & vbCrLf & vbCrLf
.Importance = olImportanceNormal 'Normal importance
' Add attachments to the message.
For intCounter = 0 To UBound(strFile)
If Not IsMissing(AttachmentPath) Then
Set objOutlookAttach = .Attachments.Add(strFile(i ntCounter) )
End If
Next intCounter
' Resolve each Recipient's name.
For Each objOutlookRecip In .Recipients
objOutlookRecip.Resolve
Next
' Should we display the message before sending?
If DisplayMsg Then
.Display
Else
.Send
End If
End With
Set objOutlook = Nothing
End Sub
I would then have a PC which is running the code and is using outlook.
Hope this helps.
R
This button event simply sets the recipient and calls the sub. Obviously for your use you would be testing the stock level (on a timer say) and then calling the sub.
Private Sub Command1_Click()
Dim strTo() As String
Dim strFile() As String
'For the purpose of this I have set the recipient tojust one address and no file attachments, ccs or bccs
ReDim strTo(0)
ReDim strFile(0)
strTo(0) = "email@email.com" 'set this to who you want the e-mail recipient to be
Call SendMessage("Subject", "", "", strTo(), "Body", strFile(), True)
End Sub
Sub SendMessage(strSubject As String, strCopyTo As String, strBCCTo As String, _
strRecipient() As String, strBody As String, strFile() As String, _
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 intCounter As Integer
'Start Outlook
Set objOutlook = CreateObject("Outlook.Appl
'This is the message section
Set objOutlookMsg = objOutlook.CreateItem(olMa
With objOutlookMsg
For intCounter = 0 To UBound(strRecipient)
If strRecipient(intCounter) <> "" Then
' Add the To recipient(s) to the message.
Set objOutlookRecip = .Recipients.Add(strRecipie
objOutlookRecip.Type = olTo
End If
Next intCounter
' Add the CC recipient(s) to the message.
If strCopyTo <> "" Then
Set objOutlookRecip = .Recipients.Add(strCopyTo)
objOutlookRecip.Type = olCC
End If
' Add the BCC recipient(s) to the message.
If strBCCTo <> "" Then
Set objOutlookRecip = .Recipients.Add(strBCCTo)
objOutlookRecip.Type = olBCC
End If
' Set the Subject, Body, and Importance of the message.
.Subject = strSubject
.Body = strBody & vbCrLf & vbCrLf
.Importance = olImportanceNormal 'Normal importance
' Add attachments to the message.
For intCounter = 0 To UBound(strFile)
If Not IsMissing(AttachmentPath) Then
Set objOutlookAttach = .Attachments.Add(strFile(i
End If
Next intCounter
' Resolve each Recipient's name.
For Each objOutlookRecip In .Recipients
objOutlookRecip.Resolve
Next
' Should we display the message before sending?
If DisplayMsg Then
.Display
Else
.Send
End If
End With
Set objOutlook = Nothing
End Sub
I would then have a PC which is running the code and is using outlook.
Hope this helps.
R
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
You can use vbSendMail downloadable from http://www.freevbcode.com/ShowCode.Asp?ID=109
ASKER
What do I need to get the code to run. I am getting a Run-time error "424'."Object required" Which object is the code looking for.
if stock1.level < 50 then
'IF YOUR CONDTION ARE MET THEN SEND THE EMAIL, you must fill in the info
With SMTP
.Server = txtServer
.MailFrom = txtMailFrom
.SendTo = txtSendTo
.MessageSubject = txtMessageSubject
.MessageText = txtMessageText
.Connect
End With
end if