Visual Studio 2010!

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

 

DevSource.com: Your Source for Visual Studio on Facebook
ADVERTISEMENT
RibbonX for Dummies: Chapter 6 (Part 1)
By John Mueller

Rate This Article: Add This Article To:

RibbonX for Dummies: Chapter 6 (Part 1)
( Page 3 of 4 )

Adding a recipient

The Style group is an example of a modification that can take several forms, but the user can select only one form at a time. The Recipient group provides another sort of entry. The output of this group is positional. The recipient’s name is normally going to appear at the top of any document you create, so it always appears first in the document no matter when the user adds the name. Of course, the name does appear below any headings. The recipient’s address always appears after the name. Again, it doesn’t matter when the user adds the address. The technique that the example employs to ensure the document fidelity is to use special styles throughout the document. The code can then search for these styles and place content correctly regardless of position within the document.

The layout of the Recipient group also shows a preference for some options over others. The Recipient Name button is large to emphasize its importance. Every document type requires a recipient name. The Address and Account Number buttons appear next as smaller buttons because the user will need them for most, but not all, documents. A separator keeps optional buttons apart from the standard buttons. The user probably won’t need to include a telephone number or office location, in most cases, so these buttons appear separately and at the lowest priority.

Obtaining a recipient name is a little tricky; you have to consider where the user is most likely to obtain the required recipient information. Because many companies have Outlook (not Outlook Express), the example uses the address book from Outlook as a source of information. To begin working with Outlook, create a reference to the required object library in VBA. Select Tools?References, and you’ll see the References dialog box, as shown in Figure 6-3. Check the Microsoft Outlook 12 Object Library entry and click OK.

Figure 6-3: Add a reference to Outlook so your Word code can interact with it.

 

After you have the Outlook reference, you can begin interacting with Outlook using standard VBA programming techniques. Listing 6-5 shows the callback code for obtaining the recipient name. This code also creates the ThisRecipient object that the rest of the recipient features use to obtain information about the recipient.

Listing 6-5: Obtaining Recipient Information from Outlook

'Callback for RcptName onAction
Sub RecipientName(control As IRibbonControl)
' Create a reference to the Outlook Address list.
Dim SelName As Outlook.SelectNamesDialog
Set SelName = _
Outlook.Application.Session.GetSelectNamesDialog
' Obtain the current pane object.
Dim CurrPane As Pane
Set CurrPane = Application.ActiveWindow.ActivePane
' Allow the user to select only one name.
SelName.AllowMultipleSelection = False
' Remove the standard email fields.
SelName.SetDefaultDisplayMode olDefaultSingleName
' Display the selection dialog box.
SelName.Display
' Make sure the user selected one and only one
' name.
If SelName.Recipients.Count = 1 Then
' Store the recipient information for later
' use.
Set ThisRecipient = SelName.Recipients.Item(1)
Else
' Display an error message.
MsgBox “You must select one recipient.”
' Leave the Sub.
Exit Sub
End If
' Go to the beginning of the document.
CurrPane.Selection.GoTo wdGoToLine, wdGoToFirst
' Check for a special header.
If CurrPane.Selection.Style = “Special Header” Then
' Move past the special header.
CurrPane.Selection.GoToNext wdGoToLine
End If
' Add a Special Heading paragraph.
CurrPane.Selection.InsertParagraph
CurrPane.Selection.Style = “Recipient Name”
' Insert the recipient’s name.
CurrPane.Selection.EndKey
CurrPane.Selection.Text = ThisRecipient.Name
CurrPane.Selection.GoToNext wdGoToLine
End Sub

The code begins by creating an instance of the SelectNamesDialog, which provides access to the Outlook address book from inside Word. Because the application is interested in obtaining a single name only, it sets the AllowMultipleSelection property to False. In addition, it removes the e-mail fields by setting the SetDefaultDisplayMode property to olDefaultSingleName. When using the default settings, the user sees the security dialog box, shown in Figure 6-4, when the code calls SelName.Display.

Notice that you can check Allow Access For and set a time interval to avoid seeing the message more than once. The longest interval you can set is 10 minutes, which provides sufficient time to perform all of the configuration tasks for this application. After the user approves the Outlook Access, the application displays the Select Name: Contacts dialog box, as shown in Figure 6-5. (The figure shows just one name, a test contact used for this example.) Even with all of the precautions taken with the Select Name: Contacts dialog box, the user can still choose to click OK without actually selecting a contact. Consequently, the code verifies that the user has selected precisely one entry.

Figure 6-4: Outlook displays this security dialog box when using the default settings.

 

The data placement comes next. The code begins by placing the insertion point at the beginning of the file. It then looks for the Special Header style in the document. The document can have only one (or no) Special Header paragraph when the user uses the application. That’s because selecting another style automatically removes any other paragraph that uses the Special Header style. When the code detects a Special Header paragraph, it moves past it.

The code inserts a new paragraph at this point and selects the Recipient Name style for it. You have to move the insertion point because Word selects the entire paragraph when you insert a new one, so the code calls CurrPane. Selection.EndKey. The actual data appears as part of ThisRecipient. Name. The code ends by placing the insertion point on the next line. Using ThisRecipient as global storage for the recipient information saves time. The other entries that the user can select work with this variable, rather than accessing Outlook directly. Consequently, adding other entries is simply a matter of accessing the correct data in ThisRecipient. For example, when the user wants to insert the recipient address, the code accesses the ThisRecipient.AddressEntry.GetContact.BusinessAddress property. All of the positioning depends on searching for particular styles and then manipulating the insertion pointer as needed to ensure accurate placement of new data. Figure 6-6 shows how typical recipient information might appear. Notice the use of styles for each data type in the style bar on the left side of the display.

Figure 6-5: The user can select any contact in their address list.

 



 
 
>>> More ASP and .Net Coding Techniques Articles          >>> More By John Mueller