Option Compare Database
Option Explicit
Private Enum InitialWindowState
wsMaximized = 0
wsMinimized = 1
wsNormalWindow = 2
End Enum
Public Sub SendNotices()
Dim OutlookApp As Object
'Dim OutlookApp As Outlook.Application
Dim rs As DAO.Recordset
Dim EmailTo As String
Dim EmailBody As String
Dim EmailSignature As String
Dim EmailSubject As String
Dim MovedToNext As Boolean
Const EMAIL_ITEM = 0
'Error handler currently turned off for debugging.
'On Error GoTo ErrHandler
Set rs = CurrentDb.OpenRecordset("QRYFirstEmail2")
'This loop is built assuming that you want one email sent per email address.
'If you want one email per record, instead, then this needs to be re-worked.
If Not rs.EOF Then
Set OutlookApp = GetOutlook(wsMinimized)
If Not OutlookApp Is Nothing Then
EmailSubject = "Module Lists to Complete"
Do
EmailTo = rs!Emailaddress
EmailBody = BuildBody(rs, EmailTo) 'NOTE: MoveNext IS PERFORMED HERE!!!
Call SendEmail(EmailTo, EmailBody, EmailSubject, EmailSignature, OutlookApp)
'There is no .MoveNext because that happens as part of BuildBody above.
Loop Until rs.EOF
MsgBox "All new emails have been sent", vbInformation, "Thank You"
End If
End If
ProcExit:
If Not rs Is Nothing Then
rs.Close
Set rs = Nothing
End If
Exit Sub
ErrHandler:
Beep
MsgBox "Error Number:" & vbTab & Err.Number & vbCrLf & vbCrLf & _
"Error Description:" & vbCrLf & Err.Description
Resume ProcExit
End Sub
Private Function BuildBody(ByRef rs As DAO.Recordset, _
ByVal EmailTo As String) As String
'Are you sure this format is what you want? It's going to look something like this:
'**********************************************************************************
'*** ModuleCode: ***
'*** For x = 1 To 23 ***
'*** DoCmd.stuff ***
'*** DoCmd.morestuff ***
'*** Call SomeRandomSub ***
'*** CurrentDb.Execute "SomeUpdateQuery", dbSeeChanges + dbFailOnError ***
'*** Next x - ModuleTitle: SomeModuleName ***
'*** ModuleCode: ***
'*** Do ***
'*** DoCmd.evenmorestuff ***
'*** DoCmd.randomstuff ***
'*** Call SomeRandomSub ***
'*** CurrentDb.Execute "SomeUpdateQuery", dbSeeChanges + dbFailOnError ***
'*** Loop Until SomeLogicalTest - ModuleTitle: SomeModuleName ***
'**********************************************************************************
Do Until rs!Emailaddress <> EmailTo
BuildBody = "Module Code: " & rs!ModuleCode & "Module Title: " & rs!ModuleTitle & vbCrLf
rs.MoveNext
If rs.EOF Then Exit Do
Loop
End Function
Private Function GetOutlook(Optional WindowState As InitialWindowState = wsMinimized) As Object
'Dim O As Outlook.Application
Dim O As Object
Const FOLDER_INBOX = 6
On Error GoTo ErrHandler
Set O = GetObject(, "Outlook.Application")
If O.Explorers.Count = 0 Then
InitOutlook:
O.Session.GetDefaultFolder(FOLDER_INBOX).Display
O.ActiveExplorer.WindowState = WindowState
End If
Set GetOutlook = O
ProcExit:
Set O = Nothing
Exit Function
ErrHandler:
Select Case Err.Number
Case -2147352567
'User cancelled setup, silently exit
Set O = Nothing
Case 429, 462
Set O = GetOutlookApp()
If O Is Nothing Then
Err.Raise 429, "OutlookApp", "Outlook Application does not appear to be installed."
Else
Resume InitOutlook
End If
Case Else
Beep
MsgBox "Error Number:" & vbTab & Err.Number & vbCrLf & vbCrLf & _
"Error Description:" & vbCrLf & Err.Description
End Select
Resume ProcExit
End Function
Private Function GetOutlookApp() As Object
On Error GoTo ErrHandler
Set GetOutlookApp = CreateObject("Outlook.Application")
ProcExit:
Exit Function
ErrHandler:
Set GetOutlookApp = Nothing
Resume ProcExit
End Function
Private Sub SendEmail(ByVal EmailTo As String, _
ByVal EmailBody As String, _
ByRef EmailSubject As String, _
ByVal EmailSignature As String, _
ByRef OutlookApp As Object)
'EmailSubject is ByRef so it can be permanently modified.
'Dim EmailItem As Outlook.MailItem
Dim EmailItem As Object
Const EMAIL_ITEM = 0
Set EmailItem = OutlookApp.CreateItem(EMAIL_ITEM)
If EmailSignature = "" Then EmailSignature = EmailItem.Body
With EmailItem
.To = EmailTo
.Subject = EmailSubject
.Body = EmailBody & vbCrLf & EmailSignature
.Send
End With
End Sub
Call SendNotices()
Debug.Print EmailSignature
Option Compare Database
Option Explicit
Private Enum InitialWindowState
wsMaximized = 0
wsMinimized = 1
wsNormalWindow = 2
End Enum
Public Sub SendNotices()
Dim OutlookApp As Object
[COLOR=darkgreen]'Dim OutlookApp As Outlook.Application
[/COLOR]Dim rs As DAO.Recordset
Dim EmailTo As String
Dim EmailBody As String
Dim EmailSignature As String
Dim EmailSubject As String
Dim MovedToNext As Boolean
Const EMAIL_ITEM = 0
On Error GoTo ErrHandler
Set rs = CurrentDb.OpenRecordset("QRYFirstEmail2")
[COLOR=darkgreen]'This loop is built assuming that you want one email sent per email address.
'If you want one email per record, instead, then this needs to be re-worked.[/COLOR]
If Not rs.EOF Then
Set OutlookApp = GetOutlook(wsMinimized)
If Not OutlookApp Is Nothing Then
EmailSubject = "Module Lists to Complete"
Do
EmailTo = rs!Emailaddress
EmailBody = BuildBody(rs, EmailTo) 'NOTE: MoveNext IS PERFORMED HERE!!!
Call SendEmail(EmailTo, EmailBody, EmailSubject, EmailSignature, OutlookApp)
[COLOR=darkgreen]'There is no .MoveNext because that happens as part of BuildBody above.[/COLOR]
Loop Until rs.EOF
MsgBox "All new emails have been sent", vbInformation, "Thank You"
End If
End If
ProcExit:
If Not rs Is Nothing Then
rs.Close
Set rs = Nothing
End If
Exit Sub
ErrHandler:
Beep
MsgBox "Error Number:" & vbTab & Err.Number & vbCrLf & vbCrLf & _
"Error Description:" & vbCrLf & Err.Description
Resume ProcExit
End Sub
Private Function BuildBody(ByRef rs As DAO.Recordset, _
ByVal EmailTo As String) As String
[COLOR=darkgreen]'Are you sure this format is what you want? It's going to look something like this:
'**********************************************************************************
'*** ModuleCode: ***
'*** For x = 1 To 23 ***
'*** DoCmd.stuff ***
'*** DoCmd.morestuff ***
'*** Call SomeRandomSub ***
'*** CurrentDb.Execute "SomeUpdateQuery", dbSeeChanges + dbFailOnError ***
'*** Next x - ModuleTitle: SomeModuleName ***
'*** ModuleCode: ***
'*** Do ***
'*** DoCmd.evenmorestuff ***
'*** DoCmd.randomstuff ***
'*** Call SomeRandomSub ***
'*** CurrentDb.Execute "SomeUpdateQuery", dbSeeChanges + dbFailOnError ***
'*** Loop Until SomeLogicalTest - ModuleTitle: SomeModuleName ***
'**********************************************************************************
[/COLOR] Do Until rs!Emailaddress <> EmailTo
BuildBody = BuildBody & "Module Code: " & vbCrLf & rs!ModuleCode & " - " & "Module Title: " & rs!ModuleTitle & vbCrLf
rs.MoveNext
If rs.EOF Then Exit Do
Loop
End Function
Private Function GetOutlook(Optional WindowState As InitialWindowState = wsMinimized) As Object
[COLOR=darkgreen]'Dim O As Outlook.Application
[/COLOR]Dim O As Object
Const FOLDER_INBOX = 6
On Error GoTo ErrHandler
Set O = GetObject(, "Outlook.Application")
If O.Explorers.Count = 0 Then
InitOutlook:
O.Session.GetDefaultFolder(FOLDER_INBOX).Display
O.ActiveExplorer.WindowState = WindowState
End If
Set GetOutlook = O
ProcExit:
Set O = Nothing
Exit Function
ErrHandler:
Select Case Err.Number
Case -2147352567
'User cancelled setup, silently exit
Set O = Nothing
Case 429, 462
Set O = GetOutlookApp()
If O Is Nothing Then
Err.Raise 429, "OutlookApp", "Outlook Application does not appear to be installed."
Else
Resume InitOutlook
End If
Case Else
Beep
MsgBox "Error Number:" & vbTab & Err.Number & vbCrLf & vbCrLf & _
"Error Description:" & vbCrLf & Err.Description
End Select
Resume ProcExit
End Function
Private Function GetOutlookApp() As Object
On Error GoTo ErrHandler
Set GetOutlookApp = CreateObject("Outlook.Application")
ProcExit:
Exit Function
ErrHandler:
Set GetOutlookApp = Nothing
Resume ProcExit
End Function
Private Sub SendEmail(ByVal EmailTo As String, _
ByVal EmailBody As String, _
ByRef EmailSubject As String, _
ByVal EmailSignature As String, _
ByRef OutlookApp As Object)
[COLOR=darkgreen]'EmailSubject is ByRef so it can be permanently modified.[/COLOR]
[COLOR=darkgreen]'Dim EmailItem As Outlook.MailItem
[/COLOR]Dim EmailItem As Object
Const EMAIL_ITEM = 0
Set EmailItem = OutlookApp.CreateItem(EMAIL_ITEM)
If EmailSignature = "" Then EmailSignature = EmailItem.Body
With EmailItem
.To = EmailTo
.Subject = EmailSubject
.Body = EmailBody & vbCrLf & EmailSignature
.Send
End With
End Sub
Option Compare Database
Option Explicit
Private Enum InitialWindowState
wsMaximized = 0
wsMinimized = 1
wsNormalWindow = 2
End Enum
Public Sub SendNotices()
Dim OutlookApp As Object
'Dim OutlookApp As Outlook.Application
Dim rs As DAO.Recordset
Dim EmailTo As String
Dim EmailCC As String
Dim EmailBody As String
Dim EmailSignature As String
Dim EmailSubject As String
Dim MovedToNext As Boolean
Const EMAIL_ITEM = 0
'Error handler currently turned off for debugging.
'On Error GoTo ErrHandler
Set rs = CurrentDb.OpenRecordset("QRYFirstEmail2")
'This loop is built assuming that you want one email sent per email address - This option is correct
'If you want one email per record, instead, then this needs to be re-worked.
If Not rs.EOF Then
Set OutlookApp = GetOutlook(wsMinimized)
If Not OutlookApp Is Nothing Then
EmailSubject = "First Email"
'EmailCC = "here@bla.ac.uk
EmailSignature = Trim("Dear " & rs.Fields("FirstName").Value) & "," & vbCrLf & vbCrLf & "We have received notification that your module/s listed above has successfully passed through the approvals process." & vbCrLf & vbCrLf & _
"text here." & vbCrLf & "We do not currently have a list on the Reading List service for this module. We're therefore contacting you to offer our support with getting started with using the service to create your list yourself." & vbCrLf & _
"Similarly we also offer refresher training sessions to those academics who have not used the service in a while. Please contact us so we can set this up." & vbCrLf & _
"Please request access to your module list on the service by clicking on the link below and completing the webform:" & _
"more text here." & vbCrLf & _
"xxxxxtext here." & vbCrLf & _
"Kind regards" & vbCrLf & "Reading List team" & vbCrLf
Do
EmailTo = rs!EmailAddress
EmailBody = BuildBody(rs, EmailTo) 'NOTE: MoveNext IS PERFORMED HERE!!!
Call SendEmail(EmailTo, EmailCC, EmailBody, EmailSubject, EmailSignature, OutlookApp)
'There is no .MoveNext because that happens as part of BuildBody above.
Loop Until rs.EOF
MsgBox "All new emails have been successfully sent!", vbInformation, "Thank You"
'run query update
CurrentDb.Execute "QRYFirstEmail2UPDATE"
End If
End If
ProcExit:
If Not rs Is Nothing Then
rs.Close
Set rs = Nothing
End If
Exit Sub
ErrHandler:
Beep
MsgBox "Error Number:" & vbTab & Err.Number & vbCrLf & vbCrLf & _
"Error Description:" & vbCrLf & Err.Description
Resume ProcExit
End Sub
Private Function BuildBody(ByRef rs As DAO.Recordset, _
ByVal EmailTo As String) As String
Do Until rs!EmailAddress <> EmailTo
BuildBody = BuildBody & Chr(149) & " " & "Module Code: " & rs!ModuleCode & " - " & "Module Title: " & rs!ModuleTitle & vbCrLf
rs.MoveNext
If rs.EOF Then Exit Do
Loop
End Function
Private Function GetOutlook(Optional WindowState As InitialWindowState = wsMinimized) As Object
'Dim O As Outlook.Application
Dim O As Object
Const FOLDER_INBOX = 6
On Error GoTo ErrHandler
Set O = GetObject(, "Outlook.Application")
If O.Explorers.Count = 0 Then
InitOutlook:
O.Session.GetDefaultFolder(FOLDER_INBOX).Display
O.ActiveExplorer.WindowState = WindowState
End If
Set GetOutlook = O
ProcExit:
Set O = Nothing
Exit Function
ErrHandler:
Select Case Err.Number
Case -2147352567
'User cancelled setup, silently exit
Set O = Nothing
Case 429, 462
Set O = GetOutlookApp()
If O Is Nothing Then
Err.Raise 429, "OutlookApp", "Outlook Application does not appear to be installed."
Else
Resume InitOutlook
End If
Case Else
Beep
MsgBox "Error Number:" & vbTab & Err.Number & vbCrLf & vbCrLf & _
"Error Description:" & vbCrLf & Err.Description
End Select
Resume ProcExit
End Function
Private Function GetOutlookApp() As Object
On Error GoTo ErrHandler
Set GetOutlookApp = CreateObject("Outlook.Application")
ProcExit:
Exit Function
ErrHandler:
Set GetOutlookApp = Nothing
Resume ProcExit
End Function
Private Sub SendEmail(ByVal EmailTo As String, _
ByVal EmailCC As String, _
ByVal EmailBody As String, _
ByRef EmailSubject As String, _
ByVal EmailSignature As String, _
ByRef OutlookApp As Object)
'EmailSubject is ByRef so it can be permanently modified.
'Dim EmailItem As Outlook.MailItem
Dim EmailItem As Object
Const EMAIL_ITEM = 0
Set EmailItem = OutlookApp.CreateItem(EMAIL_ITEM)
If EmailSignature = "" Then EmailSignature = EmailItem.Body
With EmailItem
.To = EmailTo
.CC = EmailCC
.Subject = EmailSubject
.Body = EmailBody & vbCrLf & EmailSignature
.Send
End With
End Sub