RedLeffer
RedLeffer

Reputation: 87

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:

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

Upvotes: 2

Views: 3442

Answers (3)

Dmitry Streblechenko
Dmitry Streblechenko

Reputation: 66225

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.

Upvotes: 0

Eugene Astafiev
Eugene Astafiev

Reputation: 49395

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?

Upvotes: 3

LocEngineer
LocEngineer

Reputation: 2917

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

Upvotes: 4

Related Questions