Click here to Skip to main content
15,892,537 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
When I make the following call to declare the object in a sub, the program crashes in the VS environment.

Dim oApp As New Outlook.Application

Why does this happen and how is it corrected?
Posted
Updated 7-Feb-13 13:36pm
v2
Comments
Sergey Alexandrovich Kryukov 7-Feb-13 19:42pm    
Not enough information. Could you wrap it in try-catch block and get exception information?
—SA
KevinBrady57 7-Feb-13 19:58pm    
I did. Here's the code

Try
Dim oApp As New Outlook.Application

Catch e As Exception
MsgBox(e.Message)

End Try

No exception is reported. VS just returns to my main program form.
Sergey Alexandrovich Kryukov 7-Feb-13 20:14pm    
What do you mean? (And what is "main program form"?) Did you put a break point inside Catch, before MsgBox is called? If it returned to the called, it means New Outlook.Application was called without a problem.
If happens: it may mean that there is something wrong in your observations. Under certain conditions, the debugger shows incorrect location...
What you say now is self-contradicting. You need to get reliable observations.
—SA

1 solution

Add Reference Microsoft.Interop.Outlook
 
Share this answer
 
Comments
KevinBrady57 8-Feb-13 11:05am    
I added a reference to Microsoft.Office.Interop.Outlook in the project. The call to create the Outlook.Application object still bails-out. Below is the code for the entire subroutine.

Sub SendOutlookEmail(ByVal iMsgBody As String, ByVal iMsgSubj As String, ByVal iToEMailAddr As String, ByVal iAttachment As String)
' Create an Outlook application.
'Dim oApp As Outlook._Application

Dim oApp As Outlook.Application

Try
oApp = New Outlook.Application ' sub bails-out here

Catch e As Exception

MsgBox(e.Message)

End Try

' Create a new MailItem.
Dim oMsg As Outlook._MailItem
Dim oAttachs As Outlook.Attachments = oMsg.Attachments
Dim oAttach As Outlook.Attachment

oMsg = oApp.CreateItem(Outlook.OlItemType.olMailItem)

oMsg.Subject = iMsgSubj

oMsg.Body = iMsgBody & vbCr

oMsg.To = iToEMailAddr

If iAttachment <> "" Then
Dim sSource As String = iAttachment
' TODO: Replace with attachment name
Dim sDisplayName As String = FileNameOnly(iAttachment) ' use the filename as the attachement name
Dim sBodyLen As String = oMsg.Body.Length
oAttachs = oMsg.Attachments
oAttach = oAttachs.Add(sSource, , sBodyLen + 1, sDisplayName)
End If


' Send the message
oMsg.Send()

' Clean up
oApp = Nothing
oMsg = Nothing
oAttach = Nothing
oAttachs = Nothing


End Sub

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900