none
Exception from HRESULT: 0x80004004 (E_ABORT) When Outlook not loaded RRS feed

  • Question

  • I am running Outlook 2007 and attempting to run the below code but am getting an error when outlook is not loaded....

    Private

     

    Sub Button1_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles

    Button1.Click

     

    Dim ol As Microsoft.Office.Interop.Outlook.

    Application

    ol = CreateObject(

    "Outlook.Application"

    )

     

    Dim mail As MailItem = ol.CreateItem(OlItemType

    .olMailItem)

     

    Const ToVar As String =

    "bryan.gugelman@gmail.com"

     

    ' MessageBox.Show("Ava")

     

     

    AddHandler ol.ItemSend, AddressOf

    EmailSent

    mail = ol.CreateItem(0)

     

    Dim Recp As Recipient

    = mail.Recipients.Add(ToVar)

    Recp.Resolve()

    mail.Subject =

    "Trying new method"

    mail.Body =

    "Sent while Outlook is open"

    mail.Attachments.Add(

    "C:\test\test.txt"

    )

    mail.Display()

     

     

    End

    Sub

     

    Private Sub EmailSent(ByVal item As Object, ByRef cancel As Boolean

    )

     

    Dim mail As MailItem = TryCast(item, MailItem

    )

     

    Dim inFile As IO.

    StreamWriter

     

    Dim logFile As String =

    "C:\test\log.txt"

     

    Dim DateTimeOccured As

    Date

    DateTimeOccured =

    DateTime

    .Now

     

    If mail IsNot Nothing

    Then

     

    For Each recp As Recipient In

    mail.Recipients

     

    Const ToVar As String =

    "bryan.gugelman@gmail.com"

     

    If recp.Address <> ToVar

    Then

     

    MessageBox.Show("Reciepient Changed... Log to Database."

    )

     

    If IO.File.Exists(logFile)

    Then

    inFile = IO.

    File

    .AppendText(logFile)

    inFile.WriteLine(

    "Reciepient Changed.... Logged to Database."

    )

    inFile.Write(recp.Address)

    inFile.Write(

    "......"

    )

    inFile.Write(DateTimeOccured)

    inFile.WriteLine(

    ""

    )

    inFile.Close()

     

    Else

    inFile = IO.

    File

    .CreateText(logFile)

    inFile.Close()

    inFile = IO.

    File

    .AppendText(logFile)

    inFile.WriteLine(

    "No Log File Found... Added to Database."

    )

    inFile.WriteLine(

    "Reciepient Changed.... Logged to Database."

    )

    inFile.Write(recp.Address)

    inFile.Write(

    "......"

    )

    inFile.Write(DateTimeOccured)

    inFile.WriteLine(

    ""

    )

    inFile.Close()

     

    End

    If

     

    End

    If

     

    Next

     

    MessageBox.Show("Email Send."

    )

     

    End

    If

     

    End

    Sub

     

    End

     

    Class

     

     

    Monday, May 9, 2011 8:59 PM

Answers

  • Hi,

    see if that helps:

      Private Function CreateInstance() As Outlook.Application
    
        '' is there an existing application object ?
        If (Process.GetProcessesByName("OUTLOOK").Count() > 0) Then
    
          '' use the GetActiveObject method to attach an existing application object
          Return System.Runtime.InteropServices.Marshal.GetActiveObject("Outlook.Application")
        Else
    
          '' create a new instance
          Return New Outlook.Application()
        End If
    
      End Function
    


    Helmut Obertanner [http://www.x4u.de] [http://www.outlooksharp.de]
    Tuesday, May 10, 2011 7:24 AM
    Answerer

All replies

  • Hi,

    see if that helps:

      Private Function CreateInstance() As Outlook.Application
    
        '' is there an existing application object ?
        If (Process.GetProcessesByName("OUTLOOK").Count() > 0) Then
    
          '' use the GetActiveObject method to attach an existing application object
          Return System.Runtime.InteropServices.Marshal.GetActiveObject("Outlook.Application")
        Else
    
          '' create a new instance
          Return New Outlook.Application()
        End If
    
      End Function
    


    Helmut Obertanner [http://www.x4u.de] [http://www.outlooksharp.de]
    Tuesday, May 10, 2011 7:24 AM
    Answerer
  • Helmut's solution doesn't actually solve the problem raised.

    The problem raised (and the problem that I am getting on a client's PC but not mine is that E_ABORT is raised if outlook is NOT open.  This corresponds with the "Else" condition in Helmut's reply which then proceeds with the crash. 

    If Outlook is open (e.g. the "If" condition is met) then New Outlook.Application() not only provides an interface to Outlook but methods that add a Recipient or adds an Attachment run without failt.   

    I think what Xtalwind didn't metnion is that it isn't

    New outlook.Application 

    that is causing the E_ABORT, it is these two commands:

    Outlook.Recipient oRecip = (Outlook.Recipient)oMsg.Recipients.Add(sRecipientEmailAddress);

    Outlook.Attachment oAttach = oMsg.Attachments.Add(sZipFilename, iAttachType, iPosition, sDisplayName);

    - but only on some machines and only when Outlook wasn't already open.

    I suspect this has something to do with security.
    Friday, April 13, 2012 12:08 AM
  • i do not see anywehre call to

    var session = outlookApp.GetNamespace("MAPI");

    session.Logon();

    both calls are importnant when starting outlook on your own.

    Friday, April 13, 2012 8:59 AM