CreateObject doesn't work in Class Module (Visual Basic 6) - class

in a VB6 project, this code worked for sending an email via Outlook 2013 using the Microsoft Office Outlook 15.0 Object Library:
Private Sub Command1_Click()
Dim objOutlook As Outlook.Application
Set objOutlook = CreateObject("Outlook.Application")
Dim mail As Outlook.MailItem
Set mail = objOutlook.CreateItem(olMailItem)
mail.To = txt_Recipient
mail.Subject = txt_Subject
mail.body = txt_Inhalt.Text
mail.Send
Now when I tried to transfer the same code into a class module, it kept throwing an error 429 that it couldn't create the object in the third line. Any idea why it doesn't work in a class module while it does when being directly coded in the Command_Click event?
Code in the class:
Public objOutlook As outlook.Application
Public Sub MailSenden(ByVal empfaenger As String, ByVal betreff As String, ByVal inhalt As String)
Set objOutlook = CreateObject("Outlook.Application")
Dim mail As outlook.MailItem
Set mail = objOutlook.CreateItem(olMailItem)
mail.To = empfaenger
mail.Subject = betreff
mail.body = inhalt
mail.Send
End Sub
Thanks for any help you can give me!
Colin

I'm curious why you're using late binding when you have the Outlook 15.0 type library handy (and referenced in your project). Have you tried:
Set objOutlook = New Outlook.Application

Related

EMAIL wont Outlook Email using Access form

MS Access 2019 form supplies data to send an email via Outlook 2019.
The recipient (email address) will not populate the email "To" box unless it is text, but will not if it is an email. When 'proving the VBA data transfer via msgbox, it will. I suspect it could be my dim declaration type but I have tried others such as string and variant(illustrated)
The code below will allow the MsgBox to run, but nothing else.
My form fields are named" 'Name' txtRecip, and 'EMail' txtEmail
Any ideas?
Thanks in advance
Fred
Private Sub btnSendEmail_Click()
Dim objOutlook As Outlook.Application
Dim objOutlookMsg As Outlook.MailItem
Dim objOutlookRecip As Outlook.Recipient
Dim objOutlookAttach As Outlook.Attachment
Dim objEmail As Variant
' Create the Outlook session.
Set objOutlook = CreateObject("Outlook.Application") ' Create the message.
Set objOutlookMsg = objOutlook.CreateItem(olMailItem)
With objOutlookMsg
' Add the To recipient(s) to the message.
objEmail = [txtEmail].Value
response = MsgBox(objEmail)
Set objOutlookRecip = .Recipients.Add(objEmail)
objOutlookRecip.Type = olTo
'response = MsgBox(txtName & Chr(13) & txtEmail)
' Add the CC recipient(s) to the message.
' Set objOutlookRecip = .Recipients.Add("")
' objOutlookRecip.Type = olCC
' Set the Subject, Body, and Importance of the message.
.Subject = "B2B - Brethren Outreach"
.Body = txtName.Value & "," & Chr(13) & Chr(13) & "

MS Access 2010 - Send an automated email after form has been submitted

I've been looking at trying to get my Access database to automatically send an email to notify me of an addition once a user has entered a new record through a form. I've tried using:
Private Sub Form_AfterInsert()
Dim oApp As Outlook.Application
Dim oMail As MailItem
Set oApp = CreateObject("Outlook.application")
Set oMail = oApp.CreateItem(olMailItem)
oMail.Body = "Body of the email"
oMail.Subject = "Test Subject"
oMail.To = "Someone#somewhere.com"
oMail.Send
Set oMail = Nothing
Set oApp = Nothing
End Sub
But it just gives me the error: Compile error: User-defined type not defined.

Email will not send through VBS script

I need to send a very simple email with only text to a few recipients, but I'm getting an error.
I don't have an SMTP server to send emails through, but I do have an outlook and I'm logged in through the desktop app.
Here's the script so far:
Set objOutlook = CreateObject("Outlook.Application")
Set objMail = objOutlook.CreateItem(0)
objMail.Display
objMail.to = "recipient#whatever.com"
objMail.Subject = "Test"
objMail.Body = "test"
objMail.Send
objOutlook.Quit
Set objMail = Nothing
Set objOutlook = Nothing
When the script is run, WSH gives the error
line: 10
char: 1
error: Operation Aborted
source: (null)
This is the objMail.Send line.
And my outlook pops up with the proper recipient/subject/body, but it doesn't send.
I can't find anything related to this issue or a work around besides using an SMTP server which as far as I know I can't do.
I have a function defined and in daily use which accepts the various items for creating and sending the email. Remember if you have to create your Outlook instance, you need to log on with the appropriate mail profile in order to send anything. The profile we use here is just called "Outlook". Check what yours is called and include the Namespace stuff I have in mine.
Dim sComputer : sComputer = "." ' selects local machine
Dim oWMIService : Set oWMIService = GetObject("winmgmts:\\" & sComputer & "\root\cimv2")
Dim colItems : Set colItems = oWMIService.ExecQuery ("Select * from Win32_Process Where Name = 'outlook.exe'")
Dim oOutlook : Set oOutlook = CreateObject("Outlook.Application")
Dim oNamespace : Set oNamespace = oOutlook.GetNamespace("MAPI")
If colItems.Count = 0 Then
LOG_Write "Outlook isn't open, logging onto it..."
oNamespace.Logon "Outlook",,False,True ' name of Outlook profile
bOpenedOutlook = True
End If
Dim oFolder : Set oFolder = oNamespace.GetDefaultFolder(olFolderInbox)
oFolder.Display ' Make Outlook visible
Here is basic vbscript simple email
' For Example...
Email_List = "0m3r#Email.com;"
Set App = CreateObject("Outlook.Application")
Set Mail = App.CreateItem(0)
With Mail
.To = Email_List
.CC = ""
.BCC = ""
.Subject = "Hello World"
.HTMLBody = "Bla Bla!!!"
'.Body = strbody
'You can add a file like this
' .Attachments.Add (FilePath)
'use .Send (to send) or .Display (to display the email and edit before sending)
.Display
.send
End With
Set Mail = Nothing
Set App = Nothing
Save is as name.vbs

How to Send mail from visual basic 2010 professional

I want to send a mail using gmail id, my code is as here under, but an error occurred. How can I solve this?
Imports System.Net
Imports System.Net.Mail
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
MsgBox("This will send a mail...")
Try
Dim smtpserver As New SmtpClient()
Dim mail As New MailMessage()
smtpserver.Credentials = New Net.NetworkCredential("myname#gmail.com", "password")
smtpserver.Port = 465
smtpserver.Host = "smtp.gmail.com"
mail = New MailMessage()
mail.From = New MailAddress("myname#gmail.com")
mail.To.Add("to mail id")
mail.Subject = "Test by bharat"
mail.Body = "hello ooooooooooooooooooooooo"
smtpserver.Send(mail)
MsgBox("Mail Sent")
Catch ex As Exception
MsgBox(ex.ToString)
Close()
End Try
End Sub
End Class
Below is an example of sending an email with visual basic 10, First you need to build a form with fields to,from,text,and email_send button now the below code
Private Smtp_Server As New SmtpClient()
Private Sub email_send_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles email_send.Click
Try
Dim e_mail As New MailMessage()
Smtp_Server.Credentials = New Net.NetworkCredential("yener.turkeli#v-basic.net", "password")
Smtp_Server.Port = 25
Smtp_Server.Host = "mail.v-basic.net"
e_mail = New MailMessage()
e_mail.From = New MailAddress(from_txt.Text)
e_mail.To.Add(to_txt.Text)
e_mail.Subject = "VB.NET Email Sending"
e_mail.Body = text_txt.Text
Smtp_Server.Send(e_mail)
Catch error_t As Exception
MsgBox(error_t.ToString)
End Try
End Sub
You can make a new class called 'Mail'.
If you make a class, you don't have to type all of the code again if you want to send more than 1 email.
Imports System.Net.Mail
Public NotInheritable Class Mail
Public Property subject As String
Public Property body As String
Public Property receiver As String
Public Sub send()
Try
Dim smtpServer As New SmtpClient()
Dim mail As New MailMessage()
smtpServer.UseDefaultCredentials = False
smtpServer.Credentials = New Net.NetworkCredential("yener.turkeli#v-basic.net", "password"))
smtpServer.Port = 587
smtpServer.EnableSsl = True
smtpServer.Host = "smtp.gmail.com"
mail = New MailMessage()
mail.From = New MailAddress("yener.turkeli#v-basic.net")
mail.To.Add(receiver)
mail.Subject = subject
mail.Body = body
smtpServer.Send(mail)
Catch ex As Exception
MsgBox(ex.Message & vbNewLine & ex.StackTrace)
End Try
End Sub
End Class
And then you can make a new object in your code where and when you want to send the email:
Dim email as new Mail
email.receiver = "receiver#example.com"
email.subject = "Subject"
email.body = "Message"
email.send()
Imports System.Net.Mail
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Try
Dim Smtp_Server As New SmtpClient
Dim e_mail As New MailMessage()
Smtp_Server.UseDefaultCredentials = False
Smtp_Server.Credentials = New Net.NetworkCredential("myemail#gmail.com", "Password")
Smtp_Server.Port = 587
Smtp_Server.EnableSsl = True
Smtp_Server.Host = "smtp.gmail.com"
e_mail = New MailMessage()
e_mail.From = New MailAddress(TextBox1.Text)
e_mail.To.Add(TextBox2.Text)
e_mail.Subject = "Email Sending"
e_mail.IsBodyHtml = False
e_mail.Body = txtmsg.Text
Smtp_Server.Send(e_mail)
MsgBox("Mail Sent")
Catch error_t As Exception
MsgBox(error_t.ToString)
End Try
End Sub
End Class

ACCESS 2007 - Automatically Send and Email Using Outlook Upon a Specific Event

I am trying to create an App in Microsoft Access 2007. How can I silently send an email out using Outlook 2007 upon a specific event without any user interaction. How should I approach this. If you can provide some VBA some it would be extremely nice, but if not, could you guide me in how to accomplish this?
I was able to solve my problem with the following code:
Public Sub SendEmail()
Email_Bcc = "email#domain.com"
Email_Body = "Email body!!!!"
Email_Subject = "Email Subject"
On Error GoTo debugs
Set Mail_Object = CreateObject("Outlook.Application")
Set Mail_Single = Mail_Object.CreateItem(0)
With Mail_Single
.Subject = Email_Subject
.To = Email_Send_To
.cc = Email_Cc
.BCC = Email_Bcc
.Body = Email_Body
.send
End With
debugs:
If Err.Description <> "" Then MsgBox Err.Description
End Sub
First declare a couple variables in the event that you want to send the email, or in a function you'd like the event to call.
Public Started As Boolean
Public oApp As Outlook.Application
Public oItem As Outlook.MailItem
Next, open or get Outlook if it's running.
On Error Resume Next
'Get Outlook if it's running
Set oApp = GetObject(, "Outlook.Application")
If Err <> 0 Then
'Outlook wasn't running, start it from code
Set oApp = CreateObject("Outlook.Application")
Started = True
End If
Now, do what you've got to do with your email.
Set oItem = oApp.CreateItem(olMailItem)
With oItem
.To = "email#email.com"
.Subject = "Your email, sirrah."
.Body = "Please enjoy this complimentary email."
'Send the email
.Send
End With
Finally, close outlook if it wasn't running before and clean up.
Set oItem = Nothing
If Started Then
oApp.Quit
End If
You can do this "code-free" by using a macro and the "SendObject" action. Be sure to complete all the necessary arguments To, Subject, Message Text, and then set the Edit Message argument to 'No'.
You can then attach this macro to any event you wish, such as the OnClick event of a button.