I am trying to automate a manual process, using a VBA script in Excel. I have a spreadsheet which contains 4 list of: email addresses, subject, message, signature. I am new to VBA and want to press a command button which will automatically create outlook emails for the lists.
In my example below I have a list of 3 people I am trying to send an email too by clicking button 3, I prefer too have these emails sat in my drafts and not automatically sent as they need to be checked beforehand.
below is my code
Public Sub SendOutlookEmails()
'Microsoft Outlook XX.X Object Library is required to run this code
'Variable declaration
Dim objOutlook As Outlook.Application
Dim objMail As Outlook.MailItem
Dim lCounter As Long
'Set objects
Set objOutlook = Outlook.Application
'Read details from Excel sheet and send emails
For lCounter = 6 To 8
'Create a new email item
Set objMail = objOutlook.CreateItem(olMailItem)
'To
objMail.To = Sheet1.Range("A" & lCounter).Value
'Subject
objMail.Subject = Sheet1.Range("C" & lCounter).Value
'Email Body
objMail.Body = Sheet1.Range("D" & lCounter).Value
'Send email
'objMail.Send
'Close the object
'Set objMail = Nothing
End Sub
any help or working examples would be really helpful. Thanks
User D, I'd say use a With statement first for speed and readability. Then, there's a Save method that will save as Draft in the current NameSpace (your default email accounts' Draft folder - as you might have several email accounts on your Client, and are each considered their own NameSpace). If you use .Send method, it will send likewise if you use .Display method, it will not send, but popup on your screen (and will appear as another Task in Taskmgr).
Sub DraftEmails()
Dim oApp As New Outlook.Application
Dim oM As Outlook.MailItem
Set oM = oApp.CreateItem(olMailItem)
With oM
.To = "someone@somewhere.somedomain"
.Save
End With
End Sub
Also, to be more dynamic, I'd consider using a combination of UsedRange.Columns & the Find method. This would allow you to say, locate in Column A, the cell that has "to" in it, then can iterate down from there. So if you adjusted your columns, let's say, this would catch it and your code wouldn't need adjusted.
Let us know if this answered what you need, or of any other guidance we can give.
Danny,
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.