Please help! Sending mail from a COM add-in

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Please help!

I've been trying to send an email from within a COM addin in VB.net 2005,
but keep getting a very weird message:

'Unable to cast object of type 'System.__ComObject' to type
'Microsoft.Office.Interop.Outlook.ApplicationClass'.

I am trying to send from a form in a COM addin that I open from within
Outlook


Here's the sub I use:
-----------------------------
Private Sub SendMail( )

Dim myOlApp As Outlook._Application
'Dim myOlApp As Object
Dim MyItem As Outlook._MailItem
Dim luvRecipients As Outlook.Recipients
Dim luvRecipient As Outlook.Recipient
myOlApp = New Outlook.Application
'MyItem = myOlApp.CreateItem(OlItemType.olMailItem)
'MyItem.Display()
On Error Resume Next


'this is the body of the email
MyItem.Subject = "Image sending test"
MyItem.BodyFormat = Outlook.OlBodyFormat.olFormatHTML
MyItem.HTMLBody = "<HTML><DIV ALIGN=CENTER><IMG
src='\\Fil-nw04-10\hello.jpg></HTML>"

MyItem.Send()

End Sub
 
An Outlook add-in should never contain a statement like this:

myOlApp = New Outlook.Application

Instead, you need to be using the Outlook.Application object exposed by the application architecture. Are you using VSTO 2005 SE or IDTExtensibility2?
 
Hi Sue

I'm using IDTExtensibility2. However, and excuse my beginner-ness, but I
did think to try passing that object, but it's created in the "OnConnection"
sub in another class. How can I pass it properly to the class with my form
that sends the email...or should I even be taking that route? (Sorry if this
makes no sense...I'm still a very VB6-oriented individual). My old fashoined
and sloppy sense would have me use a global variable, but I'm guessing that's
out here in .net land. Here's where the application object is first exposed
in IDTExtensibility2:



Public Sub OnConnection(ByVal application As Object, ByVal connectMode
As Extensibility.ext_ConnectMode, ByVal addInInst As Object, ByRef custom As
System.Array) Implements Extensibility.IDTExtensibility2.OnConnection

If (connectMode <> Extensibility.ext_ConnectMode.ext_cm_Startup)
Then _
Call OnStartupComplete(custom)

olApp = CType(application,
Microsoft.Office.Interop.Outlook.Application)

End Sub ( )



Thank you very much for your help
-Josh
 
Back
Top