Microsoft KB Archive/290803

= OL2002: How to Open a Form from a Toolbar Button =

Article ID: 290803

Article Last Modified on 11/23/2006

-

APPLIES TO


 * Microsoft Outlook 2002 Standard Edition

-



This article was previously published under Q290803





SUMMARY
This article explains how to use Outlook Visual Basic for Applications to open a custom Outlook form directly from the Outlook toolbar.

NOTE: You cannot use the code in this article to open forms created using the Exchange Forms Designer or other MAPI forms created using C++. The Items.Add method in the Outlook object model only works with Outlook forms.



MORE INFORMATION
You can create a Visual Basic for Applications macro that opens a custom form. You can then assign the macro to a toolbar button or a menu so that the form can be easily opened.

To Create a Custom Form
If you already have a custom form stored in your Inbox and know the message class of the form, you can skip this section and go to the next section.
 * 1) In the Folder List, click the Inbox.
 * 2) On the Tools menu, point to Forms, and click Design a Form.
 * 3) Click Message, and then click Open.
 * 4) Click the (P.2) page of the form.
 * 5) If the Control Toolbox is not visible, on the Forms menu, click Control Toolbox.
 * 6) Select a control from the Control Toolbox and drag it to the form.
 * 7) On the Tools menu, point to Forms, and then click Publish Form.
 * 8) In the Look in list, select Inbox.
 * 9) For the display name, type FormsTest, and then click Publish. Note that the message class is IPM.Note.MyForm.
 * 10) Click Yes to select the Save Form Definition with Item check box.
 * 11) Close the form and do not to save it.

To Create the Visual Basic for Applications Code
Microsoft provides programming examples for illustration only, without warranty either expressed or implied, including, but not limited to, the implied warranties of merchantability and/or fitness for a particular purpose. This article assumes that you are familiar with the programming language being demonstrated and the tools used to create and debug procedures. Microsoft support professionals can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific needs. If you have limited programming experience, you may want to contact a Microsoft Certified Partner or the Microsoft fee-based consulting line at (800) 936-5200. For more information about Microsoft Certified Partners, please visit the following Microsoft Web site:

https://partner.microsoft.com/global/30000104

For more information about the support options that are available and about how to contact Microsoft, visit the following Microsoft Web site:

http://support.microsoft.com/default.aspx?scid=fh;EN-US;CNTACTMS

To create the code to open the form, follow these steps:  On the Tools menu, point to Macro, and click Visual Basic Editor. In the Project - Project1 pane, double-click Project1, and double-click Microsoft Outlook Objects. Double-click ThisOutlookSession to open a code window.  In the code window, enter the following code: Sub DisplayForm Set myFolder = Session.GetDefaultFolder(olFolderInbox) Set myItem = myFolder.Items.Add(&quot;IPM.Note.MyForm&quot;) myItem.Display End Sub This code assumes that the name of your form is MyForm. Be sure to modify the third line of code if your form's message class is different.

 On the File menu, click Save VBAProject.OTM. Close the Visual Basic Editor.</li></ol>

To Create the Toolbar Button

 * 1) On the View menu, point to Toolbars, and click Customize.
 * 2) On the Commands tab, in the Categories list, click Macros. The macro will be listed as Project1.ThisOutlookSession.DisplayForm
 * 3) Drag the macro name to a toolbar.
 * 4) With the toolbar button selected, click Modify Selection, to make any desired changes to the appearance of the button.
 * 5) Click Close.

To Modify the Code
This code can be used with any mail message form, regardless of whether it is published in the Personal Forms Library, Organizational Forms Library, or the Inbox folder. However, other types of forms are typically published in a folder and the resultant items are saved to that folder. This would typically be the case if you create a custom contact form, for example.

You may need to alter the code so that the custom form can be found in a specific folder (the folder it was published in) and so the item is saved in the correct folder; the following line of code is critical to this:

Set myFolder = Session.GetDefaultFolder(olFolderInbox) This line of code assumes that you want to use the default Inbox folder. If you want to use a different folder, substitute one of the following Outlook constants for olFolderInbox:


 * olFolderDeletedItems
 * olFolderOutBox
 * olFolderSentMail
 * olFolderCalendar
 * olFolderContacts
 * olFolderJournal
 * olFolderNotes
 * olFolderTasks

The code also assumes that the folder you want to use is one of the default set of folders. The default set of folders are those folders that are at the same level as the Inbox which receives incoming mail. However, if you want to use a custom form that is published in another folder, you must modify the code appropriately.

For more information on how to reference other folders in Outlook, please see the &quot;Referencing Existing Folders&quot; section of the following article in the Microsoft Knowledge Base:

290804 OL2002: Programming Examples for Referencing Items and Folders

Examples of Opening Other Forms
To open a default contact item, change the code to: Sub DisplayForm Set myFolder = Session.GetDefaultFolder(olContactFolder) Set myItem = myFolder.Items.Add(&quot;IPM.Contact&quot;) myItem.Display End Sub To open a custom form with a message class of IPM.Note.Test that is published in the Test folder located in the default Inbox folder, change the code to:

Sub DisplayForm Set myFolder = Session.GetDefaultFolder(olFolderInbox).Folders(&quot;Test&quot;) Set myItem = myFolder.Items.Add(&quot;IPM.Note.Test&quot;) myItem.Display End Sub To open a custom form with a message class IPM.Appointment.Test that is published in the Personal Forms Library, or the Organizational Forms Library, change the code to:

Sub DisplayForm Set myFolder = Session.GetDefaultFolder(olFolderCalendar) Set myItem = myFolder.Items.Add(&quot;IPM.Appointment.Test&quot;) myItem.Display End Sub To open a custom form in a Microsoft Exchange public folder titled My Public Folder, located at the top of the public folder hierarchy, use the following code:

Sub DisplayForm Set myFolder1 = Session.Folders(&quot;Public Folders&quot;) Set myFolder2 = myFolder1.Folders(&quot;All Public Folders&quot;) Set myFolder3 = myFolder2.Folders(&quot;My Public Folder&quot;) Set myItem = myFolder3.Items.Add(&quot;IPM.Appointment.Test&quot;) myItem.Display End Sub

To Open the Choose Forms Dialog Box
Instead of creating Visual Basic for Applications code to open a form, you may prefer to open the Choose Forms dialog box from a toolbar button. To do this without programming, follow these steps:


 * 1) On the View menu, point to Toolbars, and click Customize.
 * 2) On the Commands tab, under Categories, click File.
 * 3) In the list of Commands, drag Choose Form... to a toolbar.
 * 4) Click Close.

<div class="references_section">