.NET Forum / Visual Studio.NET / VS Tools for Office / March 2007
Outlook 2007 Add-In VSTO SE: Populate To-Field
|
|
Thread rating:  |
Steffen Grellmann - 22 Mar 2007 12:20 GMT Hi newsgroup,
I've developed an Add-In for Outlook 2007 using VSTO 2005 SE which opens a form containing e-mail-addresses from a separate (company) SQL-database.
My problem is that I want to hand over e-mail-addresses from that form to the To-Field of a new e-mail. I couldn't access that To-field that simple like in VBA using ActiveInspector.CurrentItem.To.
What I have in VSTO so far is the code below, but this is always crashing with an exception error.
Any assistance would be highly appreciated.
'Reference is set to the Outlook Object Library Imports Outlook = Microsoft.Office.Interop.Outlook
Public Class MyForm
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim oOutlook As Outlook.ApplicationClass
oOutlook = CreateObject("Outlook") oOutlook.ActiveInspector.CurrentItem.To = "email@domain.com"
End Sub End Class
Kind regards,
Steffen
John - 22 Mar 2007 13:14 GMT Hi Steffen,
I don't know about the specifics of Outlook, but you don't need to create and Outlook object as you already have it in your add-in.
Change you click method to:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim oOutlook As Outlook.Application
oOutlook = Globals.ThisAddIn.Application
oOutlook.ActiveInspector.CurrentItem.To = "email@domain.com"
End Sub
Hope that helps.
Best regards
John
> Hi newsgroup, > [quoted text clipped - 30 lines] > > Steffen Ken Slovak - [MVP - Outlook] - 22 Mar 2007 14:26 GMT Or in the ThisAddIn_Startup() event you can use this.Application to set a class level Outlook.Application object which can be referenced elsewhere.
The preferred method to add recipients is to access the email item's Recipients collection. Add each email address using Recipients.Add with the email address, then use Recipient.Resolve to resolve the email reference. Set the Recipient.Type property to OlMailRecipientType.olTo.
 Signature Ken Slovak [MVP - Outlook] http://www.slovaktech.com Author: Absolute Beginner's Guide to Microsoft Office Outlook 2003 Reminder Manager, Extended Reminders, Attachment Options http://www.slovaktech.com/products.htm
> Hi Steffen, > [quoted text clipped - 54 lines] >> >> Steffen Steffen Grellmann - 25 Mar 2007 22:05 GMT Hi Ken,
thank you very much for replying. Your help is highly appreciated.
>The preferred method to add recipients is to access the email item's >Recipients collection. Add each email address using Recipients.Add with the >email address, then use Recipient.Resolve to resolve the email reference. >Set the Recipient.Type property to OlMailRecipientType.olTo. Do you have sample code to demonstrate that for use in VSTO 2005? What I found is this sample, but I'm not sure how to declare "olMailItem" and couldn't get it working in VSTO.
from http://support.microsoft.com/kb/161088/en-us
Dim objOutlookMsg As Outlook.MailItem Dim objOutlookRecip As Outlook.Recipient
' Create the Outlook session. Set objOutlook = CreateObject("Outlook.Application")
' Create the message. Set objOutlookMsg = objOutlook.CreateItem(olMailItem)
With objOutlookMsg ' Add the To recipient(s) to the message. Set objOutlookRecip = .Recipients.Add("Nancy Davolio") objOutlookRecip.Type = olTo End With
Kind regards,
Steffen
Ken Slovak - [MVP - Outlook] - 26 Mar 2007 00:10 GMT In VB.NET code it would look something like this:
Imports Outlook = Microsoft.Office.Interop.Outlook
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim oOutlook As Outlook.Application Dim objOutlookMsg As Outlook.MailItem Dim objOutlookRecip As Outlook.Recipient
oOutlook = Globals.ThisAddIn.Application
objOutlookMsg = oOutlook.CreateItem(Outlook.OlItemType.olMailItem)
objOutlookRecip = objOutlookMsg.Recipients.Add("Nancy Davolio") objOutlookRecip.Type = Outlook.OlMailRecipientType.olTo objOutlook.Recip.Resolve()
objOutlookMsg.Display() End Sub
 Signature Ken Slovak [MVP - Outlook] http://www.slovaktech.com Author: Absolute Beginner's Guide to Microsoft Office Outlook 2003 Reminder Manager, Extended Reminders, Attachment Options http://www.slovaktech.com/products.htm
> Hi Ken, > [quoted text clipped - 30 lines] > > Steffen Steffen Grellmann - 26 Mar 2007 22:06 GMT Hi Ken,
thank you very much for replying. As you may notice - I'm new to this matter.
What I have now is the code below. With this code a warning occurs in the errorlist "The component "outlook" to one refers, one did not find".
Trying to .Display in the last line produces an COMExceptionError.
The way I want to start this code is like this: New mail message is opened by the user itself, from the modified ribbon on TabNewMailMessage a vbForm get opened, from there I wish to start the Code from a button on that vbForm.
What's wrong?
Kind regards,
Steffen
Dim objOutlook As Outlook.Application Dim objOutlookMsg As Outlook.MailItem Dim objOutlookRecip As Outlook.Recipient
objOutlook = Globals.ThisAddIn.Application objOutlookMsg = objOutlook.CreateItem(Outlook.OlItemType.olMailItem) objOutlookRecip = objOutlookMsg.Recipients.Add("test@test.de") objOutlookRecip.Type = Outlook.OlMailRecipientType.olTo objOutlookRecip.Resolve() objOutlookMsg.Display()
>In VB.NET code it would look something like this: > [quoted text clipped - 17 lines] > objOutlookMsg.Display() >End Sub Ken Slovak - [MVP - Outlook] - 27 Mar 2007 15:36 GMT OK, the flow on this would be:
1. User opens item. That fires the NewInspector event of the Inspectors collection. So that event has to be handled.
2. In NewInspector you have to make sure it's a type of item you want to handle. If you only want to handle emails then you would check in that event handler for Inspector.CurrentItem.Class = OlItemType.OlMail.
3. If it's an item you want to handle you would have to provide the XML for the ribbon UI you want to provide in the GetCustomUI() callback. In VSTO 2005 SE you must override the RequestService() call to provide your ribbon object.
4. In GetCustomUI you must handle whatever email types you want based on the RibbonID string passed to you in that callback. You might want to handle "Microsoft.Outlook.Mail.Read" but not "Microsoft.Outlook.Mail.Compose" for example. Or you might want to handle both. For whichever ones you want to handle you supply the ribbon XML in that callback.
5. In the OnAction callback specified by your XML you handle the click event for that button you added to the ribbon. That's where you would open your Windows.Form and do whatever it is you want to do.
Because of all that special handling like the override to RequestService it's best to start out by reviewing the Outlook 2007 sample addins and there's also one or more for VSTO 2005 SE with Outlook. Those are all linked from the Office Web site, so I'd go there and download a few of the samples and see how everything is being handled there. Then you'll have a better basis for what you want to do.
 Signature Ken Slovak [MVP - Outlook] http://www.slovaktech.com Author: Absolute Beginner's Guide to Microsoft Office Outlook 2003 Reminder Manager, Extended Reminders, Attachment Options http://www.slovaktech.com/products.htm
> Hi Ken, > [quoted text clipped - 28 lines] > objOutlookRecip.Resolve() > objOutlookMsg.Display() Steffen Grellmann - 28 Mar 2007 21:21 GMT Hi Ken,
thank you very much for replying and for your assistance. Please let me rephrase my question: I already have modified the ribbon. I added a button to the TabNewMailMessage. That button shows up only if the user want to compose a new mail. From that button a Windows form is opened. That form should contain an addressbook which comes from our SQL-database. This is working fine so far. The question is, how do I get an e-mail-address from that form to the new mail messages .To-field that the user is composing at that point? What I have so far is quoted below.
Or do I really have to follow your flow from No 1. to No. 5 to realize that? That would be far more complicate than in VBA.
The background is that I had developed a solution like that in VBA as a document template for Outlook 2003 and have to migrate that now to a VSTO-AddIn and I'm lacking some experience at this time.
Kind regards,
Steffen
Dim objOutlook As Outlook.Application Dim objOutlookMsg As Outlook.MailItem Dim objOutlookRecip As Outlook.Recipient
objOutlook = Globals.ThisAddIn.Application objOutlookMsg = objOutlook.CreateItem(Outlook.OlItemType.olMailItem) objOutlookRecip = objOutlookMsg.Recipients.Add("test@test.de") objOutlookRecip.Type = Outlook.OlMailRecipientType.olTo objOutlookRecip.Resolve() objOutlookMsg.Display()
Ken Slovak - [MVP - Outlook] - 29 Mar 2007 14:41 GMT What you have looks OK. Are you having any problems with it?
 Signature Ken Slovak [MVP - Outlook] http://www.slovaktech.com Author: Absolute Beginner's Guide to Microsoft Office Outlook 2003 Reminder Manager, Extended Reminders, Attachment Options http://www.slovaktech.com/products.htm
> Hi Ken, > [quoted text clipped - 29 lines] > objOutlookRecip.Resolve() > objOutlookMsg.Display() Steffen Grellmann - 29 Mar 2007 21:30 GMT Hi Ken,
thank you for replying!
>What you have looks OK. Are you having any problems with it? This code is crashing with an COMException in the line objOutlookMsg.Display() at the end of the code. Any ideas?
Kind regards,
Steffen
Imports Outlook = Microsoft.Office.Interop.Outlook
Public Class MyForm Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim objOutlook As Outlook.Application Dim objOutlookMsg As Outlook.MailItem Dim objOutlookRecip As Outlook.Recipient
objOutlook = Globals.ThisAddIn.Application
objOutlookMsg = objOutlook.CreateItem(Outlook.OlItemType.olMailItem)
objOutlookRecip = objOutlookMsg.Recipients.Add("test@test.de") objOutlookRecip.Type = Outlook.OlMailRecipientType.olTo objOutlookRecip.Resolve() objOutlookMsg.Display() Me.Close() End Sub End Class
Ken Slovak - [MVP - Outlook] - 29 Mar 2007 23:21 GMT Try supplying a False argument for the modal argument and see if that helps. A lot of optional arguments that can just be left out in unmanaged code need actual arguments supplied in managed code.
You also should put in error handling with Try...Catch blocks and catch the exceptions so you know exactly what's going on. That's good programming practice, especially with managed code.
 Signature Ken Slovak [MVP - Outlook] http://www.slovaktech.com Author: Absolute Beginner's Guide to Microsoft Office Outlook 2003 Reminder Manager, Extended Reminders, Attachment Options http://www.slovaktech.com/products.htm
> Hi Ken, > [quoted text clipped - 31 lines] > End Sub > End Class Steffen Grellmann - 30 Mar 2007 09:57 GMT Hi Ken,
thank you for replying. Could you please post a code snipped how you mean to supply the False argument?
>Try supplying a False argument for the modal argument and see if that helps. >A lot of optional arguments that can just be left out in unmanaged code need >actual arguments supplied in managed code. I tried objOutlookMsg.Display(False) but this doesn't help anyway. Also no effect if I try to .Display at the FormClosing or FormClosed events.
Kind regards,
Steffen
Ken Slovak - [MVP - Outlook] - 30 Mar 2007 15:10 GMT Nothing fancy, I meant just what you did: objOutlookMsg.Display(False).
I just created a quick VSTO 2005 SE addin in VB as a test and had no problems displaying the email message. I didn't get fancy and try to do ribbon things, I just displayed a Windows.Form on startup with a "Click Me" button that when clicked displays the email message. It worked with no exceptions and I didn't put any error handling my little test.
Here's the code for the test I ran:
' ************ ThisAddin Class code
Imports Forms = System.Windows.Forms
public class ThisAddIn
Private Sub ThisAddIn_Startup(ByVal sender As Object, _ ByVal e As System.EventArgs) Handles Me.Startup
Dim newForm As New MyForm newForm.Show()
End Sub
Private Sub ThisAddIn_Shutdown(ByVal sender As Object, _ ByVal e As System.EventArgs) Handles Me.Shutdown
End Sub
End class
' ************ Now the form code, unchanged from example
Imports Outlook = Microsoft.Office.Interop.Outlook
Public Class MyForm Private Sub Button1_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles Button1.Click
Dim objOutlook As Outlook.Application Dim objOutlookMsg As Outlook.MailItem Dim objOutlookRecip As Outlook.Recipient
objOutlook = Globals.ThisAddIn.Application
objOutlookMsg = objOutlook.CreateItem(Outlook.OlItemType.olMailItem)
objOutlookRecip = objOutlookMsg.Recipients.Add("test@test.de") objOutlookRecip.Type = Outlook.OlMailRecipientType.olTo objOutlookRecip.Resolve() objOutlookMsg.Display() Me.Close() End Sub
End Class
 Signature Ken Slovak [MVP - Outlook] http://www.slovaktech.com Author: Absolute Beginner's Guide to Microsoft Office Outlook 2003 Reminder Manager, Extended Reminders, Attachment Options http://www.slovaktech.com/products.htm
> Hi Ken, > [quoted text clipped - 14 lines] > > Steffen Steffen Grellmann - 30 Mar 2007 21:49 GMT Hi Ken,
thank you very much for replying. This is working. Actually I don't want to create a new mail. I just wanted to pass the e-mail-adress to a new mail that is already open. This is working now also after changing
objOutlookMsg = objOutlook.CreateItem(Outlook.OlItemType.olMailItem)
to
objOutlookMsg = objOutlook.ActiveInspector.CurrentItem
Thank you for your assistance!
Kind regards,
Steffen
>Nothing fancy, I meant just what you did: objOutlookMsg.Display(False). > [quoted text clipped - 51 lines] > >End Class
Free MagazinesGet these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...
|
|
|