Please note, that if you do not use named arguments, you need to fill in the arguments in the same order as they are set in the macro. There are two ways to insert the required arguments.Įxample without named arguments: Call SendEmail(" ", "Email subject", "Email text")Įxample with named arguments: Call SendEmail(recipient:=" ", subject:="Email subject", body:="Email text", enable_html:=True) The above macro, can be called from any script. 'Set the credentials of your Gmail account Password = "your password" 'Insert your Gmail password or app password (if you have 2-factor authentication enabled) Sub SendEmail(recipient As String, subject As String, body As String, Optional enable_html As Boolean = False, Optional cc As String = "", Optional bcc As String = "", Optional attachment As String = "") For example, you can create a sub/macro which takes email, subject, and text as arguments and automatically sends an email.īelow is an example of such macro. The code can easily be changed, to make it more dynamic. With the above VBA code, you should be able to create your own macros to send emails. You have now generated an app password that you can use in your macro.Select app “Mail” and select device “Windows Computer” and click the GENERATE button.Under the Signing in to Google tab, click on App passwords.Luckily, this is a fairly simple process. If you have 2-factor authentication enabled on your Gmail account, you need to create an app password to use instead of your normal password. The code is tested to work on “English (United States)” and “English (United Kingdom)”, but others might work as well. Some regions in Windows, will create conflicts due to the encoding. If you want to enable HTML in the body text of the email, you need to set the enable_html variable to True: enable_html = True Windows Region Example: attachment = "C:\Documents\filename.xls" If you want to add an attachment to the email, you just need to add the local path to the attachment variable. Password or app password for your email account (see instructions below)īesides this, there are some optional fields:.You need to fill in the following information: MsgBox "An error ocurred." & vbNewLine & Err.Number & ": " & Err.Description MsgBox "Error: check your credentials." & vbNewLine & Err.Number & ": " & Err.Description MsgBox "Error: check your internet connection." & vbNewLine & Err.Number & ": " & Err.Description Item(msConfigURL & "/sendpassword") = password Item(msConfigURL & "/sendusername") = from Item(msConfigURL & "/smtpserverport") = 465 Item(msConfigURL & "/smtpauthenticate") = 1 Item(msConfigURL & "/smtpusessl") = True Set configObj = CreateObject("CDO.Configuration") Set mailObj = CreateObject("CDO.Message") Password = "your password" 'Insert your Gmail password or App password (if you have 2-factor authentication enabled)īody = "Body text" 'The body text of the emailĪttachment = "" 'Local path to file you want to attach to the email (optional)Įnable_html = False 'Set to True if you want to add HTML to the body text of the email (optional)ĭim mailObj, configObj As Object, fields As Variant, msConfigURL As String Recipient = " " 'Insert recipient emailĬc = "" 'Insert CC email recipient (optional)īcc = "" 'Insert BCC email recipient (optional) The configuration is available at the beginning of the macro, right after the following line: 'CONFIGURATION - EDIT THISįull VBA macro 'Macro to send emails using Gmailĭim from, recipient, cc, bcc, password, subject, body, attachment As String, enable_html As Booleanįrom = " " 'Insert your own email I have made the macro very easy to use, just change the configuration and you are good to go. This is the complete script that you need to start sending emails via your Gmail account. 4.1 How to password protect a VBA project VBA script: send emails using Gmail
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |