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

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

Related

Send email without displaying [duplicate]

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

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).

Edit, send and save email to file system

We currently have an email automatically created by Excel using VBA, with subject, recipient, message body with template text all filled in.
Sub CreateMail(Optional sFile As String = "")
'Create email to send to requestor with attachment sFile
'Declarations
Dim app As Outlook.Application
Dim msg As Outlook.MailItem
Dim send_to As Recipient
Dim send_tos As Recipients
'Initiations
Set app = CreateObject("Outlook.Application")
Set msg = app.CreateItem(olMailItem)
Set send_tos = msg.Recipients
Set send_to = send_tos.Add("receiver#email.com")
send_to.Type = 1
'Create message
With msg
.SentOnBehalfOfName = "sender#email.com"
.Subject = "This is the email subject"
.HTMLBody = "This is the email body" & vbCrLf
'Resolve each Recipient's name.
For Each send_to In msg.Recipients
send_to.Resolve
Next
If Len(sFile) > 0 Then
.Attachments.Add sFile
End If
.Display
End With
End sub
After making some manual changes to the email that is created, we'd like to send it and have a copy saved to a folder on the file system automatically (in addition to the usual sent folder in Outlook). Is there a way to do this all within Excel VBA?
I suspect it might be possible using Outlook VBA, however the folders are defined in Excel and we'd like to keep the code together in the one file.
What is your code for sending email? This works for me in an Excel VBA module:
Dim appOutLook As Outlook.Application
Dim MailOutLook As Outlook.MailItem
Set appOutLook = CreateObject("Outlook.Application")
Set MailOutLook = appOutLook.CreateItem(olMailItem)
With MailOutLook
.BodyFormat = olFormatRichText
.To = "email address"
.Subject = "Test"
.HTMLBody = "Test " & Now
.DeleteAfterSubmit = True 'to not retain in sent folder
.Display
.SaveAs "C:\filepath\Test.txt", 0
' .Send
End With
However, guess the real trick is allowing edit of the email before saving file. So far not seeing solution for that. Unfortunately the code execution does not pause while the message window is open. I was hoping for the pause since Office is supposed to be an integrated suite of apps - like opening a form in Access in dialog mode which does pause execution of code.
With code in Excel only, monitor the SentItems folder.
Utilizing Outlook Events From Excel
Confirm the mail from a unique ID.
The unique ID could be in the subject or body.
You could try saving the unique ID in PR_SEARCH_KEY. It is the same idea How, can get the exact sent Email from Sent Items folder? and How to uniquely identify an Outlook email as MailItem.EntryID changes when email is moved

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

Macro in Outlook for Automatic email [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 5 years ago.
Improve this question
I want to create an automatic mail response to the email which is currently active when I press a macro button.
Automatic email body should be read from an outside file - the format is .txt.
Aim: when I press a macro button it reads the contents of that .txt file and gives an auto reply to the active email.
Is this possible with a macro or VBA?
This procedure (along with the three ancillary functions below it) will take the currently open or selected email, and reply to it using the text inside whatever text file you specify in the filepath at the top.
Sub ReplyCurrentMsg()
' ************************
' change this to point to the file you
' want to put in the message body
' when running the macro
' ************************
Const TEXT_FILE_PATH As String = "C:\My Files\file_to_include.txt"
Dim obj As Object
Dim msg As Outlook.mailItem
Dim msgReply As Outlook.mailItem
Dim fileNum As Integer
Dim fileContents As String
Set obj = GetCurrentItem
If TypeName(obj) = "MailItem" Then
Set msg = obj
Set msgReply = msg.Reply
fileNum = FreeFile
' http://www.exceluser.com/explore/questions/vba_textcols.htm
Open TEXT_FILE_PATH For Input As #fileNum
fileContents = Input$(LOF(fileNum), 1)
Close #fileNum
With msgReply
.Body = fileContents
.Display ' or .Send
End With
End If
End Sub
Function GetCurrentItem() As Object
Select Case True
Case IsExplorer(Application.ActiveWindow)
Set GetCurrentItem = ActiveExplorer.Selection.item(1)
Case IsInspector(Application.ActiveWindow)
Set GetCurrentItem = ActiveInspector.CurrentItem
End Select
End Function
Function IsExplorer(itm As Object) As Boolean
IsExplorer = (TypeName(itm) = "Explorer")
End Function
Function IsInspector(itm As Object) As Boolean
IsInspector = (TypeName(itm) = "Inspector")
End Function
Just so I understand you what it to iterate through all active/enable email then send out content in a text file or the text file as an attachment.
Here is a link to show how to read text files.
Hey, take look at the link VBA read text files
see if this helps.