Xem mẫu

  1. [ Team LiB ] Creating Custom Forms Although the standard Outlook forms are often good enough, there are times when you'll wish the form had different fields. You might want to remove some of the default fields, add some of your own, or just rearrange what's there. You might want to create a boilerplate message to use in response to inquiries, or pre- address a message form to a group of people with whom you often correspond. You can do all of this and more using custom forms. You can create templates and forms containing boilerplate text by editing the fields in the form. But when you want to add or remove fields or use scripting with the form, you need to use Design mode to open Outlook forms so that they can be edited. You don't need a special program to use Design mode; it's part of every form included with Outlook and is enabled when you tell Outlook you want to design a form. You can open a form in Design mode two ways: by using Tools, Forms, Design a Form and selecting a form from the dialog; or when a form is already open, by using its Tools, Forms, Design this Form menu option. In any case, when it's a message form, it will look similar to Figure 20.1. Figure 20.1. A message form in Design mode. Hidden pages have parentheses around their names. Drag fields from the Field Chooser to add them to a form or create your own fields.
  2. You need to use Outlook as your email editor to design email forms or templates. When Word is your editor, you can't go into Design mode for email forms. When forms are in Design mode, the Form Design toolbar is visible (see Figure 20.2). This toolbar has many of the design tools you'll use the most. The Form and Layout menus also contain design tools. Figure 20.2. The Forms toolbar has many of the commands you'll need when you customize your forms.
  3. Customize the Forms toolbar to add the Run This Form button to it. Doing so makes it easier to test your forms as you design them. Along with the Form Design toolbar, you'll also use the Field Chooser and Control Toolbox to create your custom forms. Right-click on any control to access the Properties and Advanced Properties dialogs. The Properties dialog contains display, value, and validation information for the control, whereas Advanced Properties has more advanced display properties. To design forms, drag controls from the Control Toolbox or fields from the Field Chooser and configure the properties. Select the control and drag it to the form, dropping the control when the mouse cursor is over the area where you want the control placed. When you drag fields to the form, the fields are automatically arranged on the left edge of the form. A feature many people request is to list a person's age on the contact form, so I'll show you how to create a custom form with the contact's age on it. Task: Design a Form Designing a custom form is actually pretty easy, at least for simple forms. You can customize any Outlook form, except for message forms, when you use Word as the editor. 1. Open the Design Form dialog using the Tools, Forms, Design a Form Menu (see Figure 20.3). The Standard form library contains the form types that can be customized. Figure 20.3. The Design Form dialog lists all forms currently installed on your computer or in your Outlook message store.
  4. Outlook's notes, known in form language as IPM.StickyNote, are not customizable and can't be used for custom forms. You can save notes to use as templates. 2. Select a form to edit or customize. For this example, I'm going to choose a contact form to open (see Figure 20.4). Figure 20.4. A contact form in Design mode.
  5. The first page of each of the appointment, journal, and task forms is not customizable. The All Fields, Properties, and Actions tabs are not customizable on any form. Editable pages have a grid to help you align the controls and page names in parentheses indicate that the page is hidden on published forms. The Field Chooser lists all available fields that can be used on the forms and displays when a form opens in Design mode. You can create new fields if you need them (click the New button). You won't see the picture control when you're designing a custom form but it remains on the forms unless you change the first page of a custom contact form. Making any changes to the first page removes the picture control and the form reverts to the style used by Outlook 2002 and older versions. If you need to add fields and want to keep the new look of the form, add the fields to another page.
  6. 3. Our age form uses a TextBox to display a formula that calculates a person's age, so we need to choose the Control Toolbox button to display the controls. Because I like the contact form with the picture control, I'm going to add the Age field to (P.2). When the first control is dropped on the page, the parentheses disappear. 4. Drag a Label and a TextBox control to the form. These are the second and third icons in the Control Toolbox, identified by the uppercase A icon and the lowercase ab icon. 5. Right-click on the control identified as Label1 and choose Properties (see Figure 20.5). Figure 20.5. Change the properties of a control using the Properties sheet. In this case, we're leaving the label named Label1 and changing the caption to Current Age. You can change other properties, such as the font and colors. Because this field is used as the name for the TextBox, we don't need to change anything on the other tabs. 6. Right-click on the TextBox and choose Properties. Change the Name to txtAge
  7. and select the Value tab. Click New to create a new field for Age, using Number for the Type: and changing Format: to All Digits (see Figure 20.6). Figure 20.6. Create a new field for displaying the age. 7. Add a check to the Set the Initial Value of This Field To: box and choose Edit. Enter the formula to calculate the age (see Figure 20.7): 8. IIf([Birthday]"None",DateDiff("yyyy",[Birthday],Date()) 9. -IIf(DateDiff("d",CDate(Month([Birthday]) & "/" & Day([Birthday]) & "/" & 10. Year(Date())),Date())
  8. 11. Choose Calculate This Formula Automatically and click the Value tab. Select the Validation tab and add a check to Include This Field for Printing and Save As. Click OK. A less accurate, but easier to type formula is (Now() - [Birthday] ) /365. This formula rounds to the contact's nearest birthday, with the age off by as much as six months. Now it's time to test your form. Choose Form, Run This Form to open a new form using your new form. Enter 1 test in the File As Field, select the Details tab, and type your birth date in the Birthday field. Save the form using File, Save (Ctrl+S) and check the Age field. You could add this to an appointment form that you use for birthdays and anniversaries. Does it show your age as of your last birthday? If the age is correct, congratulations: You created your first custom form. If the age is not correct, check the formula for typographical errors. If you want to copy the formula and paste it into the field, the formula and sample forms are at http://www.outlook-tips.net/samples.htm. Before publishing or saving the form, you'll want to change the tab name from P.2 to something more informative. Choose Form, Rename Page and enter a new name for your page.
  9. The new page uses an ugly gray color. You can change this by right-clicking on the Current Age label and choosing Advanced Properties. Select the BackColor field, type 00f7f7f7 in the text box at the top of the form, and then click Apply, as shown in Figure 20.6. This changes the label field to the silver color used on the other pages. You can double-click on the BackColor property to open the color picker, if you want to use a different color. Click again anywhere on the page to switch to the Properties dialog for the page (see Figure 20.8). Change the BackColor property to 00f7f7f7, apply it, and close the Properties dialog. Figure 20.8. Use the Advanced Properties dialog to change the display properties for the control. The final step before saving the form is entering a version number. This helps to prevent corruption in the forms cache when you increment the version number after editing the form and republishing it using the same name. When you don't use a version number and republish the form, you might cause the form to become one-offed. One-offed means that the form uses a form definition stored with the form, not the global definition. This results in larger item sizes and the possibility of mismatched fields. Select the (Properties) tab and enter a number in the Version field. In most cases, you'll use 1, but any number will do. Enter a number in the Form Number field.
  10. Although it's not necessary, you can also select custom icons to use for the large and small icons. Choose a 32x32 icon for each size, with the large icon filling the 32x32 square and the small icon using a 16x16 image in the upper-left corner. Custom icons usually won't be visible on forms in your Outlook folders until you restart Outlook. Occasionally, you'll also need to delete C:\Documents and Settings\username\Local Settings\Application Data\Microsoft\ FORMS\FRMCACHE.DAT before the icons will be visible. If you can't find the folder, you have hidden files and folders enabled. Use Search and enable Search Hidden Files and Folders under More Advanced Options. The other options you can configure on the dialog include password protecting the form so that others can't customize it and sending the form definition with the item. Check Send Form Definition when you are creating a form you're going to share with other people, or when you use Exchange Server and didn't publish the form to the Organizational Forms library. When it's enabled, the complete form is sent, resulting in a larger item but enabling the recipient to see and use your customizations. If you don't send the form definition, Outlook uses the default form to display the item. Although you can create many nice custom forms using only controls and fields, at some point you might need code to do what you want the form to do. You'll need to know how to write VBScript, or at least know where to find code samples on the Internet. Task: Add a VB Script to a Form I'm going to customize another contact form, this time with a command button that enables you to add the time and date to the Notes field. 1. Open a new contact form in Design mode and click on the Control Toolbox toolbar button to display the toolbox. 2. Drag the CommandButton control to an empty area on the form. I'm putting it under the Address fields on my form (see Figure 20.9). Figure 20.9. Add the CommandButton to your form,
  11. 3. Right-click on the button and choose Properties. Enter a descriptive caption in the Caption field and click OK. 4. Choose Form, View Code to open the Script Editor. Enter the following script in the editor then click on the X to close the window: 5. Sub StampDate() 6. Item.Body = Now() & vbCrLf & vbCrLf & Item.Body 7. End Sub 8. Sub CommandButton1_Click() 9. Call StampDate() 10. End Sub 11. Choose Form, Run This Form to test your code. Pressing the command button should enter the current time and date. You'll find it's easier to use a third-party script editor or the Visual Basic IDE when you're working with complex scripts. You'll have better tools to verify your code and can copy and paste the finished script into the Script Editor window. Because the form contains code, it must be published. Outlook's security prevents the code from running. [ Team LiB ]