Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Do NOT send email if there is no data 1

Status
Not open for further replies.

LD1010

Technical User
Dec 6, 2001
78
US
With the help provided by Remou in thread181-1091473 I was able to automate the sending of email notifications of MDO "Mandatory Day Off" and PTO "Paid Time Off" time off being taken by a specified group of empolyees. This all works well except there are some days that no one is scheduled off and then the recipients would receive an email with no data for the body, in other words, blank. Can someone help me rewrite this code so the email will only be sent if there is data for the report. The record source for the report is a query called qryMDO. As always your expertise and help is much appreciated. Following is the code I have so far.

Private Sub Command24_Click()
On Error GoTo Err_Command24_Click

Const ForReading = 1, ForWriting = 2, ForAppending = 3

Dim fs, f
Dim RTFBody, strTo
Dim MyApp As New Outlook.Application
Dim MyItem As Outlook.MailItem

DoCmd.OutputTo acOutputReport, "rptMDO", acFormatHTML, "rptMDO.htm"
Set fs = CreateObject("Scripting.FileSystemObject")
Set f = fs.OpenTextFile("rptMDO.htm", ForReading)
RTFBody = f.ReadAll
f.Close

Set MyItem = MyApp.CreateItem(olMailItem)
With MyItem
.To = Me.txtSendTo
.Subject = "MDO/PTO notification"
.HTMLBody = RTFBody
End With
MyItem.Send

DoCmd.Close

Exit_Command24_Click:
Exit Sub

Err_Command24_Click:
MsgBox Err.Description
Resume Exit_Command24_Click

End Sub
 
Hint: the line of code will be something like:

If <whatever condition when I don't want email sent> Then Exit Sub
 
You could just wrap the entire mailitem part in an if statement, something like:
Code:
[red]If RTFBody <> "" then[/red]
Set MyItem = MyApp.CreateItem(olMailItem)...

...MyItem.Send
[red]end if[/red]
Hope this helps

Andy
---------------------------------
[green]' Signature removed for testing purposes.[/green]

 
Thanks for the help JoeAtWork and HarleyQuinn. It seems to be working fine now.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top