Option Compare Database
Public Function SendEmail()
PROC_DECLARATIONS:
Dim olApp As Outlook.Application
Dim olnamespace As Outlook.NameSpace
Dim olMail As Outlook.MailItem
Dim rstEmailDets As Recordset
Dim strSender As String
Dim strRecipient As String
Dim strEmail As String
PROC_START:
On Error GoTo PROC_ERROR
PROC_MAIN:
DoCmd.SetWarnings False
Set dbs = CurrentDb
'Put on hourglass
DoCmd.Hourglass True
'Collect the email details in a recordset to use in the email loop below
Set rstEmailDets = dbs.OpenRecordset("SELECT Voornaam, E-mail FROM Tabel_Rubicon_Gebruikers;")
With rstEmailDets
.MoveFirst
Do While Not rstEmailDets.EOF
'Set parameters for email: recipient name, recipient email
strRecipient = Nz(rstEmailDets("Voornaam"), "")
strEmail = Nz(rstEmailDets("Tabel_Rubicon_Gebruikers"), "")
With olMail
'Email Details
.To = strEmail
.Subject = "Bevestiging Incident voor " & strRecipient
.Body = vbCrLf & _
"Hallo " & strRecipient & "," & vbCrLf & vbCrLf & _
"Wij zullen je zo spoedig mogelijk helpen" & vbCrLf & vbCrLf & " "
.Importance = olImportanceNormal
.Send
End With
.MoveNext
Loop
End With
MsgBox "All Emails have now been sent, Thank you for your patience"
PROC_EXIT:
' Perform cleanup code here, set recordsets to nothing, etc.
On Error Resume Next
DoCmd.RunCommand acCmdWindowHide '(hides the database window)
Set olApp = Nothing
Set olnamespace = Nothing
Set olMail = Nothing
Set dbs = Nothing
Set rstEmailDets = Nothing
DoCmd.Hourglass False
Exit Function
PROC_ERROR:
If Err = -2147467259 Then
MsgBox "You have exceeded the storage limit on your mail box. Please delete some items before clicking OK", vbOKOnly
Resume
End If
If Err = 2501 Then
MsgBox "You have attempted to cancel the output of the emails." & vbCrLf & _
"This will cause major problems." & vbCrLf & _
"Please be Patient"
Resume
End If
End Function
... you should actually have been referencing the email, rather than the table... so it should be strEmail = Nz(rstEmailDets("Email"), "")strEmail = Nz(rstEmailDets("Tabel_Rubicon_Gebruikers"), "")
With olMail
Public Function SendEmail()
PROC_DECLARATIONS:
Dim olApp As Outlook.Application
Dim olnamespace As Outlook.NameSpace
Dim olMail As Outlook.MailItem
Dim rstEmailDets As Recordset
Dim strSender As String
Dim strRecipient As String
Dim strEmail As String
Dim dbs As Database
PROC_START:
On Error GoTo PROC_ERROR
PROC_MAIN:
DoCmd.SetWarnings False
Set dbs = CurrentDb
'Put on hourglass
DoCmd.Hourglass True
'Collect the email details in a recordset to use in the email loop below
Set rstEmailDets = dbs.OpenRecordset("SELECT Voornaam, Email FROM Tabel_Rubicon_Gebruikers;")
With rstEmailDets
.MoveFirst
Do While Not rstEmailDets.EOF
'Set parameters for email: recipient name, recipient email
strRecipient = Nz(rstEmailDets("Voornaam"), "")
strEmail = Nz(rstEmailDets("Email"), "")
Set olApp = New Outlook.Application
Set olnamespace = olApp.GetNamespace("MAPI")
Set olMail = olApp.CreateItem(olMailItem)
With olMail
'Email Details
.To = strEmail
.Subject = "Bevestiging Incident voor " & strRecipient
.Body = vbCrLf & _
"Hallo " & strRecipient & "," & vbCrLf & vbCrLf & _
"Wij zullen je zo spoedig mogelijk helpen" & vbCrLf & vbCrLf & " "
.Importance = olImportanceNormal
.Save
.Display
If MsgBox("Email Saved, if you like the send it click yes, otherwise click no", vbYesNo, "Send Email?") = vbYes Then
.Send
Else: GoTo PROC_EXIT
End If
End With
.MoveNext
Loop
End With
MsgBox "All Emails have now been sent, Thank you for your patience"
PROC_EXIT:
' Perform cleanup code here, set recordsets to nothing, etc.
On Error Resume Next
DoCmd.RunCommand acCmdWindowHide '(hides the database window)
Set olApp = Nothing
Set olnamespace = Nothing
Set olMail = Nothing
Set dbs = Nothing
Set rstEmailDets = Nothing
DoCmd.Hourglass False
Exit Function
PROC_ERROR:
If Err = -2147467259 Then
MsgBox "You have exceeded the storage limit on your mail box. Please delete some items before clicking OK", vbOKOnly
Resume
End If
If Err = 2501 Then
MsgBox "You have attempted to cancel the output of the emails." & vbCrLf & _
"This will cause major problems." & vbCrLf & _
"Please be Patient"
Resume
End If
MsgBox "error no: " & Err.Number & " Error Description: " & Err.Description
Resume PROC_EXIT
End Function
Dim dbs As Database
Error no: 13 Error Discription: Types do not match
Jibbadiah said:Creegfire,
This code works for me. In fact I recreated your table and field names and while testing I accidentally sent 3 emails to fictitious email accounts... phil@msn.com isn't fictitious !! ... and he didn't know German!!
I have the following references libraries ticked:
visual basic for applications
microsoft access 8 object library
microsoft 3.51 dao object library
microsoft outlook 8 object library
If it still doesn't work after selecting them, then zip a copy of your db and post to this site. Ensure it is Access 97, and don't include any confidential data.
J.
Public Function SendEmail(strEmail As String, strSubject As String, strEmailBody As String, blnSend As Boolean, Optional strAttachment As String)
PROC_DECLARATIONS:
Const sProc_Name As String = "EmailTASSurvey"
Dim olApp As Outlook.Application
Dim olnamespace As Outlook.NameSpace
Dim olMail As Outlook.MailItem
Dim strThisMonth As String
PROC_START:
On Error GoTo PROC_ERROR
PROC_MAIN:
DoCmd.SetWarnings False
'Create a new instance of an Outlook Application object
Set olApp = New Outlook.Application
Set olnamespace = olApp.GetNamespace("MAPI")
Set olMail = olApp.CreateItem(olMailItem)
With olMail
'Email Details
.To = strEmail
.Subject = strSubject
'Email Content
.Body = "" & strEmailBody & vbCrLf
.Importance = olImportanceNormal
.ReadReceiptRequested = True
'If attachment is sent then attach
If Len(strAttachment) > 0 Then
.Attachments.Add (strAttachment)
End If
'If bln Send = true then Send, otherwise just save
If blnSend = True Then
.Send
Else
.Save
.Display
End If
End With
'Update status on status bar
vStatusBar = SysCmd(acSysCmdClearStatus)
Proc_Exit:
' Perform cleanup code here, set recordsets to nothing, etc.
On Error Resume Next
Set olApp = Nothing
Set olnamespace = Nothing
Set olMail = Nothing
DoCmd.Hourglass False
Exit Function
PROC_ERROR:
If Err = -2147467259 Then
MsgBox "You have exceeded the storage limit on your mail box. Please delete some items before clicking OK", vbOKOnly
Else
MsgBox Err.Number & " - " & Err.Description
End If
Resume Proc_Exit
End Function
Private Function Ed
Dim strFilename As String
strFilename = "c\temp\Attachment.xls"
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel97, "qryOutputForAttachment", strFilename, True
SendEmail "phil@msn.co.uk", "Folks, " & vbCrLf & vbCrLf & "This is my sample email." & vbCrLf & vbCrLf & "Regards," & vbCrLf & vbCrLf & "Ed" & vbCrLf, False, strFilename
End Function