0
votes

I have a code in Access where it will send an email to the person in charge when the user click on save button. The code will use Outlook.application to send the email.

The code works fine but if outlook is not setup (i.e. fresh install without any user account setup) then the my email code will get stuck until user reactivates Access to acknowledge the error.

Sub Send_Email()
Dim oApp As Outlook.Application
Dim oMail As MailItem

On Error GoTo MailErr
If IsNull(Email) Then
MsgBox "You do not have an email account! No email will be sent!" & vbNewLine & "Email updates will be sent to your supervisor!" Me.Email.Value = DLookup("[Email]", "tblEmployeeList", "EmpName = '" & Me.txtSupName & "'")
Else
Set oApp = CreateObject("Outlook.application")
Set oMail = oApp.CreateItem(olMailItem)

oMail.Body = "IT Incident " & Me.ReqID & " has been created."
oMail.Subject = "Alert: New IT Incident"
oMail.to = Forms!MainForm!lblITAdminEmail.Caption
oMail.Send
Set oMail = Nothing
Set oApp = Nothing
End If

MailErr:
'MsgBox Err
If Err = 287 Then
AppActivate "Microsoft Access"
MsgBox "Error 287: Mail not sent! Pls contact IT/BI"

ElseIf Err <> 0 Then
MsgBox "Pls contact BI/IT admin! Error " & Err & " occured!"
End If
Set oMail = Nothing
Set oApp = Nothing
End Sub

Is there a way to use VBA to check if Outlook has been setup properly prior to running this code?

1

1 Answers

1
votes

Assuming that you are using at least Outlook 2007; take a look at the DefaultProfileName property of your Outlook.Application Object. This will return an empty string if no profile has been created or if there is no default profile.

You could just check this, but I believe that it's possible that an Outlook profile could exist but no actual e-mail accounts are configured within it (for instance if the user aborted the set-up wizard part way through). In this instance you could look at the Accounts Object which includes a Count property. Obviously if this is 0 then you know there are no accounts configured within the profile.

A simple example of how you could implement this.

Dim oApp As Outlook.Application

Set oApp = Outlook.Application

If Not oApp.DefaultProfileName = "" Then

    If oApp.Session.Accounts.Count > 0 Then

        ' Send the e-mail

    End If

End If

Set oApp = Nothing