Loop Problem with emailing

Sevn

I trust ME!
Local time
Today, 09:41
Joined
Mar 13, 2008
Messages
97
Hello again.

I have a small module, that is suppose to send a group of sales reps their monthly reports. I thought I had everything in the right place, but I keep getting the following error:
Run-time error '91': Object variable or With block variable not set

What does this mean?

The code processes the first rep, but errors at the Loop line.

Code:
Public Sub EmailReports()

Dim olapp As Object
Dim olns As Object
Dim olfolder As Object
Dim olitem As Object
Dim olattach As Object
Dim dbPath As String
Dim Rs As DAO.Recordset
Dim strRepID As String
Dim strRep As String
Dim strRepEmail

dbPath = Application.CurrentProject.Path
Set Rs = CurrentDb.OpenRecordset("SELECT [tbl-SalesRepXREF].SalesRepID, [tbl-SalesRepXREF].SalesRepName, [tbl-SalesRepXREF].EmailAddy FROM [tbl-SalesRepXREF] WHERE ((([tbl-SalesRepXREF].SalesRepName) Like '*A&I*'));")

Do Until Rs.EOF
strRepID = Rs("SalesRepID")
strRep = Rs("SalesRepName")
strRepEmail = Rs("EmailAddy")

    Set olapp = CreateObject("Outlook.Application")
    Set olns = olapp.GetNamespace("MAPI")
    Set olfolder = olns.getdefaultfolder(6)
    Set olitem = olapp.createitem(0)
    Set olattach = olitem.attachments

        olitem.To = strRepEmail
        olitem.CC = ""
        olitem.Subject = "Sales History Reports for " & Month(Now - 1)
        olitem.body = "Hello," & Chr(13) & Chr(10) & Chr(13) & Chr(10) & "Please find enclosed the monthly Sales History Reports" & Chr(13) & Chr(10) & Chr(13) & Chr(10) & "Thanks,"

            If Month(Now) = 1 Then
                olattach.Add dbPath & "\Exports\" & Year(Now - 1) & " - 12\" & strRepID & " - " & strRep & ".zip", 1
            Else
                olattach.Add dbPath & "\Exports\" & Year(Now) & " - " & Right("0" & Month(Now - 1), 2) & "\" & strRepID & " - " & strRep & ".zip", 1
            End If

'        olitem.display
        olitem.send

    Set olitem = Nothing
    Set Rs = Nothing
    Set Db = Nothing
    Set olfolder = Nothing
    Set olns = Nothing
    Set olapp = Nothing

Rs.MoveNext
Loop

End Sub
 
Last edited:
Never mind, I had my Loop in the wrong spot.

Should have been as follows:

Code:
Public Sub EmailReports()

Dim olapp As Object
Dim olns As Object
Dim olfolder As Object
Dim olitem As Object
Dim olattach As Object
Dim dbPath As String
Dim Rs As DAO.Recordset
Dim strRepID As String
Dim strRep As String
Dim strRepEmail

dbPath = Application.CurrentProject.Path
Set Rs = CurrentDb.OpenRecordset("SELECT [tbl-SalesRepXREF].SalesRepID, [tbl-SalesRepXREF].SalesRepName, [tbl-SalesRepXREF].EmailAddy FROM [tbl-SalesRepXREF] WHERE ((([tbl-SalesRepXREF].SalesRepName) Like '*A&I*'));")

Do Until Rs.EOF
strRepID = Rs("SalesRepID")
strRep = Rs("SalesRepName")
strRepEmail = Rs("EmailAddy")

    Set olapp = CreateObject("Outlook.Application")
    Set olns = olapp.GetNamespace("MAPI")
    Set olfolder = olns.getdefaultfolder(6)
    Set olitem = olapp.createitem(0)
    Set olattach = olitem.attachments

        olitem.To = strRepEmail
        olitem.CC = ""
        olitem.Subject = "Sales History Reports for " & Month(Now - 1)
        olitem.body = "Hello," & Chr(13) & Chr(10) & Chr(13) & Chr(10) & "Please find enclosed the monthly Sales History Reports" & Chr(13) & Chr(10) & Chr(13) & Chr(10) & "Thanks,"

            If Month(Now) = 1 Then
                olattach.Add dbPath & "\Exports\" & Year(Now - 1) & " - 12\" & strRepID & " - " & strRep & ".zip", 1
            Else
                olattach.Add dbPath & "\Exports\" & Year(Now) & " - " & Right("0" & Month(Now - 1), 2) & "\" & strRepID & " - " & strRep & ".zip", 1
            End If

'        olitem.display
        olitem.send

Rs.MoveNext
Loop

    Set olitem = Nothing
    Set Rs = Nothing
    Set Db = Nothing
    Set olfolder = Nothing
    Set olns = Nothing
    Set olapp = Nothing

End Sub
 

Users who are viewing this thread

Back
Top Bottom