Send email without displaying [duplicate] - vba

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
How can I send an email through an account using MS Access VBA? I know this question is vague but it's so hard to find the relevant information online that isn't outdated in some way.
EDIT: I don't mean to be rude to those who are answering, but I am using MS Access. I cannot write the actual code in Outlook VBA.

Add a reference to the Outlook object model in the Visual Basic editor. Then you can use the code below to send an email using outlook.
Sub sendOutlookEmail()
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

Here is email code I used in one of my databases. I just made variables for the person I wanted to send it to, CC, subject, and the body. Then you just use the DoCmd.SendObject command. I also set it to "True" after the body so you can edit the message before it automatically sends.
Public Function SendEmail2()
Dim varName As Variant
Dim varCC As Variant
Dim varSubject As Variant
Dim varBody As Variant
varName = "james#yahoo.com"
varCC = "billy#gmail.com, joe#yahoo.com"
'separate each email by a ','
varSubject = "Hello"
'Email subject
varBody = "Let's get ice cream this week"
'Body of the email
DoCmd.SendObject , , , varName, varCC, , varSubject, varBody, True, False
'Send email command. The True after "varBody" allows user to edit email before sending.
'The False at the end will not send it as a Template File
End Function

Related

How do I use a field values on an access form to populate items of email using VBA and gmail? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 days ago.
Improve this question
I have the correct VBA to send email using gmail in an access database using a command button. Is there a way to populate the .To using a field on a form? I'd like to use fields on the form in the body of the message also. Is this possible?
I tried inserting the fields as I did using Outlook, but nothing worked.
Dim NewMail As CDO.Message
Dim mailConfig As CDO.Configuration
Dim fields As Variant
Dim msConfigURL As String
On Error GoTo Err:
Set NewMail = New CDO.Message
Set mailConfig = New CDO.Configuration
' load all default configurations
mailConfig.Load -1
Set fields = mailConfig.fields
'Set All Email Properties
With NewMail
.Sender = "xxxx#gmail.com"
.From = "xxxx"
.To = Left([emailadd], InStr([emailadd], "#"))
.CC = ""
.BCC = ""
.Subject = "Demo Spreadsheet Attached"
.Textbody = "Let me know if you have questions about the attached spreadsheet!"
End With

Access VBA code to import emails into table [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 months ago.
Improve this question
I have inherited a database which has VBA code, unfortunately the colleague has left the organisation and we need to make 4 amendments. 1 - The code works on your personal inbox however we have moved to a team mailbox, so can anyone assist with how to change the code to address this? 2 - We need to pull the senders email address currently it pulls the persons name on occasion it will identify an email but that is very limited (is it to do with the SMTP address?) 3 - we would like to put a date range for the pulling of emails. 4- Once it has imported the emails can it move them to a folder called imported.
Thanks
Sub ImportMailPropFromOutlook()
' Code for specifing top level folder and initializing routine.
' Set up Outlook objects.
Dim ol As New Outlook.Application
Dim olns As Outlook.Namespace
Dim ofO As Outlook.MAPIFolder
Dim ofSubO As Outlook.MAPIFolder
Dim objItems As Outlook.Items
Set olns = ol.GetNamespace("MAPI")
Set ofO = olns.GetDefaultFolder(olFolderInbox) '--- Specifies top level folder for importing Oultook mail.
'Set of = olns.PickFolder '--- Allows user to select top level folder for importing Outlook mail.
'Set info and call GetMailProp code.
Set objItems = ofO.Items
GetMailProp objItems, ofO
'Set info and call ProcessSubFolders.
'For Each ofSubO In of.Folders
' Set objItems = ofSubO.Items
' ProcessSubFolders objItems, ofSubO
'Next
End Sub
Sub GetMailProp(objProp As Outlook.Items, ofProp As Outlook.MAPIFolder)
' Code for writeing Outlook mail properties to Access.
' Set up DAO objects (uses existing Access "Email" table).
Dim rst As DAO.Recordset
Set rst = CurrentDb.OpenRecordset("Email")
'Set Up Outlook objects.
Dim cMail As Outlook.MailItem
Dim cAtch As Outlook.Attachments
'Write Outlook mail properties to Access "Email" table.
iNumMessages = objProp.Count
If iNumMessages <> 0 Then
For i = 1 To iNumMessages
If TypeName(objProp(i)) = "MailItem" Then
Set cMail = objProp(i)
'If ([rst]![EmailLocation] <> ofProp.Name) And ([rst]![EntryID] <> cMail.EntryID) Then
rst.AddNew
rst!EntryID = cMail.EntryID
rst!ConversationID = cMail.ConversationID
rst!Sender = cMail.Sender
rst!SenderName = cMail.SenderName
rst!SentOn = cMail.SentOn
rst!To = cMail.To
rst!CC = cMail.CC
rst!BCC = cMail.BCC
rst!Subject = cMail.Subject
Set cAtch = cMail.Attachments
cntAtch = cAtch.Count
If cntAtch > 0 Then
For j = cntAtch To 1 Step -1
strAtch = cAtch.Item(j).FileName
rst!Attachments = strAtch
Next
Else
rst!Attachments = "No Attachments"
End If
'rst!Count = cMail.Attachments.Count
rst!Body = cMail.Body
rst!HTMLBody = cMail.HTMLBody
rst!Importance = cMail.Importance
rst!Size = cMail.Size
rst!CreationTime = cMail.CreationTime
rst!ReceivedTime = cMail.ReceivedTime
rst!ExpiryTime = cMail.ExpiryTime
'rst!EmailLocation = ofProp.Name
rst.Update
'End If
End If
Next i
End If
End Sub
Sub ProcessSubFolders(objItemsR As Outlook.Items, OfR As Outlook.MAPIFolder)
'Code for processing subfolders
' Set up Outlook objects.
Dim ofSubR As Outlook.MAPIFolder
'Set info and call GetMailProp code.
GetMailProp objItemsR, OfR
'Set info and call ProcessSubFolders. Recursive.
For Each ofSubR In OfR.Folders
Set objItemsR = ofSubR.Items
ProcessSubFolders objItemsR, ofSubR
Next
End Sub
We need to pull the senders email address currently it pulls the persons name on occasion it will identify an email but that is very limited (is it to do with the SMTP address?)
In the code you are getting the Sender property, but it is not a scalar property. it returns an instance of the an AddressEntry object that corresponds to the user of the account from which the MailItem is sent. Instead, you need to use the Address property of the AddressEntry class to get a string representing the email address.
In case of Exchange accounts you may use the AddressEntry.GetExchangeUser method which returns an ExchangeUser object that represents the AddressEntry if the AddressEntry belongs to an Exchange AddressList object such as the Global Address List (GAL) and corresponds to an Exchange user. Then you may get the ExchangeUser.PrimarySmtpAddress property value which is a string representing the primary Simple Mail Transfer Protocol (SMTP) address for the ExchangeUser. Returns an empty string if this property has not been implemented or does not exist for the ExchangeUser object.
In cases when you need to convert Ex-like addresses to SMTP ones you may find the HowTo: Convert Exchange-based email address into SMTP email address article helpful.
Once it has imported the emails can it move them to a folder called imported.
Use the Move method available for all Outlook items.

I am getting a run time error '429'. Trying to get Excel macro to send a email [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
Sub SendEmail()
' SendEmail Macro
'
Dim olApp As Outlook.Application
Set olApp = CreateObject("Outlook.Application")
Dim olMail As Outlook.MailItem
Set olMail = olApp.CreateItem(olMailItem)
olMail.To = ""
olMail.CC = ""
olMail.Subject = " There is a change to the EMR adjustment Form that Approval"
olMail.Send
End Sub
You are mixing up early and late binding,
Late binding uses either the Visual Basic GetObject function or the CreateObject function to initialize Outlook
Example
Dim olApp as Object
Set olApp = CreateObject("Outlook.Application")
To use early binding, you first need to set a reference to the Outlook object library, Microsoft Outlook xx.x Object Library
Example
Dim olApp as Outlook.Application
Set olApp = New Outlook.Application

How can I change this code so that it task scheduler can run it as a .vbs file? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I have some vba code that sends an email with an attachment. it currently exists as a vba project in excel, but I would like to be able to save it as a vbs script so that I can fire it off every night with task scheduler. It only works in the project module I assume because I have to add a reference to an outlook library. If I save the script in notepad as a .vbs, it doesn't run.
Option Explicit
Sub SendBasicEmail()
Dim olApp As Outlook.Application
Dim olEmail As Outlook.MailItem
Set olApp = New Outlook.Application
Set olEmail = olApp.CreateItem(olMailItem)
With olEmail
.Display
.Attachments.Add "FileDirectory"
.To = "my email"
.Subject = "Subject"
.Send
End With
End Sub
I don't know if you can use it from a service but if you save this code into a text file with .vbs extension, this code will do the same as your Excel VBA version:
Option Explicit
Const olMailItem = 0
Sub SendBasicEmail()
Dim olApp: Set olApp = CreateObject("Outlook.Application")
Dim olEmail: Set olEmail = olApp.CreateItem(olMailItem)
With olEmail
.Display
.Attachments.Add "FileDirectory"
.To = "my email"
.Subject = "Subject"
.Send
End With
End Sub
SendBasicEmail
The main differences:
You cannot reference the Outlook library statically, so you have to use CreateObject
Since missing the library, you have to look up the values of constants (e.g. olMailItem)
You cannot declare your variables as of certain type, you can give them a name only and they will be all Variants.
You have to call this Sub directly, e.g. at the end of the file rather than from a button's event handler
Outlook, or any other Office app, cannot be used in a service (such as the Scheduler).

MS Access VBA: Sending an email through Outlook [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
How can I send an email through an account using MS Access VBA? I know this question is vague but it's so hard to find the relevant information online that isn't outdated in some way.
EDIT: I don't mean to be rude to those who are answering, but I am using MS Access. I cannot write the actual code in Outlook VBA.
Add a reference to the Outlook object model in the Visual Basic editor. Then you can use the code below to send an email using outlook.
Sub sendOutlookEmail()
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
Here is email code I used in one of my databases. I just made variables for the person I wanted to send it to, CC, subject, and the body. Then you just use the DoCmd.SendObject command. I also set it to "True" after the body so you can edit the message before it automatically sends.
Public Function SendEmail2()
Dim varName As Variant
Dim varCC As Variant
Dim varSubject As Variant
Dim varBody As Variant
varName = "james#yahoo.com"
varCC = "billy#gmail.com, joe#yahoo.com"
'separate each email by a ','
varSubject = "Hello"
'Email subject
varBody = "Let's get ice cream this week"
'Body of the email
DoCmd.SendObject , , , varName, varCC, , varSubject, varBody, True, False
'Send email command. The True after "varBody" allows user to edit email before sending.
'The False at the end will not send it as a Template File
End Function