VBA代码,用于在发送邮件之前检查Outlook是否已安装 [英] VBA code to check if Outlook is Setup before sending mail

查看:155
本文介绍了VBA代码,用于在发送邮件之前检查Outlook是否已安装的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Access中有一个代码,当用户单击保存"按钮时,它将向负责人发送电子邮件.该代码将使用Outlook.application发送电子邮件.

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.

该代码可以正常工作,但是如果未设置Outlook(即未安装任何用户帐户的全新安装),则我的电子邮件代码将被卡住,直到用户重新激活Access以确认错误为止.

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

是否可以使用VBA在运行此代码之前检查Outlook是否已正确设置?

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

推荐答案

假定您至少使用Outlook 2007;看看

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.

您可以检查一下,但是我相信可能存在一个Outlook配置文件,但其中没有配置实际的电子邮件帐户(例如,如果用户在途中中止了设置向导).在这种情况下,您可以查看帐户对象,该对象包括

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

这篇关于VBA代码,用于在发送邮件之前检查Outlook是否已安装的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆