0

我正在尝试从 Access 通过 Outlook 发送自动电子邮件,但我遇到了一个问题,如果用户尚未打开他们的电子邮件,我将获得Application-Defined or Object-Defined Error. 我正在使用后期绑定来避免 .dll,因为我在 Office 2003 和 Office 2010 上都有用户。

无论如何围绕这个错误并且仍然允许电子邮件通过?或者如果还没有,可能会“迫使”前景开放?

提前致谢

当然,这是电子邮件的完整代码。

当我通过它失败时Set appOutlookRec = .Recipients.Add(myR!Email)

Option Explicit
Function SendEmail(strDep, strIssue, strPriority, strDate, strDesc, wonum, user)

Const olMailItem = 0
Const olTo = 1
Const olCC = 2
Dim sqlVar As String
Dim strSQL As String

If strDep = "Cycle" Then

ElseIf strDep = "Fabrication" Then
    sqlVar = "Fabricator"
ElseIf strDep = "Facility" Then
    sqlVar = "Facility"
ElseIf strDep = "Gage" Then
    sqlVar = "Gage"
ElseIf strDep = "IT" Then
    sqlVar = "IT"
ElseIf strDep = "Machine Shop" Then
    sqlVar = "Machine_Shop_Manager"
ElseIf strDep = "Safety" Then
    sqlVar = "Safety"
ElseIf strDep = "Maintenance" Then
    sqlVar = "Maintenance_Manager"
ElseIf strDep = "Supplies Request" Then
    sqlVar = "Supplies"
Else:
End If

Dim myR As Recordset

'Refers to Outlook's Application object
Dim appOutlook As Object
'Refers to an Outlook email message
Dim appOutlookMsg As Object
'Refers to an Outlook email recipient
Dim appOutlookRec As Object

'Create an Outlook session in the background
Set appOutlook = CreateObject("Outlook.Application")

'Create a new empty email message
Set appOutlookMsg = appOutlook.CreateItem(olMailItem)

'Using the new, empty message...
With appOutlookMsg    

strSQL = "SELECT Email FROM Employees WHERE " & sqlVar & " = True"
Set myR = CurrentDb.OpenRecordset(strSQL)

Do While Not myR.EOF
Set appOutlookRec = .Recipients.Add(myR!Email)
appOutlookRec.Type = olTo
myR.MoveNext
Loop

strSQL = "SELECT Email FROM Employees WHERE '" & user & "' = Username"

Set myR = CurrentDb.OpenRecordset(strSQL)

Set appOutlookRec = .Recipients.Add(myR!Email)
appOutlookRec.Type = olCC

.Subject = wonum

.Body = "Department: " & strDep & vbNewLine & vbNewLine & _
    "Issue is at: " & strIssue & vbNewLine & vbNewLine & _
    "Priority is: " & strPriority & vbNewLine & vbNewLine & _
    "Complete by: " & strDate & vbNewLine & vbNewLine & _
    "Description: " & strDesc

.Send

End With

Set myR = Nothing
Set appOutlookMsg = Nothing
Set appOutlook = Nothing
Set appOutlookRec = Nothing

End Function
4

3 回答 3

0

尝试在 .Send 之前使用 .Save。我正在通过 MS Access 安排 Outlook 代码。

于 2013-07-08T14:39:44.660 回答
0

因此,似乎正在发生的是您对 Outlook.Application 的引用。停滞 - 因为没有更好的词。您不仅想创建 Outlook 会话 - 您还想连接到现有的正在运行的应用程序。

我不是 Access 的专家,所以我只建议笼统地说:尝试在已经运行的 Outlook 应用程序上获取句柄,否则让它打开 Outlook(给它时间使用 sleep/wait 和 DoEvents 命令完全启动)并再次尝试获取该句柄。

VBA在 Outlook 中使用试图读取发件人姓名(也得到相同的错误)。追溯到我获取当前 Outlook 应用程序句柄的方法。代替:

Set appOutlook = CreateObject("Outlook.Application");

我不得不:

Set appOutlook = ThisOutlookSession;

希望这可以帮助!

于 2016-08-23T13:45:51.053 回答
0

在该行之后Set appOutlook = CreateObject("Outlook.Application"),添加以下内容:

set NS = appOutlook.GetNamespace("MAPI")
NS.Logon
于 2016-08-23T15:19:09.330 回答