Im trying to send an email automatically with the push of a button. The only problem is that Outlook has to be running in order for the program to work. If outlook is not running, I get an error message (see mvberror.jpg below).
My code is;
Dim response
response = MsgBox("Do you want to send a message to Bev telling her you made a copy of " & lstfiles.FileName, vbYesNo, "STOP")
If response = vbYes Then 'User chose yes.
Dim objOutlook As New Outlook.Application
Dim ObjOutlookMsg As Outlook.MailItem
Set objOutlookMsg = objOutlook.CreateItem(olMailItem)
With objOutlookMsg
.To = "email address"
.Subject = "test"
.Body = "Bev, " & vbNewLine
.Body = .Body & "I made a copy of drawing number: ** " & lstfiles.FileName
.Body = .Body & " **, from the Bravo folder. " & vbNewLine & vbNewLine
.Body = .Body & "Thanks"
.Send
End With
Set objOutlookMsg = Nothing
Set objOutlook = Nothing
End If
Try this style of code, I know this works 'cos we're using it at the moment (ignore getcentrecode and getbatchnumber). This still will not send the email automatically, but this might just be because we're not connected to the web at the time.
One thing to look out for though, the Outlook reference you use in VB MUST be the same type and version as the client machines (i.e. Outlook 2000 references and Outlook 97 clients will not work, also Outlook Express will not work with anything)
Hope this helps
Chris
Dim OutLookApp As New Outlook.Application
Dim OutLookEmail As Outlook.MailItem
Set OutLookEmail = OutLookApp.CreateItem(olMailItem)
Dim Counter As Long
Dim Total As Long
cmdSendEmail.Enabled = False
If Trim(txtEmail.Text) <> "" Then
If Dir("c:\temp\C" & GetCentreCode & "B" & GetBatchNumber & ".mdb") <> "" Then
OutLookEmail.To = txtEmail.Text
OutLookEmail.Subject = "EMAIL INVOICING"
OutLookEmail.Attachments.Add "C:\temp\C" & GetCentreCode & "B" & GetBatchNumber & ".mdb"
OutLookEmail.Send
End If
End If
OutLookApp.Quit
cmdSendEmail.Enabled = True
Call MsgBox("Message sent!", vbInformation)
It is still returning an error when I dont have outlook running. When I debug, it goes to the same spot;(Set OutLookEmail = OutLookApp.CreateItem(olMailItem)). My references are correct, so....
Thanks for the reply though...
-el guapo
"The cheese is old and moldy, where's the bathroom?"