Outlook.MailItem - 方法 "To" 失败 Outlook 2013

Outlook.MailItem - method "To" failed Outlook 2013

我使用 "ThisOutlookSession" 和一个表格 "UserFormWorkTime"。

使用 Outlook 2010 我没有遇到任何问题。但是现在使用 Outlook 2013, 我收到以下错误:

这是我的代码:

'Benutzername für E-Mail auslesen
'MsgBox Session.Accounts.Item(1).UserName
Var = Split(Session.Accounts.Item(1).UserName, ".")
Vorname = Var(0)
Name = Var(1)

' E-Mail erstellen und anzeigen
Set objOutlook = CreateObject("Outlook.Application")
Set objMail = objOutlook.CreateItem(0)

If TextBoxInputWorkStart = "" Then

    With objMail
      .To = TextBoxInputTo
      .CC = TextBoxInputCC
      .Subject = "Arbeitszeit " + TextBoxInputDate + ""
      .HTMLBody = "Sehr geehrte Damen und Herren," & "<br><br>" & "aufgrund " & TextBoxInputReason & " war keine Zeiterfassung möglich." & "<br><br>" & "Ende: " & TextBoxInputWorkEnd & "<br><br>" & "Vielen Dank für das Eintragen" & "<br>" & Vorname & " " & Name
      ' A dialog box is open. Close it and try again => avoid this error, display it modelessly
      Unload Me
      objMail.Display

    End With

几件事。

1) 如果这是 Outlook VBA 并且您正确使用 ThisOutlookSession,则无需 Set objOutlook = CreateObject("Outlook.Application")

2) 始终将字符串 s 与 & 连接,不要使用 +

3) 不要将对象与其 .Text 值混淆。而不是 TextBoxInputDate 使用 TextBoxInputDate.Text.

尝试以下稍微修改过的代码:

Dim objMail As MailItem
' E-Mail erstellen und anzeigen

Set objMail = ThisOutlookSession.CreateItem(olMailItem)

With objMail
    .To = TextBoxInputTo.Text
    .CC = TextBoxInputCC.Text
    .Subject = "Arbeitszeit " & TextBoxInputDate.Text & ""
    .HTMLBody = "Sehr geehrte Damen und Herren," & "<br><br>" & "aufgrund " & TextBoxInputReason.Text & " war keine Zeiterfassung möglich." & "<br><br>" & "Ende: " & TextBoxInputWorkEnd.Text & "<br><br>" & "Vielen Dank für das Eintragen" & "<br>" & Vorname & " " & Name
    ' A dialog box is open. Close it and try again => avoid this error, display it modelessly
    Unload Me
    objMail.Display
End With

使用新的 Recipient 对象的 Recipients property of the MailItem class to add recipients. The Add method of the Recipients class creates a new recipient in the Recipients collection. The Type 属性 设置为关联的 AppointmentItem、JournalItem、MailItem、MeetingItem 或 TaskItem 对象的默认值,并且必须重置以指示另一个收件人类型。

  Set myItem = Application.CreateItem(olMailItem)  
  Set myRecipient = myItem.Recipients.Add ("Eugene Astafiev")  
  myRecipient.Type = olCC

不要忘记使用收件人 class 的 Resolve or ResolveAll 方法根据地址簿解析收件人。

您在代码中遇到任何异常或错误吗?

错误代码是 RPC_E_DISCONNECTED,这意味着进程外 COM 服务器已终止,而您仍然有对其对象之一的引用。

这通常发生在用户关闭 Outlook 而您的代码正在自动执行它时。