【问题标题】:Outlook.MailItem - method "To" failed Outlook 2013Outlook.MailItem - 方法“To”失败 Outlook 2013
【发布时间】:2015-10-05 13:00:19
【问题描述】:

我使用“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

【问题讨论】:

  • 这条信息用英文怎么说?似乎这是 .To = TextBoxInputTo 有问题的行,您是否在代码的前面正确声明了 objMail 并且没有发布该部分?
  • 是的,我在 ThisOutlookSession 中声明了它: UserFormWorkTime.TextBoxInputTo.Text = "test@test.de" UserFormWorkTime.TextBoxInputCC.Text = "test@test.de;test2@test.de"跨度>

标签: vba outlook outlook-2010


【解决方案1】:

一些事情。

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

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

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

【讨论】:

    【解决方案2】:

    使用 MailItem 类的 Recipients 属性添加收件人。 Recipients 类的Add 方法在Recipients 集合中创建一个新的收件人。新 Recipient 对象的 Type 属性设置为关联 AppointmentItem、JournalItem、MailItem、MeetingItem 或 TaskItem 对象的默认值,并且必须重置以指示其他收件人类型。

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

    不要忘记使用 Recipient(s) 类的 ResolveResolveAll 方法来根据地址簿解析收件人。

    您是否在代码中发现任何异常或错误?

    【讨论】:

      【解决方案3】:

      错误代码是 RPC_E_DISCONNECTED,这意味着进程外 COM 服务器在您仍然引用其中一个对象时终止。

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-10-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-08-31
        • 2020-04-24
        • 1970-01-01
        相关资源
        最近更新 更多