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

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

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

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

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

Running code on a click within an Excel spreadsheet, preferably via a button

I have never used macro's before or used Visual Basic for Applications. I need this as part of a checklist in Microsoft Excel 2013.
My Aim:
Once the checklist has been filled, I want the active worksheet to be attached to an email whilst auto-filling the email addresses and the subject which will be "Checklist_XXX" the XXX part for example will be amended depending on who is using the checklist, so it could be Checklist_12345.
Steps I have taken:
The checklist is complete, and I have created a ActiveX button which by default has no code.
I found the following code online which seems to be what I need:
Sub Mail_workbook_Outlook_1()
Dim OutApp As Object
Dim OutMail As Object
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
On Error Resume Next
With OutMail
.to = "myemail#gmail.com,myemail2#gmail.com"
.Subject = "Checklist_"
.Body = "I have attached my checklist related to change"
.Attachments.Add ActiveWorkbook.FullName
.Display
End With
On Error GoTo 0
Set OutMail = Nothing
Set OutApp = Nothing
End Sub
What I think this does:
I believe it will create an Outlook email with the attached message and will have myemail#gmail.com and myemail2#gmail.com as recipients with the subject and body amended. I changed it from .send to .display so I can review before I send.
The short question:
How do I run this code on a click within my Excel spreadsheet, preferably via a button?
To attach the code to the click of an ActiveX command button:
Click the button and select View Code.
You'll be taken to a screen with some code in it:
Private Sub CommandButton1_Click()
End Sub
Simply add your procedure name in there:
Private Sub CommandButton1_Click()
Mail_workbook_Outlook_1
End Sub
NB:
In may be worth your while to update the button name to something more meaningful than CommandButtonx.
Right-click, select properties and update the (name) field.

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