RibbonX for Dummies: Chapter 6 (Part 2) (
Page 1 of 6 )
RibbonX For Dummies®
Chapter 6: Developing Business Applications for Word
ISBN: 978-0-470-16994-0
Copyright of Wiley Publishing, Inc.
Indianapolis, Indiana
Posted with Permission
Automating Envelopes
Many of the Office 2007 features are generic and some are a little unintuitive
for the complete novice to use. Two such examples are envelopes and labels.
You can see the buttons for each on the Mailings tab with the Create group.
You have a number of decisions to make when you change or augment an
existing feature. Look again at the Letter/Memo tab in Figure 6-1. You may
simply decide to place the Create group on the Mailings tab there and leave
the original in place as well. Adding an existing group, with complete functionality,
to a custom tab is easy. Simply add the group name, as shown here:
<group idMso="GroupEnvelopeLabelCreate" />
Remember, however, that you have to keep the whole workflow issue in mind
when you work out the details of an application that uses existing features.
You may decide that you really don't want users creating labels within the
Letter/Memo tab because they won't ordinarily perform that task for a single
letter. It's always possible to use just part of a group. For example, you might
decide to display the Envelopes button using XML like this.
<group id="MailIt" label="Create">
<button idMso="EnvelopesAndLabelsDialog"
size="large"/>
</group>
Notice that you still must define a size for the button. Otherwise you can't
change anything about the Envelopes button — which means you still can't
control the behavior. Unfortunately, there's a problem with the Envelopes
dialog box. When you click the button, the Delivery Address field is blank
unless you know the secret of highlighting the text you want to see in the
output. The user also has to know details — such as the kind of envelope
loaded in your printer and whether that envelope has your address preprinted
on it. In fact, there are a hundred ways in which the user can create a
pile of partially used and useless envelopes.
To overcome this problem (and keep the user from choosing the Envelopes
option on the Mailings tab), you can repurpose the control. Normally you
won't want to repurpose the default behavior of a control, but in this case
you're actually making things easier for the template user. Here's the
<commands> element you add to override the existing envelope functionality:
<commands>
<command idMso="EnvelopesAndLabelsDialog"
onAction="CreateEnvelope" />
</commands>
Of course, now you have to duplicate the functionality that the Envelopes
and Labels dialog box provides using other techniques. Fortunately, Word
provides the Application.ActiveDocument.Envelope object you can
use to provide programmatic support for creating an envelope. Listing 6-10
shows how you can use this feature with the Letter/Memo tab.
Listing 6-10: Creating Custom Envelope Output
'Callback for EnvelopesAndLabelsDialog onAction
Sub CreateEnvelope(control As IRibbonControl, _
ByRef cancelDefault)
'Create an envelope reference.
Dim ThisEnvelope As Envelope
Set ThisEnvelope = _
Application.ActiveDocument.Envelope
' Set the envelope size.
ThisEnvelope.DefaultSize = "Size 10"
' Omit the return address.
ThisEnvelope.DefaultOmitReturnAddress = True
' Obtain the current pane object.
Dim CurrPane As Pane
Set CurrPane = Application.ActiveWindow.ActivePane
' Define a search.
Dim DoSearch As Find
Set DoSearch = CurrPane.Selection.Find
' Holds the address range start.
Dim AddressStart As Long
' Search for the start of the range.
DoSearch.Wrap = wdFindContinue
DoSearch.Text = ""
DoSearch.Style = "Recipient Name"
If DoSearch.Execute() Then
AddressStart = CurrPane.Selection.Range.Start
End If
' Search for the end of the range.
DoSearch.Wrap = wdFindContinue
DoSearch.Text = ""
DoSearch.Style = "Recipient Address"
While DoSearch.Execute()
Wend
' Reset the address start.
CurrPane.Selection.MoveStart _
wdCharacter, AddressStart - _
CurrPane.Selection.Start
' Output the envelope.
ThisEnvelope.PrintOut
' Go to the beginning of the document.
CurrPane.Selection.GoTo wdGoToLine, wdGoToFirst
End Sub
The example begins by creating an Envelope object, ThisEnvelope, based
on the default envelope for the active document. The resulting object contains
all of the system defaults (which may or may not meet your printing
requirements). You should always check essentials such as the envelope
size because you don't know whether the user has changed these entries.
In this case, the example sets the envelope size and omits that return
address. The actual power of workflow applications, however, is that you can
more easily control the data that the application puts out. As shown in the
listing, the application finds the beginning and ending point of the recipient
information based solely on the document styles. It then selects the entire
range and calls ThisEnvelope.PrintOut to print the envelope.
If you decide to include the envelope information as part of the document,
you can also use ThisEnvelope.Insert. The envelope information
appears at the top of the document in a separate section.