0

I have a vb.net script that sends automatic emails from Outlook. It currently works in the Office 2010 Suite. I upgraded my environments to 2013 and now it is not working. What I'm doing isn't complicated. All I want to do is grab a .pdf and attache it to an email. Then add the recipients and send the email. The place where it crashes is the actual sending of the email. It throws this execption

System.Runtime.InteropServices.COMEception occurred in Microsoft.VisualBasic.dll
Additional information: Operation aborted (Execption from HRESULT: 0x80004004 (E_ABORT))

I'm thinking because I upgraded environments something isn't referenced correctly. But I can't seem to find a fix. Below is my code, any help would be greatly appreciated! Thanks in advanced.

Imports Outlook = Microsoft.Office.Interop.Outlook
Module Module1

   Sub main()
      sendEmail()
   End Sub

   Public Sub sendEmail()
      Dim ol As Object
      Dim mail AS Object
      ol = CreateObject("Outlook.Application")
      mail = ol.CreateItem(0)
      mail.To = "email@example.com"
      mail.Subject = "Subject Line"
      mail.Body = "Body text"
      mail.Attachments.Add("path\to\attachment.pdf")

      mail.Send()                 'crashes on this line
      mail = Nothing
      ol = Nothing
   End Sub
End Module

Also I am running on Windows 7 with the Office 2013 suite. Looking at the App.config my supported Runtime version is v4.0 and the .NetFramework version is v4.5

David Zemens
  • 53,033
  • 11
  • 81
  • 130
  • see this possible solution (c#, but should be portable) -- ensure Outlook is open and that user is logged on? http://stackoverflow.com/questions/11330101/can-only-send-email-via-outlook-if-outlook-is-open – David Zemens Dec 02 '15 at 15:42
  • 1
    Firstly VB.NET and VBScript are different things (with VB.NET you don't create scripts). Your problem would depend upon what you are using to communicate with Office Outlook. But the given version might certainly be an issue (usually, the code is targetted to a given Office version). By assuming that it is Office Interop, you would have to update the reference to the current Office/Outlook in your project. – varocarbas Dec 02 '15 at 15:44
  • @DavidZemens thanks for the post. I tried that and the same errors are occurring. – Mike Bellistri Dec 02 '15 at 16:15
  • Well then you should probably consider @varocarbas ' advice, too. VB.net is not my area of expertise, but based on my limited understanding, his suggestion seems like plausible cause for your error. – David Zemens Dec 02 '15 at 16:25
  • @varocarbas Thanks for the clarification. From what I understand I want to communicate with the outlook object through the Interop references. Which lead me to thinking it could be communicating with the old versions of Outlook (since I updated it). But even after updating the references for the project it still seems to crash. I'm not looking for a 'quick fix' and move on. I want to get a full understanding of this issue. As always I'm will continue my research. – Mike Bellistri Dec 02 '15 at 16:36
  • Have you updated the reference in the project? In add reference, COM, Microsoft Outlook Object Library? Bear in mind that there might be various references (e.g., to Office too); you can check them in the properties of the project. – varocarbas Dec 02 '15 at 16:47
  • Yes I checked that before I posted and made sure everything was updated and it all checked out. – Mike Bellistri Dec 02 '15 at 17:01
  • I don't know if you are aware about this fact, but the code you are posting is not using the Office interop types. All this object + createObject is old code (i.e., one working even with VB6, before .NET) which perhaps is not compatible with the new Outlook version. That's why you get the error in VisualBasic.dll (-> deals with all the VB stuff which is not exactly VB.NET). You should better fully rely on interop (it is much more programmer friendly and reliable). There are plently of codes online. After a quick research, you should be able to find a "proper" version to replace this one. – varocarbas Dec 02 '15 at 17:07

1 Answers1

0

Since your solution is automating Outlook cross-process you first need to ensure that these types of applications are authorized so check your anti-virus state in Outlook's Trust Center:

https://msdn.microsoft.com/en-us/library/office/ff864479.aspx

I would also wrap the CreateObject call in a Try/Catch block to ensure that isn't failing to begin with. Also make sure that you have an Outlook profile created and that your application isn't running under a non-authorized security context like via a Task Scheduler process or in a web application.

Eric Legault
  • 5,706
  • 2
  • 22
  • 38