VBA 用于在发送邮件之前检查 Outlook 是否已安装的代码
VBA code to check if Outlook is Setup before sending mail
我在 Access 中有一个代码,当用户单击保存按钮时,它会向负责人发送一封电子邮件。该代码将使用 Outlook.application 发送电子邮件。
代码工作正常,但如果未设置 outlook(即全新安装,没有任何用户帐户设置),那么我的电子邮件代码将卡住,直到用户重新激活 Access 以确认错误。
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 在 运行 这段代码之前是否已正确设置?
假设您至少使用 Outlook 2007;看看你的 Outlook.Application 对象的 DefaultProfileName 属性。如果没有创建配置文件或没有默认配置文件,这将 return 一个空字符串。
您可以检查一下,但我相信 Outlook 配置文件可能存在,但其中没有配置实际的电子邮件帐户(例如,如果用户中途中止了设置向导) .在这种情况下,您可以查看包含 Count 属性 的帐户对象。显然,如果这是 0,那么您就知道配置文件中没有配置任何帐户。
一个简单的例子来说明如何实现它。
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
我在 Access 中有一个代码,当用户单击保存按钮时,它会向负责人发送一封电子邮件。该代码将使用 Outlook.application 发送电子邮件。
代码工作正常,但如果未设置 outlook(即全新安装,没有任何用户帐户设置),那么我的电子邮件代码将卡住,直到用户重新激活 Access 以确认错误。
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 在 运行 这段代码之前是否已正确设置?
假设您至少使用 Outlook 2007;看看你的 Outlook.Application 对象的 DefaultProfileName 属性。如果没有创建配置文件或没有默认配置文件,这将 return 一个空字符串。
您可以检查一下,但我相信 Outlook 配置文件可能存在,但其中没有配置实际的电子邮件帐户(例如,如果用户中途中止了设置向导) .在这种情况下,您可以查看包含 Count 属性 的帐户对象。显然,如果这是 0,那么您就知道配置文件中没有配置任何帐户。
一个简单的例子来说明如何实现它。
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