I'm automating a report that gets sent to users based on criteria the user enters. The name of the user will be used with the docmd.sendobject to email the report. Does anyone know of a way to verify that the name is in the Outlook address book and will be sent ok? Right now, I have created an Outlook object representing the Address Book and search it to find the name the report is being sent to. Just wondering if there were any simpler way to do this?
Thanks, Janel
Some of my code -
Dim rs As DAO.Recordset
Dim sManager As String
Dim dBegDate As Date
Dim dEndDate As Date
Dim sNoSend As String
Dim i As Integer
Dim bExists As Boolean
Dim oApp As Outlook.Application
Dim oNS As Outlook.NameSpace
Dim oAList As Outlook.AddressList
Dim oAListEntry As Outlook.AddressEntries
Const cQry = "qryRegionalManager"
Const cRpt = "rpt_Leads_By_Manager"
If ((Nz(Me.FromDate, ""
<> ""
And (Nz(Me.ToDate, ""
)) Then
If (MsgBox("Please confirm you wish to automate the report process for dates entered", vbQuestion + vbYesNo, "Confirm Automation"
= vbYes) Then
Set oApp = New Outlook.Application
Set oNS = oApp.GetNamespace("MAPI"
Set oAList = oNS.AddressLists("Global Address List"
If (oAList Is Nothing) Then
MsgBox "Unable to automate report. Cannot verify email addresses of Managers.", vbInformation, "Unable to Automate Report"
Else
DoCmd.Hourglass (True)
Set oAListEntry = oAList.AddressEntries
Set rs = CurrentDb.OpenRecordset(cQry)
If (rs.RecordCount <> 0) Then
rs.MoveFirst
Do While Not (rs.EOF)
sManager = Nz(rs![Regional_Sales_Manager], ""
dBegDate = Me.FromDate
dEndDate = Me.ToDate
If (sManager <> ""
Then
Me.Manager = sManager
'verify that manager exists in address book
bExists = False
'MsgBox "begin time - " & Timer()
For i = 1 To oAListEntry.Count
If (oAListEntry.Item(i).Name = sManager) Then
bExists = True
Exit For
End If
Next i
'MsgBox "end time - " & Timer()
If (bExists) Then
DoCmd.SendObject acSendReport, cRpt, acFormatRTF, sManager, , , "Sales Leads for " & dBegDate & " - " & dEndDate, Editmessage:=False
Else
sNoSend = sNoSend & sManager & vbNewLine
End If
End If 'smanager <> ""
rs.MoveNext
Loop
If (sNoSend <> ""
Then
MsgBox "Unable to verify email addresses for the following recipients - " & vbNewLine & sNoSend, vbInformation, "Unable to Email all Reports"
End If
End If 'record count verification
End If 'address book verification
End If 'verify automation
Else
MsgBox "Beginning and Ending Dates are both required", vbInformation, "Missing Required Information"
End If 'date verification
Thanks, Janel
Some of my code -
Dim rs As DAO.Recordset
Dim sManager As String
Dim dBegDate As Date
Dim dEndDate As Date
Dim sNoSend As String
Dim i As Integer
Dim bExists As Boolean
Dim oApp As Outlook.Application
Dim oNS As Outlook.NameSpace
Dim oAList As Outlook.AddressList
Dim oAListEntry As Outlook.AddressEntries
Const cQry = "qryRegionalManager"
Const cRpt = "rpt_Leads_By_Manager"
If ((Nz(Me.FromDate, ""
If (MsgBox("Please confirm you wish to automate the report process for dates entered", vbQuestion + vbYesNo, "Confirm Automation"
Set oApp = New Outlook.Application
Set oNS = oApp.GetNamespace("MAPI"
Set oAList = oNS.AddressLists("Global Address List"
If (oAList Is Nothing) Then
MsgBox "Unable to automate report. Cannot verify email addresses of Managers.", vbInformation, "Unable to Automate Report"
Else
DoCmd.Hourglass (True)
Set oAListEntry = oAList.AddressEntries
Set rs = CurrentDb.OpenRecordset(cQry)
If (rs.RecordCount <> 0) Then
rs.MoveFirst
Do While Not (rs.EOF)
sManager = Nz(rs![Regional_Sales_Manager], ""
dBegDate = Me.FromDate
dEndDate = Me.ToDate
If (sManager <> ""
Me.Manager = sManager
'verify that manager exists in address book
bExists = False
'MsgBox "begin time - " & Timer()
For i = 1 To oAListEntry.Count
If (oAListEntry.Item(i).Name = sManager) Then
bExists = True
Exit For
End If
Next i
'MsgBox "end time - " & Timer()
If (bExists) Then
DoCmd.SendObject acSendReport, cRpt, acFormatRTF, sManager, , , "Sales Leads for " & dBegDate & " - " & dEndDate, Editmessage:=False
Else
sNoSend = sNoSend & sManager & vbNewLine
End If
End If 'smanager <> ""
rs.MoveNext
Loop
If (sNoSend <> ""
MsgBox "Unable to verify email addresses for the following recipients - " & vbNewLine & sNoSend, vbInformation, "Unable to Email all Reports"
End If
End If 'record count verification
End If 'address book verification
End If 'verify automation
Else
MsgBox "Beginning and Ending Dates are both required", vbInformation, "Missing Required Information"
End If 'date verification