The code below has worked for several years. I am using Office 2010 and have been for a couple of years and this code always worked until yesterday. I now get an error on the line "Set MyOutlook = New Outlook.Application" in red below and have no idea why. The error is Run-Time error 429. ActiveX componet can't create object.
Any ideas? The only change to my machine is the Office optional updates that were installed this week.
Thanks
Any ideas? The only change to my machine is the Office optional updates that were installed this week.
Thanks
Code:
Option Compare Database
Option Explicit
Public Function SendEMail()
Dim db As DAO.Database
Dim MailList As DAO.Recordset
Dim MyOutlook As Outlook.Application
Dim MyMail As Outlook.MailItem
Dim Subjectline As String
Dim BodyFile As String
Dim fso As FileSystemObject
Dim MyBody As TextStream
Dim MyNewBodyText As String
Dim MyBodyText As String
Set fso = New FileSystemObject
' First, we need to know the subject.
Subjectline$ = InputBox$("Please enter the subject line for this mailing.", _
"We Need A Subject Line!")
' If there's no subject, call it a day.
If Subjectline$ = "" Then
MsgBox "No subject line, no message." & vbNewLine & vbNewLine & _
"Quitting...", vbCritical, "E-Mail Merger"
Exit Function
End If
' Now we need to put something in our letter...
BodyFile$ = "C:\Users\Dominic\Documents\FALCON\Newsletter Stuff\body1.txt"
' Check to make sure the file exists...
If fso.FileExists(BodyFile$) = False Then
MsgBox "The body file isn't where you say it is. " & vbNewLine & vbNewLine & _
"Quitting...", vbCritical, "I Ain't Got No-Body!"
Exit Function
End If
' Since we got a file, we can open it up.
Set MyBody = fso.OpenTextFile(BodyFile, ForReading, False, TristateUseDefault)
' and read it into a variable.
MyBodyText = MyBody.ReadAll
' and close the file.
MyBody.Close
' Now, we open Outlook for our own device..
[COLOR=#EF2929]Set MyOutlook = New Outlook.Application[/color]
' Set up the database and query
Set db = CurrentDb()
Dim ParamStart As String
Dim ParamEnd As String
Dim Num1 As Integer
Dim Num2 As Integer
Dim sWhere As String
Dim strSQL As String
ParamStart = UCase(left(InputBox$("Start With Letter", "Start First Name Letter"), 1))
ParamEnd = UCase(left(InputBox$("End With Letter", "End First Name Letter"), 1))
' Get the Asc number for the letter
Num1 = Asc(ParamStart$)
Num2 = Asc(ParamEnd$)
sWhere = " AND Asc(Left([First],1)) BETWEEN " & Num1 & " AND " & Num2
strSQL = "SELECT [First] & "" "" & [Mid] & "" "" & [Last] AS Name, tblMaster.ADDRESS, [CITY] & "", "" & [ST] & "" "" & [Zip] AS Address2, " & _
"IIf(IsNull([Phone]),"" "",[Phone]) AS PHONE1, IIf(IsNull([Tour]),"" "",[Tour]) AS TOUR1, tblMaster.SIGN, " & _
"IIf(IsNull([FltStatus]),"" "",[FltStatus]) AS FltStatus1, IIf(IsNull([Platoon]),"" "",[Platoon]) AS Platoon1, " & _
"IIf([tblmaster].[PLATOONID]=20 Or [tblmaster].[ID]=485,""FREE"",[tblDuesYearsLKU].[DuesYears] & """") AS SWITCH, tblMaster.[E-Mail] AS Email " & _
"FROM tblFltStatusLKU RIGHT JOIN (tblStateLKU RIGHT JOIN (tblPlatoonLKU RIGHT JOIN ((tblMaster LEFT JOIN qryMaster_Label_PD1 ON " & _
"tblMaster.ID = qryMaster_Label_PD1.ID) LEFT JOIN tblDuesYearsLKU ON qryMaster_Label_PD1.MaxOfDuesID = tblDuesYearsLKU.DuesID) ON " & _
"tblPlatoonLKU.PlatoonID = tblMaster.PLATOONID) ON tblStateLKU.STCODEID = tblMaster.STCODEID) ON tblFltStatusLKU.StatusID = tblMaster.STATUSID " & _
"Where ((tblMaster.[E-Mail]) Is Not Null) AND ((tblMaster.STATUSTYPEID)=1) "
strSQL = strSQL & sWhere
Set MailList = db.OpenRecordset(strSQL)
Do Until MailList.EOF
' This creates the e-mail
Set MyMail = MyOutlook.CreateItem(olMailItem)
' This addresses it the e-mail
MyMail.To = MailList("EMail")
'This gives the e-mail a subject
MyMail.Subject = Subjectline$
'This provides the e-mail body
MyMail.Body = MyBodyText
' This line will copy the "master" template into a variable
MyNewBodyText = MyBodyText
' Now we can replace tokens to our heart's content
' without worrying about corrupting the "master" template
MyNewBodyText = Replace(MyBodyText, "[[Name]]", MailList("Name"))
MyNewBodyText = Replace(MyNewBodyText, "[[Address]]", MailList("Address"))
MyNewBodyText = Replace(MyNewBodyText, "[[Address2]]", MailList("Address2"))
MyNewBodyText = Replace(MyNewBodyText, "[[Phone1]]", MailList("Phone1"))
MyNewBodyText = Replace(MyNewBodyText, "[[Tour1]]", MailList("Tour1"))
MyNewBodyText = Replace(MyNewBodyText, "[[Platoon1]]", MailList("Platoon1"))
MyNewBodyText = Replace(MyNewBodyText, "[[SWITCH]]", MailList("SWITCH"))
MyMail.Body = MyNewBodyText
MyMail.Attachments.Add "C:\Users\Dominic\Documents\FALCON\Newsletter Stuff\ShortTimer.pdf", olByValue, 1, "Short Timer"
'This sends it!
MyMail.Send
'Some people have asked how to see the e-mail instead of automaticially sending it.
'Uncomment the next line And comment the "MyMail.Send" line above this.
'MyMail.Display
'And on to the next one...
MailList.MoveNext
Loop
'Cleanup after ourselves
Set MyMail = Nothing
'Uncomment the next line if you want Outlook to shut down when its done. Otherwise, it will stay running.
'MyOutlook.Quit
Set MyOutlook = Nothing
MailList.Close
Set MailList = Nothing
db.Close
Set db = Nothing
End Function