Help!

Please help! Sending mail from a COM add-in


Post new topic   General Reply to Topic (not reply to a specific post)    Forums Home -> Program Add-Ins RSS
Next:  Deleting a single occurrence of the meeting  
Author Message
Dewey
External


Since: May 26, 2006
Posts: 16



PostPosted: Tue Jul 17, 2007 4:16 pm    Post subject: Please help! Sending mail from a COM add-in
Archived from groups: microsoft>public>outlook>program_addins (more info?)

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
-----------------------------

Thanks very much for any help in advance!

-Josh
Back to top
Sue Mosher [MVP-Outlook]
External


Since: Feb 11, 2005
Posts: 21627



PostPosted: Tue Jul 17, 2007 7:24 pm    Post subject: Re: Please help! Sending mail from a COM add-in [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

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?

--
Sue Mosher, Outlook MVP
Author of Microsoft Outlook 2007 Programming:
Jumpstart for Power Users and Administrators
http://www.outlookcode.com/article.aspx?id=54


"Dewey" <Dewey.RemoveThis@discussions.microsoft.com> wrote in message news:701C8174-91B2-4309-9A29-647A0F335A73@microsoft.com...
> 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
> -----------------------------
>
> Thanks very much for any help in advance!
>
> -Josh
>
Back to top
Dewey
External


Since: May 26, 2006
Posts: 16



PostPosted: Tue Jul 17, 2007 7:24 pm    Post subject: Re: Please help! Sending mail from a COM add-in [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

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



"Sue Mosher [MVP-Outlook]" wrote:

> 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?
>
> --
> Sue Mosher, Outlook MVP
> Author of Microsoft Outlook 2007 Programming:
> Jumpstart for Power Users and Administrators
> http://www.outlookcode.com/article.aspx?id=54
>
>
> "Dewey" <Dewey.TakeThisOut@discussions.microsoft.com> wrote in message news:701C8174-91B2-4309-9A29-647A0F335A73@microsoft.com...
> > 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
> > -----------------------------
> >
> > Thanks very much for any help in advance!
> >
> > -Josh
> >
>
Back to top
Sue Mosher [MVP-Outlook]
External


Since: Feb 11, 2005
Posts: 21627



PostPosted: Tue Jul 17, 2007 8:46 pm    Post subject: Re: Please help! Sending mail from a COM add-in [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

You can use the article at http://msdn2.microsoft.com/en-us/library/Aa155703.aspx as your guide and use a separate OutAddIn class to expose an Outlook.Application object that all other modules can access.

--
Sue Mosher, Outlook MVP
Author of Microsoft Outlook 2007 Programming:
Jumpstart for Power Users and Administrators
http://www.outlookcode.com/article.aspx?id=54


"Dewey" <Dewey.TakeThisOut@discussions.microsoft.com> wrote in message news:56ECDD47-8E73-4528-BB86-F6CE6B28F2B8@microsoft.com...
> 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
>
>
>
> "Sue Mosher [MVP-Outlook]" wrote:
>
>> 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?
>>
>> "Dewey" <Dewey.TakeThisOut@discussions.microsoft.com> wrote in message news:701C8174-91B2-4309-9A29-647A0F335A73@microsoft.com...
>> > 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
>> > -----------------------------
>> >
>> > Thanks very much for any help in advance!
>> >
>> > -Josh
>> >
>>
Back to top
Display posts from previous:   
Post new topic   General Reply to Topic (not reply to a specific post)    Forums Home -> Program Add-Ins All times are: Eastern Time (US & Canada) (change)
Page 1 of 1

 
You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum