简体   繁体   中英

Outlook.MailItem - method "To" failed Outlook 2013

I use "ThisOutlookSession" and a form "UserFormWorkTime".

With Outlook 2010 I had no problems. But now with Outlook 2013, I get the following error:

错误

Here is my code:

'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

A few things.

1) If this is Outlook VBA and you correctly use ThisOutlookSession , then there is no nee to Set objOutlook = CreateObject("Outlook.Application")

2) Always concatenate string s with & , do not use +

3) Do not conflate the object with its .Text value. Instead of TextBoxInputDate use TextBoxInputDate.Text .

Try the following, slightly adapted code:

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

Use the 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 property of a new Recipient object is set to the default value for the associated AppointmentItem, JournalItem, MailItem, MeetingItem, or TaskItem object and must be reset to indicate another recipient type.

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

Don't forget to use the Resolve or ResolveAll methods of the Recipient(s) class to get recipients resolved against the address book.

Do you get any exceptions or errors in the code?

The error code is RPC_E_DISCONNECTED, which means the out-of-proc COM server terminated while you still had a reference to one of its objects.

This usually happens when the user closes Outlook while your code is automating it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM