VSTO Tour Series (v): Create an Outlook solution

Source: Internet
Author: User
Tags contact form visual studio 2010

Original: VSTO Tour series (v): Create an Outlook solution

Summary of the topic

    • Introduction
    • Outlook object Model
    • Customizing Outlook Forms
    • Summary

First, Introduction

In the previous topic, we gave you a brief introduction to how to create a word solution, so you'll be introduced to Outlook related content in this topic. We can see from the templates under the Office node in visual Studio 2010 that Outlook has only add-in templates and does not provide document-level templates such as Word or Excel. So VSTO does not create host items and host controls for Outlook solutions (both Excel and VSTO in Word provide them with host items and host controls, because these controls are document-level.) For specific content of host controls and host items, refer to topic one in this series. To better create a solution for Outlook, the following is a detailed introduction to the Outlook object model.

Second, the Outlook object model

2.1 Application Object

The Application Object represents the Outlook application and is also the top-level object in the Outlook object Model (which is the same as Word and Excel). An instance of the object that is to be obtained in a VSTO project can also be implemented by the application field of the ThisAddIn class, with the following code: Globals.ThisAddIn.Application or This.application

2.2 Namespace Object

The session property of the Application object Returns a Namespace object, which is literally the meaning of the conversation, and in web development we can get the saved information from the session object, as well, We can access Outlook data through namespace objects, such as when we want to get the number of messages in the Inbox, we can use the Namespace object at this point, the code is as follows:

  // Get Inbox folder             This . Application.Session.GetDefaultFolder (Outlook.OlDefaultFolders.olFolderInbox);             // get the number of            messages in your Inbox int mailnumbers = Inbox.Items.Count;

2.3 Explorer Object

The Explorer Object is a window that displays the contents of a folder in Outlook. Folders contain items, such as mail items, the pop-up point says that when we click on a message or calendar or contact in Outlook, the window that appears is an Explorer object.

2.4 Inspector Object

The Inspector Object is a window that displays each item in Outlook, such as a single e-mail item, an individual contact item. See here, some friends will certainly confuse the Explorer object with the Inspector object, believe that through the following figure you can definitely understand the difference between the two:

2.5 Outlook folder, which is the MAPIFolder object

Outlook folders are used to store items (that is, mail items, contact items, task items, and so on), and you can create your own folders in Outlook, but some default folders are also available in Outlook. The above code: this. Application.Session.GetDefaultFolder (outlook.oldefaultfolders. olFolderInbox); is to get the Inbox folder, and if you need to get another folder, you can get it by changing the OlDefaultFolders enumeration value.

2.6 Outlook items

Items in Outlook are included in the folder. We can create a mail item with the following code, and if you want to get another Outlook item, you can modify the Outlook.olitemtype enumeration value to implement the

// Create a mail item            Outlook.MailItem MailItem = (outlook.mailitem)this. Application.createitem (Outlook.olitemtype.  olMailItem);

Iii. Customizing Outlook Forms

With the introduction of so many of the objects in Outlook, you must be eager to know what features the VSTO technology can do for Outlook. The following is the use of VSTO technology to customize the Outlook contact form, in many cases, we would like to contact the form of a parent's contact information, and then in the existing Outlook interface does not have such a field to fill us, so here through the form region Way to implement a custom contact form, we need to include a control that enters the parent's name and contact information in the contact form.

First, we need to create the Outlook Form region and create the following steps:

    1. Right-click Project, select Add-New Project-->outlook form region
    2. Select the "Design a new form region" option

3. The form region type is selected adjacent to the custom form attached to the bottom of the page.

4. Enter a name for the form region, and the settings display the form region in both the edit mode and the reading pattern.

5. Embed our form region in the contact form.

After a whole series of steps, VS will help us automatically create an empty form region, and if we want to modify the settings of the form region we just created, we can modify it through its Properties window.

After creating the form region, we need to add the controls we need to customize in the form region, where the design interface is as follows:

After the design of the interface, the end is to implement our background logic, that is, write code in the background to complete the functions we need to implement. The specific background code is as follows (because there are explanations in the code comments, people do not understand the implementation process can see code comments):

 //corresponding contact person (contacts) object        PrivateOutlook.ContactItem ContactItem; //Custom Property Objects        PrivateOutlook.itemproperty Mothername =NULL; PrivateOutlook.itemproperty Mothertelnumber =NULL; PrivateOutlook.itemproperty Fathername =NULL; PrivateOutlook.itemproperty Fathertelnumber =NULL; //occurs before the form region is displayed. //Use this.        Outlookitem gets a reference to the current Outlook item. //Use this. Outlookformregion gets a reference to the form region.         Private voidContactformregion_formregionshowing (Objectsender, System.EventArgs e) {            //get the Contact object for the FormRegionContactItem = This. Outlookitem asOutlook.ContactItem; //when you remove a value from a custom attribute, first make sure that the custom property is not empty. ensureproperties (); //to remove a value from a contact's custom attribute to assign a value to a controlTxbmothername.text =Mothername.value; Txbfathername.text=Fathername.value; Txbmothertel.text=Mothertelnumber.value; Txbfathertel.text=Fathertelnumber.value; }        //occurs when the form region is closed. //Use this.        Outlookitem gets a reference to the current Outlook item. //Use this. Outlookformregion gets a reference to the form region.         Private voidContactformregion_formregionclosed (Objectsender, System.EventArgs e) {            //Releasing ObjectsSystem.Runtime.InteropServices.Marshal.FinalReleaseComObject (ContactItem); ContactItem=NULL; }        //Make sure all custom properties are not empty        Private voidensureproperties () {Ensureitemproperty (refMothername,"Monthername", Outlook.OlUserPropertyType.olText); Ensureitemproperty (refFathername,"Fathername", Outlook.OlUserPropertyType.olText); Ensureitemproperty (refMothertelnumber,"Mothertelnumber", Outlook.OlUserPropertyType.olText); Ensureitemproperty (refFathertelnumber,"Fathertelnumber", Outlook.OlUserPropertyType.olText); }        //make sure that the project property is not a null reference        Private voidEnsureitemproperty (refOutlook.itemproperty Property,stringname, Outlook.oluserpropertytype propertyType) {            //If the custom property is empty//first get the Property object from the collection of properties associated with the contact item//If the property does not already exist in the project collection, add the property name into the ItemProperties collection            if(Property = =NULL) { property=Contactitem.itemproperties[name]; if(Property = =NULL) { property=contactItem.ItemProperties.Add (name, PropertyType); }            }        }        //Father Name Modify event        Private voidTxbfathername_textchanged (Objectsender, EventArgs e) {            //Save the value into a custom propertyFathername.value =Txbfathername.text; }        //Father's phone number modification event        Private voidTxbfathertel_textchanged (Objectsender, EventArgs e) {            //Save the value into a custom propertyFathertelnumber.value =Txbfathertel.text; }        //Mother Name Modification event        Private voidTxbmothername_textchanged (Objectsender, EventArgs e) {            //Save the value into a custom propertyMothername.value =Txbmothername.text; }        //Mother's phone number modification event        Private voidTxbmothertel_textchanged (Objectsender, EventArgs e) {            //Save the value into a custom propertyMothertelnumber.value =Txbmothertel.text; }

As you can see from the code above, the main implementation idea is to initialize the contact window, get the value from the custom property to initialize the value of the control in the form, and if the custom property does not exist, You need to add the custom attribute to the ItemProperties collection to ensure that the custom property is not empty, and if the custom attribute exists, the control in the form is initialized by getting the value directly in the custom attribute. Finally, you register the modification events for each control in the form, saving the changes in the values in the control to the custom properties so that you can get the modified data the next time you open the contact .

So our design is finished, then look at the program's running results (press F5 to run the program):

The first window that appears to run is:

When you click New Contact, the Edit contact form appears, and you can see our custom form in the form:

Enter the contact information, click the Save and Close button, then we open the contact, modify the "Father Phone" to "123456" (previously 123456789), click the Save and Close button, when we open the contact again next time, you can get the modified data information:

This completes the requirements we started with, of course, there are a lot of deficiencies, such as the lack of verification of the phone number (such as in the Phone Number text box to enter the Chinese or non-existent number, or not a 11-digit number, etc.).

Iv. Summary

Here, the content of this topic is finished, I hope that through this topic can help you understand how to use VSTO technology to customize our Outlook form and our Outlook interface (custom Outlook interface and custom Excel interface is very similar, specific operation can refer to topic III). In the next topic, we will show you how to automate programming for office.

This topic Source: Http://files.cnblogs.com/zhili/VSTOOutlook.zip

VSTO Tour Series (v): Create an Outlook solution

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.