DatCrazyMongoose
New Member
- Joined
- Dec 31, 2019
- Messages
- 3
- Office Version
- 365
- Platform
- Windows
This is my first post, but I've been coming here for Excel help for a while. This is such a great resource, and I appreciate it very much! Anyway, on to the business at hand... My first VBA script is an attempt to send reminder emails based on cells in a sheet and content from a document. It works, but I know it's not elegant, and I'm afraid I'll have unforeseen problems with errors. I don't know anything about error-checking, so that's what I'd like some advice on as it pertains to this script. Any and all input is appreciated. Thanks!
VBA Code:
Sub Send_Reminder_Emails()
'Sends emails using Excel data for "Subject" and "To", and Word document for "Body"
'Establish variables
Dim olApp As Object
Dim olEmail As Object
Dim wd As Object
Dim editor As Object
Dim doc As Object
Dim row_number As Integer
row_number = 2
'Open Outlook, Word, and document
Set olApp = CreateObject("Outlook.Application")
Set wd = CreateObject("Word.Application")
Set doc = wd.Documents.Open("P:\BDST\Sales Account Listing\Review Dates\Templates\45Days.docx")
'Start loop if cell "A'row_number'" is not empty
Do While ActiveSheet.Range("A" & row_number) > ""
DoEvents
'Copy contents of Word document
doc.Content.Copy
'Create, fill, and send email object
Set olEmail = olApp.CreateItem(0)
With olEmail
.Display
.To = ActiveSheet.Range("B" & row_number)
.Subject = ActiveSheet.Range("A" & row_number)
Set editor = .GetInspector.WordEditor
editor.Content.Paste
.Send
End With
row_number = row_number + 1
Loop
'Clean up
Set editor = Nothing
doc.Close
wd.Quit
Set doc = Nothing
Set wd = Nothing
Set olEmail = Nothing
Set olApp = Nothing
End Sub