So I've got a database that exports 6 different Excel docs which need to be distributed to different departments. I'm looking for a best practice regarding my vb in sending the emails, rather than just repeating a chunck of code 6 times. Here's what I've got. It works fine - but since I will have err1.xls through err6.xls I'd like to repeat the task of emailing them for each document. Additionally, each document goes to a different email address. Thank you.
Code:
Public Function email()
Dim objOutlook As Outlook.Application
Dim mail As Outlook.MailItem
strFile = "<a href=""\\path\err1.xls"">Click here</a>"
Set objOutlook = GetObject(, "Outlook.Application")
If objOutlook Is Nothing Then
Set objOutlook = CreateObject("Outlook.Application")
End If
Set mail = objOutlook.CreateItem(olMailItem)
On Error Resume Next
With mail
.To = emailto
.CC = ""
.BCC = ""
.Subject = ""
.HTMLBody = "<p>New records have been added to your spreadsheet </p>" & vbNewLine & vbNewLine & "<p>Thank you,</p>" & vbNewLine & strFile
.Send
End With
Set mail = Nothing
Set objOutlook = Nothing
End Function