How to Programmatically Publish a Form

ID: Q186437


The information in this article applies to:


SUMMARY

This article provides code samples that show how the Outlook object model can be used to publish a form. The samples cover using Microsoft Visual Basic and Microsoft Visual Basic Scripting Edition (VBScript) to perform this task.


MORE INFORMATION

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 the Microsoft fee-based consulting line at (800) 936-5200. For more information about the support options available from Microsoft, please see the following page on the World Wide Web:

http://www.microsoft.com/support/supportnet/refguide/

Microsoft Outlook Form

The following steps create a custom Outlook form that programmatically publishes a form in the Inbox:
  1. Open a new mail message. On the Tools menu, click Forms and then click Design This Form.


  2. Click the (P.2) page of the form. On the Form menu, click Control Toolbox. Use the Control Toolbox to add a Command Button to page P.2.


  3. On the Form menu, click View code. Type or copy the following code into the VBScript code window of the form and then close the editor:
    
           Option Explicit
           Dim olNS
           Dim MyFolder
           Dim MyItem
           Dim MyForm
    
           Sub CommandButton1_Click()
              Set olNS = Item.Application.GetNamespace("MAPI")
              Set MyFolder = olNS.GetDefaultFolder(6) '6=olFolderInbox
              Set MyItem = Application.CreateItem(0)  '0=MailItem
              Set MyForm = MyItem.FormDescription
              MyForm.Name = "MyInboxForm"
              MyForm.PublishForm 3, MyFolder          '3=olFolderRegistry
              Item.Close 1                            '1=olDiscard
           End Sub 


  4. On the Tools menu, click Forms and then click Publish Form As. Select the Inbox as the destination, enter MyForm as the Display Name, and then click Publish. Choose either Yes or No when you are prompted about sending the form definition with the item.


  5. Close and don't save changes to the form.


  6. On the Actions menu, click New MyForm.


  7. Click the P.2 page of the form and click the button.


This will open a new instance of MyForm and publish it to the Inbox as MyInboxForm. You can verify this by clicking the Actions menu and seeing New MyInboxForm listed at the bottom of the menu.

Visual Basic Project

Use the following steps to create a Microsoft Visual Basic 5.0 project to demonstrate how to programmatically publish a form into the Inbox:
  1. Create a new standard Visual Basic project.


  2. Create a reference to the Outlook 98 Type Library.


  3. Add a command button to the Form.


  4. Type or paste the following code into General Declarations section of the form code window.
    
          Option Explicit
          Dim olApp As Outlook.Application
          Dim olNS As Outlook.NameSpace
          Dim olFolder As Outlook.MAPIFolder
          Dim olItem As Outlook.MailItem
          Dim myForm As Outlook.FormDescription
    
          Private Sub Command1_Click()
             Set olApp = CreateObject("Outlook.Application")
             Set olNS = olApp.GetNamespace("MAPI")
             olNS.Logon , , True, True
             Set olFolder = olNS.GetDefaultFolder(olFolderInbox)
             Set olItem = olApp.CreateItem(olMailItem)
             Set myForm = olItem.FormDescription
             myForm.Name = "MyInboxForm"
             myForm.PublishForm olFolderRegistry, olFolder
             Unload Me
          End Sub 


  5. When you run this Visual Basic application, it publishes a new form in the Inbox and gives it a new name.



REFERENCES

For information concerning a problem with the PublishForm method, please see the following article in the Microsoft Knowledge Base:

Q222526 OL98: Outlook Doesn't Exit After Using PublishForm Method
For more information about creating solutions with Microsoft Outlook 98, please see the following articles in the Microsoft Knowledge Base:
Q180826 OL98: Resources for Custom Forms and Programming

Q182349 OL98: Questions About Custom Forms and Outlook Solutions
For more information about how to reference other items and folders, please see the following articles in the Microsoft Knowledge Base:
Q182614 OL98: Programming Examples for Referencing Items and Folders
Search the Microsoft Developer Network (MSDN) Library and the Microsoft Outlook Visual Basic for Applications help file (Vbaoutl.hlp) for the following keywords: Outlook FormDescription

Additional query words: OutSol OutSol98


Keywords          : kbdta kbdtacode OffVBS 
Version           : WINDOWS:
Platform          : WINDOWS 
Issue type        : kbhowto 

Last Reviewed: August 2, 1999