Techniques - DevSource
DevSource: Microsoft Developer Resource DevSource Home Sponsored by Microsoft Home Add Ons Architecture Languages Techniques Using VS Forums
Home arrow Techniques arrow Page 5 - RibbonX for Dummies: Chapter 6 (Part 2)
RibbonX for Dummies: Chapter 6 (Part 2)
By John Mueller

Rate This Article: Add This Article To:

RibbonX for Dummies: Chapter 6 (Part 2)
( Page 5 of 6 )

Adding the user information

When the user reaches the Employee Information group, the Ribbon-handling code becomes a little boring. All that it does is pass the request to the ThisAddIn class, using code like this:

public void EmpName(Office.IRibbonControl control)

{

// Insert the data into the field.

Globals.ThisAddIn.InsertUserName();

}

Of course, if you fail to include any of these little glue-code calls, a feature of your application will fail to work. However, most of the activity occurs in the ThisAddIn class; there you begin by adding an Outlook reference. As with the letter-and-memo example, this example relies on a user entry within Outlook to supply user information that Word can't. To add the required reference, right-click References in Solution Explorer and choose Add Reference from the context menu to display the Add Reference dialog box, shown in Figure 6-14.

ADVERTISEMENT

Locate the Microsoft Outlook 12.0 Object Library entry, shown in Figure 6-14, and click OK. You'll see the Outlook reference added to the References folder of your project. Rather than type the long list of object names for Outlook in your code, however, make sure your Imports or using statement looks like the one shown here:

using Outlook = Microsoft.Office.Interop.Outlook;

After you have the required reference in place, you can begin creating the methods for handling the user entries. These entries fall into two categories. The username is a value that you can obtain from Word, so the example uses this information directly. In addition, you have to have the username before you can locate the remaining data in Outlook. The remaining entries all come from the user's address book entry in Outlook. Listing 6-14 shows examples of both kinds of code.

Figure 6-14: Add a reference to the Outlook object library.

 

Listing 6-14: Obtaining User Information and Placing It in Fields

public void InsertUserName()

{

// Obtain the username.

string UserName = Application.UserName;

// Define the field to fill.

object SelectEmpReq = (object)"EmpName";

// Insert the data into the field.

Application.ActiveDocument.FormFields.get_Item(

ref SelectEmpReq).Result = UserName;

// Locate the user in the Outlook address book.

Outlook._Application ThisOutlook =

new Outlook.Application();

foreach (Outlook.AddressEntry UserEntry in

ThisOutlook.Session.AddressLists[1].AddressEntries)

{

if (UserEntry.Name == Application.UserName)

{

UserOutlookEntry =

ThisOutlook.Session.GetRecipientFromID(

UserEntry.ID);

return;

}

}

}

public void InsertUserID()

{

// Get the user identifier, which is stored in the

// CustomerID field.

string UserID =

UserOutlookEntry.AddressEntry.GetContact().CustomerID;

// Define the field to fill.

object SelectEmpReq = (object)"EmpID";

// Insert the data into the field.

Application.ActiveDocument.FormFields.get_Item(

ref SelectEmpReq).Result = UserID;

}

The code begins by obtaining the username using a technique that looks very similar to VBA. The code also creates the SelectEmpReq variable, which contains the name of the field to work with on the form. Notice that this code isn't designed to work with a specific form — it works with any form that contains a field with the right name. Consequently, you can create any number of forms and use the same code to modify them all — as long as you use consistent field names. This single application could end up working with hundreds of forms.

After the code creates the required variables, it calls the Application. ActiveDocument.FormFields.get_Item() method to access the required field. The Result property lets you obtain or modify the value of the field. This particular piece of code is very much different from VBA, which points out a problem with simply moving your VBA code to Visual Studio and assuming you can make minor changes to it.

At this point, the code begins locating the user's information in Outlook. As a point of interest, compare this code to the VBA version of the code in Listing 6-7. You'll notice that the C# code is actually more compact and it executes faster than the VBA equivalent. The code begins by using a foreach loop to look at the UserEntry.Name property for each user in the address book and compare it to the Application.UserName property value. When the code finds a match, it assigns the information to the UserOutlookEntry using the ThisOutlook.Session.GetRecipientFromID() method, just as you would in VBA. When this code executes, you may see the dialog box shown in Figure 6-4, just as you would when the same code executes in VBA.

Now that the code has access to the UserOutlookEntry variable, it can use a simple technique to place values from Outlook into the form fields. The code relies on the same UserOutlookEntry.AddressEntry.GetContact() method call that you do in VBA; then it assigns the resulting value to the appropriate field, as it does for the username.



 
 
>>> More Techniques Articles          >>> More By John Mueller
 



Microsoft's Future: A Chat With Their CTO, Barry Briggs

Play Video >

All Videos >

Julia explores the Robotics Studio!

Read now >

Messages to Bill Gates!

Read now >

View Now
DevSource RSS FEEDS
XML Want an easy way to keep up with breaking tech news? And the Get DevSource headlines delivered to your desktop with RSS.