Newsgroup: it.comp.lang.visual-basic
Titolo: Outlook & VB

Ho notato che l'argomento interessava molti.
Io ho risolto un po' dei miei problemi scrivendo alla Micro$ft
msh...@microsoft.com
 .Inserendo nell'oggetto questi codici: Q181202,
Q182614, Q182394, Q182349, Q180826 ed ottenendo delle lettere con
spiegazioni e codice d'esempio.
Oggetto: Q181202
How to Automate Outlook from Another Program
Oggetto: Q182614
Programming Examples for Referencing Items and Folders
Oggetto: Q182394
How to Use Command Bars in Outlook Solutions
Oggetto: Q182349
Questions About Custom Forms and Outlook Solutions
Oggetto: Q180826
Resources for Custom Forms and Programming

Io ho risolto un po' dei miei problemi scrivendo alla Micro$ft......
ciao,
sembra che a me la M$ non voglia spedire nulla :-(
ti scoccia postarmi le mail ?? mi interesserebbero molto !
thanks
Daniele

Se puoi interesserebbero molto anche a me!
Ciao

From a developer's point of view, the main window in Outlook (the
"application window") is referred to as "Explorer" and the form windows are
referred to as "inspectors." Both types of windows have command bars
(including both menus and toolbars). When working with the Outlook command
bars, it is always important to make sure you are working with the correct
set of command bars.
- Use the Explorer objects (ActiveExplorer/GetExplorer) to refer to
   the main application window in Outlook.
- Use the Inspector objects (ActiveInspector/GetInspector) to refer to an
   Outlook item's window.
                  To reference              To create a
   Window type    an open window, use:      new window, use:
   ------------------------------------------------------------
   Application    ActiveExplorer method     GetExplorer method
   Item/Form      ActiveInspector method    GetInspector method
Syntax for Referencing a Command Bar
------------------------------------
Use the following simplified syntax in both Microsoft Visual Basic for
Applications and Microsoft Visual Basic Scripting Edition (VBScript):
   Set MyCB = <Inspector object>.CommandBars.Item("Menu Bar")
The following syntax may not work in all cases:
   Set MyCB = <Inspector object>.CommandBars("Menu Bar")
VBScript Example to Execute an Outlook Command
----------------------------------------------
You can use the following sample code with a Command Button on a contact
form so that you can easily create a new letter for a contact:
   Sub CommandButton1_Click()
      Set MyCB = Item.GetInspector.CommandBars.Item("Menu Bar")
      Set MyMenu = MyCB.Controls("Actions")
      Set MyMenuItem = MyMenu.Controls("New Letter to Contact")
      MyMenuItem.Execute
   End Sub
REFERENCES
==========
For more information about using command bars in Outlook 98 solutions,
please see the following articles in the Microsoft Knowledge Base:
   ARTICLE-ID: Q184791
   TITLE     : OL98: How to Programmatically Synchronize Folders
   ARTICLE-ID: Q186471
   TITLE     : OL98: How to Programmatically Change Displayed Calendar Date
For more information about creating solutions with Microsoft Outlook 98,
please see the following articles in the Microsoft Knowledge Base:
   ARTICLE-ID: Q180826
   TITLE     : OL98: Resources for Custom Forms and Programming
   ARTICLE-ID: Q182349
   TITLE     : OL98: Questions About Custom Forms and Outlook Solutions
Keywords          : kbdta kbdtacode OffVBA
Version           : WINDOWS:98
Platform          : WINDOWS
Issue type        : kbhowto

Resources for Custom Forms and Programming
---------------------------------------------------------------------
The information in this article applies to:
- Microsoft Outlook 98
---------------------------------------------------------------------
SUMMARY
=======
This article describes where to find information on customizing
Microsoft Outlook 98 forms and creating solutions with Outlook.
MORE INFORMATION
================
Outlook supports custom forms, an object model, and Visual Basic Scripting
Edition (VBScript). Information on how to automate and customize Outlook to
meet your needs is available from several sources. This article discusses
the following resources:
- Outlook Forms and Visual Basic Help Files
- The Outlook Object Model
- The VBScript Web Site
- The Outlook Developer Forum Web Site
- Newsgroups
- Microsoft Press Books
- Microsoft Support Options
- Solution Providers
NOTE: Because the Microsoft Web site is constantly updated, the site
addresses in this article may change without notice. If this occurs, link
to the Microsoft home page at the following address and search for your
topic:
 
http://www.microsoft.com/
Outlook Forms and Visual Basic Help Files
-----------------------------------------
Outlook provides two related Help files, Olform.hlp and Vbaoutl.hlp.
To install these files, you must first install Outlook and then run
Setup to install the additional development tools.
For more information about installing these help files, please
see the following article in the Microsoft Knowledge Base:
   Article-ID: Q183220
   Title     : OL98: How to Install Visual Basic Help
To access these Help files after they're installed, on the
Help menu click Contents And Index. The Help files will be shown
towards the bottom of the Contents page as "Outlook Customization and
Forms" and "Microsoft Outlook Visual Basic Reference."
If you use these files frequently, consider creating a shortcut to
Olform.hlp and placing it on your desktop or Start menu. When you directly
open Olform.hlp, the Contents page will include the Visual Basic Reference
topics.
The Outlook Object Model
------------------------
Chapter 5 of the "Microsoft Office 97/Visual Basic Programmer's Guide"
provides an overview of the Outlook object model, working with Outlook
folders, items, and events, and using automation and VBScript. While not
written specifically for Outlook 98, most of the features of the object
model have remained the same, except for features added in Outlook 98. The
Programmer's Guide is included with the Microsoft Office 97, Developer's
Edition. The Office 97/Visual Basic Programmer's Guide is available at:
 
http://www.microsoft.com/OfficeDev/Articles/Opg/
The VBScript Web Site
---------------------
The Microsoft Scripting web site provides frequently asked questions,
sample code, and free downloads for people using the VBScript language. The
VBScript Web site is available at:
 
http://msdn.microsoft.com/scripting/
The Outlook Developer Forum Web Site
------------------------------------
The Microsoft Outlook Developer Forum provides technical information,
resources, sample code, and tips for those creating Outlook automation and
form solutions. The Outlook Developer Forum is available at:
 
http://www.microsoft.com/OutlookDev/
The Microsoft Developer Network Library
---------------------------------------
If you are developing enterprise-level solutions that involve Microsoft
Exchange, you should consult various Exchange and Messaging documentation
that is available at the Microsoft Developer Network Library site:
 
http://premium.microsoft.com/msdn/library/default.htm
Newsgroups
----------
Peer-to-peer newsgroups are available to help you interact with other
Outlook users. You can use any newsreader software to access these
newsgroups. The Microsoft news server name is 
msnews.microsoft.com
.
Information about all Microsoft newsgroups is available at:
 
http://support.microsoft.com/support/news
The following Outlook newsgroups focus on solution-related issues:
- Programming Forms: microsoft.public.outlook.program_forms
- Add-ins and Utilities: microsoft.public.outlook.addin_utility
Microsoft Press Books
---------------------
Microsoft publishes many books for Office programmers, but Outlook
developers should consider "Building Applications with Microsoft
Outlook 98" (ISBN 1-57231-718-3), by Microsoft Press. This book is
available at major book sellers. The previous version for Outlook 97
is out of print.
For information on offerings by Microsoft Press, consult the Microsoft
Press web site at:
 
http://mspress.microsoft.com
or call Microsoft Press at (800)MSPRESS
Microsoft Support Options
-------------------------
Microsoft provides a complete range of support options for Outlook
customers.
For more information about where to get direct support for developing
solutions with Microsoft Outlook 98, please see the following article
in the Microsoft Knowledge Base:
   Article-ID: Q185243
   Title     : OL98: Getting Support for Developing Outlook-related
               Solutions
Solution Providers
------------------
For information about Solution Providers in your local area, contact the
Microsoft Solution Provider Sales and Information line at 
(800)765-7768
in the US or 
(800)563-9048
 in Canada. For information about support in
other locations, contact a local Microsoft subsidiary.
REFERENCES
==========
For more information about common questions related to creating solutions
with Outlook 98, please see the following article in the Microsoft
Knowledge Base:
   Article-ID: Q182349
   Title     : OL98: Questions About Custom Forms and Outlook Solutions
Keywords          : kbdtacode
Version           : WINDOWS:98
Platform          : WINDOWS
Issue type        : kbinfo

Viste le richieste posto tutti i messaggi: buona lettura
Vi ricordo che per richiedere gli articoli basta scrivere all'indirizzo
msh...@microsoft.com
 ed inserire nell'oggetto il numero dell'articolo
richiesto. Ad esempio Q182394

How to Automate Outlook from Another Program
---------------------------------------------------------------------
The information in this article applies to:
- Microsoft Outlook 98
---------------------------------------------------------------------
SUMMARY
=======
This article provides an overview of programming Microsoft Outlook 98 using
Automation from another program.
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 engineers 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/default.asp
Automation (formerly OLE Automation) allows one program to control another
program by either issuing commands or retrieving information
programmatically. You can use the code examples in this article in
Microsoft Word 97, Microsoft Excel 97, Microsoft Visual Basic, or any other
program that supports Automation.
Early vs. Late Binding
----------------------
You can use either "early" or "late" binding to start an Automation
session. Late binding uses either the GetObject or CreateObject function to
initialize Outlook. For example, the following code sets an object to the
Outlook program, which is the highest level object in the Outlook object
model. All Automation code must first define an Outlook.Application object
in order to access any of the other Outlook objects below that.
   Dim objOL as Object
   Set objOL = CreateObject("Outlook.Application")
To use early binding, you first need to reference the available Outlook
object library. To do this from Visual Basic (VB) or Visual Basic for
Applications, follow these steps:
1. In the Visual Basic Editor, click References on the Tools menu.
2. Click to select the "Microsoft Outlook 8.5 Object Library" check box,
   and then click OK.
The object library file is Msoutl85.olb and installs to the C:\Program
Files\Microsoft Office\Office folder by default.
Once you reference the Outlook object library, you can use the following
syntax to start an Outlook session:
   Set ol = New Outlook.Application
Using early binding has two important advantages. First, code using early
binding runs faster than code using late binding (CreateObject/GetObject).
Second, because you reference the Outlook object library, you can get
online Outlook programming help using the object browser and Help system.
The Outlook Object Model
------------------------
The Outlook object model provides functionality to manipulate data stored
in Outlook folders. However, there is limited functionality available to
control Outlook itself. For example, you cannot use the object model to
change the Options settings on the Tools menu.
NOTE: As a possible workaround to limitations regarding the object model,
you can use the CommandBars object provided by Microsoft Office to execute
commands that are assigned to either toolbar buttons or menu commands. For
example, you can use the CommandBars object to execute the New Call command
(on the Dial submenu of the Tools menu) to bring up the New Call dialog
box.
For more information about using CommandsBars with Microsoft Outlook 98,
please see the following article in the Microsoft Knowledge Base:
   Article-ID: Q182394
   Title     : OL98: How to Use Command Bars in Outlook Solutions
Most programming solutions need to interact with the data stored in
Outlook. Outlook stores all of its information in Messaging Application
Programming Interface (MAPI) folders. Therefore, after you set an object
variable to Outlook.Application, you will commonly set a "Namespace" object
to MAPI:
   Set ol = New Outlook.Application
   Set olns = ol.GetNamespace("MAPI")
Once you set the Namespace object, you are ready to set the next object to
a folder within the MAPI Namespace. One common way of doing this is by
specifying Outlook's "default" folders, which are the folders at the same
folder level as the Inbox that receives incoming mail. The following code
will set the objFolder object to the default Contacts folder:
   Set ol = New Outlook.Application
   Set olns = ol.GetNamespace("MAPI")
   Set objFolder = olns.GetDefaultFolder(olFolderContacts)
For more information about referencing folders and items, please see
the following article in the Microsoft Knowledge Base:
   Article-ID: Q182614
   Title     : OL98: Programming Examples for Referencing Items and Folders
Once you are programmatically at the folder that contains the items you
want to either use or create, you must use appropriate code to accomplish
your programming task. See the section below for some common programming
examples.
Sample Code for Common Programming Tasks
----------------------------------------
Create a New Default Task Item:
   Sub CreateNewDefaultOutlookTask()
      Dim ol As Object
      Dim NewTask As Object
      ' Set the Application object.
      Set ol = New Outlook.Application
      ' You can only use CreateItem for default items.
      ' Vbaoutl.hlp lists other Outlook constants to create other items.
      Set NewTask = ol.CreateItem(olTaskItem)
      ' Display the new task form so the user can fill it out.
      NewTask.Display
   End Sub
Create a New Task Using a Custom Form:
   Sub CreateNewContactFromCustomForm()
      Dim ol As Object
      Dim olns As Object
      Dim objFolder As Object
      Dim AllContacts As Object
      Dim NewContact As Object
      ' Set the Application object.
      Set ol = New Outlook.Application
      ' Set the Namespace object.
      Set olns = ol.GetNamespace("MAPI")
      ' Set the default Contacts folder.
      Set objFolder = olns.GetDefaultFolder(olFolderContacts)
      ' Set objAllContacts equal to the collection of all contacts.
      Set AllContacts = objFolder.Items
      ' Add a new contact to the AllContacts collection using the
      ' "IPM.Contact.MyForm" form.
      Set NewContact = AllContacts.Add("IPM.Contact.MyForm")
      ' Display the new contact form.
      NewContact.Display
   End Sub
Sample Subroutine to Loop Through All the Default Contacts:
   Sub GetOutlookContacts()
      Dim ol As Object
      Dim olns As Object
      Dim objFolder As Object
      Dim objAllContacts As Object
      Dim Contact As Object
      ' Set the Application object.
      Set ol = New Outlook.Application
      ' Set the Namespace object.
      Set olns = ol.GetNamespace("MAPI")
      ' Set the default Contacts folder.
      Set objFolder = olns.GetDefaultFolder(olFolderContacts)
      ' Set objAllContacts equal to the collection of all contacts.
      Set objAllContacts = objFolder.Items
      ' Loop through each contact.
      For Each Contact In objAllContacts
         ' Display the Fullname field for the contact.
         MsgBox Contact.FullName
      Next
   End Sub
Resources for Outlook Automation
--------------------------------
The following list includes some important resources for automating
Outlook, located on the Microsoft Outlook Developer Forum:
- Microsoft Office 97 Developer Edition "Microsoft Office 97 Visual Basic
   Programmer's Guide," Chapter 5, "Microsoft Outlook Objects," 95-115,
   available on the Microsoft Web site:
 
http://www.microsoft.com/OfficeDev/Articles/Opg/005/005.htm
- "The Microsoft Outlook 97 Automation Server Programming Model,"
   available on the Microsoft Web site:
 
http://www.microsoft.com/OutlookDev/Articles/outprog.htm
REFERENCES
==========
For more information about creating solutions with Microsoft Outlook 98,
please see the following articles in the Microsoft Knowledge Base:
   Article-ID: Q180826
   Title     : OL98: Resources for Custom Forms and Programming
   Article-ID: Q182349
   Title     : OL98: Questions About Custom Forms and Outlook Solutions
Keywords          : kbdta kbdtacode OffVBS
Version           : WINDOWS:98
Platform          : WINDOWS
Issue type        : kbhowto

Programming Examples for Referencing Items and Folders
---------------------------------------------------------------------
The information in this article applies to:
- Microsoft Outlook 98
---------------------------------------------------------------------
SUMMARY
=======
The Microsoft Outlook 98 object model is commonly used to access various
types of items in folders. This article provides an overview of the various
methods, properties, and objects that can be used to refer to Outlook items
and folders.
This article summarizes the following topics:
Referencing Existing Folders
----------------------------
- GetDefaultFolder Method
- Folders Object
- Parent Property
- GetSharedDefaultFolder Method
- GetFolderFromID Method
Creating and Referencing New Folders
------------------------------------
- Folders.Add Method
Creating and Referencing New Items
----------------------------------
- CreateItem Method
- Items.Add Method
- CreateItemFromTemplate Method
Referencing Existing Items
--------------------------
- Using Items(I) or For Each...Next
- Using Items("This is the subject")
- Find Method
- Restrict Method
- GetItemFromID Method
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 engineers 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/default.asp
Note: Visual Basic Scripting Edition (VBScript) code must use the numeric
value of the constants that are defined in the Outlook object library. You
can find a listing of these values in the Microsoft Outlook object library
Help file (Vbaoutl.hlp) under the "Microsoft Outlook Constants" Topic.
Referencing Existing Folders
----------------------------
GetDefaultFolder Method:
Default folders are those that are at the same level as the Inbox that
receives incoming mail. If you have more than one Inbox in your profile,
pressing CTRL+SHIFT+I always selects the default Inbox. The default folders
are those that most users work with on a regular basis, such as the
Calendar, Contacts, and Tasks folders. You can easily refer to these
folders using the GetDefaultFolder method. GetDefaultFolder takes one
argument, which is the type of folder you want to refer to. The following
examples assign the object variable MyFolder to the default Contacts
folder:
   ' Automation code example.
   Set ol = New Outlook.Application
   Set olns = ol.GetNameSpace("MAPI")
   Set MyFolder = olns.GetDefaultFolder(olFolderContacts)
   ' VBScript code example.
   Set olns = Item.Application.GetNameSpace("MAPI")
   Set MyFolder = olns.GetDefaultFolder(10)
Folders Object:
You can use the Folders object to refer to any folder that is visible in
the Outlook folder list. This object is typically used to refer to an
Exchange public folder or any other folder that is not a default Outlook
folder.
The following examples illustrate how to refer to a public folder called
"My Public Folder." Note that you typically start at the top-most folder
and work your way down to the folder you need to reference. Also note that
the folder names are case-sensitive and must exactly match the names as
they appear in the Outlook folder list.
   ' Automation code example.
   Set ol = New Outlook.Application
   Set olns = ol.GetNameSpace("MAPI")
   Set MyFolder1 = olns.Folders("Public Folders")
   Set MyFolder2 = MyFolder1.Folders("All Public Folders")
   Set MyFolder3 = MyFolder2.Folders("My Public Folder")
   ' VBScript code example.
   Set olns = Item.Application.GetNameSpace("MAPI")
   Set MyFolder1 = olns.Folders("Public Folders")
   Set MyFolder2 = MyFolder1.Folders("All Public Folders")
   Set MyFolder3 = MyFolder2.Folders("My Public Folder")
The following examples illustrate how you can refer to a folder called
"Business Tasks," which is a subfolder of the default Tasks folder.
   ' Automation code example.
   Set ol = New Outlook.Application
   Set olns = ol.GetNameSpace("MAPI")
   Set MyTasksFolder = olns.GetDefaultFolder(olFolderTasks)
   Set MyFolder = MyTasksFolder.Folders("Business Tasks")
   ' VBScript code example.
   Set olns = Item.Application.GetNameSpace("MAPI")
   Set MyTasksFolder = olns.GetDefaultFolder(13)
   Set MyFolder = MyTasksFolder.Folders("Business Tasks")
Parent Property:
If you already have a reference to an Outlook item or folder, then you can
use its Parent property to create a reference to the folder the item or
folder is located in.
The following examples return the name of a folder for a particular item:
   ' Automation code example.
   Set ol = New Outlook.Application
   Set MyItem = ol.CreateItem(olMailItem) ' Create new item.
   MyItem.Save                            ' Save it to Inbox.
   Set MyFolder = MyItem.Parent           ' MyFolder = Inbox.
   ' VBScript code example.
   ' Returns the folder of the current item.
   Set MyFolder = Item.Parent
GetSharedDefaultFolder:
You can use this method if someone has given you delegate permissions to
one of their default folders. In addition, you must have already connected
to the folder in Outlook by clicking the File menu, clicking Open Special
Folder, and then clicking Exchange Server Folder.
For more information about accessing the folders of other people,
please see the following article in the Microsoft Knowledge Base:
   ARTICLE-ID: Q182642
   TITLE     : OL98: (CW) How to Open Someone Else's Calendar or
               Other Folder
Once Outlook is set up to use someone else's folder, the
GetSharedDefaultFolder method is used in the same fashion as
GetDefaultFolder, except you specify one additional argument -- the name of
the other person's folder you want to reference. This example first
resolves the other person's name to verify that it is a valid name that can
be used with the GetSharedDefaultFolder method.
   ' Automation code example.
   Set ol = New Outlook.Application
   Set olns = ol.GetNameSpace("MAPI")
   Set myRecipient = olns.CreateRecipient("John Smith")
   myRecipient.Resolve
   If myRecipient.Resolved Then
      Set JohnFolder = olns.GetSharedDefaultFolder _
         (myRecipient, olFolderContacts)
   End If
   ' VBScript code example.
   Set olns = Item.Application.GetNameSpace("MAPI")
   Set myRecipient = olns.CreateRecipient("John Smith")
   myRecipient.Resolve
   If myRecipient.Resolved Then
      Set JohnFolder = olns.GetSharedDefaultFolder _
         (myRecipient, 10)
   End If
GetFolderFromID:
This method would typically be used only in more complex solutions where a
solution keeps track of both the StoreID and EntryID of a folder so that it
can be quickly referenced at a later time.
For more information about using the GetFolderFromID method, please see
the following article in the Microsoft Knowledge Base:
   ARTICLE-ID: Q180980
   TITLE     : OL98: Programming with EntryIDs, StoreIDs and GetItemFromID
Creating and Referencing New Folders
------------------------------------
Folders.Add Method:
Using the Add method on the Folders collection allows you to create a new
folder. The first argument specifies the name of the folder and the second
argument specifies the type of folder. The following example adds a
Business Tasks folder under your default tasks folder. Because the folder
type is not specified, it will inherit the type of the parent folder.
   ' Automation code example.
   Set ol = New Outlook.Application
   Set olns = ol.GetNameSpace("MAPI")
   Set MyTasksFolder = olns.GetDefaultFolder(olFolderTasks)
   Set MyNewFolder = MyTasksFolder.Folders.Add("Business Tasks")
   ' VBScript code example.
   Set olns = Item.Application.GetNameSpace("MAPI")
   Set MyTasksFolder = olns.GetDefaultFolder(13)
   Set MyNewFolder = MyTasksFolder.Folders.Add("Business Tasks")
Creating and Referencing New Items
----------------------------------
CreateItem Method:
The CreateItem method creates a new default Outlook item. If you need to
create an item based on a custom form you have created, use the Items.Add
method below. The CreateItem method is conveniently located off of the top-
level application object in the Outlook object model. The method takes only
one argument, a constant indicating the type of item to create.
   ' Automation code example.
   Set ol = New Outlook.Application
   Set MyTaskItem = ol.CreateItem(olTaskItem)
   MyTaskItem.Display
   ' VBScript code example.
   Set MyTasktem = Item.Application.CreateItem(3)
   MyTaskItem.Display
Items.Add Method:
Using the Add method on the Items collection allows you to create a new
item based on any message class, whether it is a default Outlook message
class such as IPM.Contact, or a message class for a custom form, such as
IPM.Contact.MyForm. In order to use the Items.Add method, you must first
reference the folder where you want to create a new item.
For more information about message classes, please see the following
articles in the Microsoft Knowledge Base:
   ARTICLE-ID: Q181266
   TITLE     : OL98: Working with Form Definitions and One-Off Forms
   ARTICLE-ID: Q182274
   TITLE     : OL98: How to Update Existing Items to Use a New Custom Form
The following examples use the Items.Add method to create a new item based
on a custom contact form called MyForm:
   ' Automation code example.
   Set ol = New Outlook.Application
   Set olns = ol.GetNameSpace("MAPI")
   Set myFolder = olns.GetDefaultFolder(olFolderContacts)
   Set MyItem = MyFolder.Items.Add("IPM.Contact.MyForm")
   MyItem.Display
   ' VBScript code example.
   Set olns = Item.Application.GetNameSpace("MAPI")
   Set myFolder = olns.GetDefaultFolder(10)
   Set MyItem = MyFolder.Items.Add("IPM.Contact.MyForm")
   MyItem.Display
The following examples use the Items.Add method to create a new default
contact item:
   ' Automation code example.
   Set ol = New Outlook.Application
   Set olns = ol.GetNameSpace("MAPI")
   Set myFolder = olns.GetDefaultFolder(olFolderContacts)
   Set MyItem = MyFolder.Items.Add
   MyItem.Display
   ' VBScript code example.
   Set olns = Item.Application.GetNameSpace("MAPI")
   Set myFolder = olns.GetDefaultFolder(10)
   Set MyItem = MyFolder.Items.Add
   MyItem.Display
NOTE: If you use the Items.Add method, it does not matter what the default
form for the folder is. You can specify any valid message class as long as
it has been published in the folder or has been published in the personal
or organizational forms library.
CreateItemFromTemplate Method:
Use the CreateItemFromTemplate method to create a new item based on an
Outlook template file (.oft) or .msg file format. Because most forms are
published in a folder or forms library, this method is not commonly used.
Probably the most common reason to use this method would be if you were
creating a Microsoft Visual Basic Setup program to install forms for an
Outlook solution. This would typically be done for users who do not have
network access or typically work offline in Outlook. The Visual Basic
program would do the following:
- Automate Outlook.
- Use CreateItemFromTemplate to open a form from a network share
   or floppy disk.
- Using the Outlook object model, publish the form for later use.
   ' Automation code example.
   Set ol = New Outlook.Application
   Set olns = ol.GetNameSpace("MAPI")
   ' Set MyFolder to the default contacts folder.
   Set MyFolder = olns.GetDefaultFolder(olFolderContacts)
   ' Set MyItem to an .oft file on a floppy disk.
   Set MyItem = ol.CreateItemFromTemplate("A:\Contact.oft")
   ' Set MyForm to the item Form Description for publishing.
   Set MyForm = MyItem.FormDescription
   ' Name the form, which also sets its message class.
   MyForm.Name = "My Contact"
   ' Publish the folder to the Contacts folder.
   MyForm.PublishForm olFolderRegistry, MyFolder
   ' Close and do not save changes to the item.
   MyItem.Close olDiscard
Referencing Existing Items
--------------------------
Using Items(I) or For Each...Next:
Typically these approaches are used to loop through all of the items in a
folder. The Items collection contains all of the items in a particular
folder and you can specify which item to reference by using an index with
the Items collection. This is typically used with the For I = 1 to n
programming construct.
If you are using VBScript version 2.0 or later, you can use the For
Each...Next programming construct to loop through the items in the
collection without specifying an index. Both approaches achieve the same
result.
The following examples use the Items(I) approach to loop through all of the
contacts in the Contacts folder and display their FullName field in a
dialog box.
   ' Automation code example.
   Set ol = New Outlook.Application
   Set olns = ol.GetNameSpace("MAPI")
   ' Set MyFolder to the default contacts folder.
   Set MyFolder = olns.GetDefaultFolder(olFolderContacts)
   ' Get the number of items in the folder.
   NumItems = MyFolder.Items.Count
   ' Set MyItem to the collection of items in the folder.
   Set MyItems = MyFolder.Items
   ' Loop through all of the items in the folder.
   For I = 1 to NumItems
      MsgBox MyItems(I).FullName
   Next
   ' VBScript code example.
   Set olns = Item.Application.GetNameSpace("MAPI")
   ' Set MyFolder to the default contacts folder.
   Set MyFolder = olns.GetDefaultFolder(10)
   ' Get the number of items in the folder.
   NumItems = MyFolder.Items.Count
   ' Set MyItem to the collection of items in the folder.
   Set MyItems = MyFolder.Items
   ' Loop through all of the items in the folder.
   For I = 1 to NumItems
      MsgBox MyItems(I).FullName
   Next
The following examples use the For Each...Next construct to achieve the
same result as the preceding examples:
   ' Automation code example.
   Set ol = New Outlook.Application
   Set olns = ol.GetNameSpace("MAPI")
   ' Set MyFolder to the default contacts folder.
   Set MyFolder = olns.GetDefaultFolder(olFolderContacts)
   ' Set MyItems to the collection of items in the folder.
   Set MyItems = MyFolder.Items
   For Each SpecificItem in MyItems
      MsgBox SpecificItem.FullName
   Next
   ' VBScript code example.
   Set olns = Item.Application.GetNameSpace("MAPI")
   ' Set MyFolder to the default contacts folder.
   Set MyFolder = olns.GetDefaultFolder(10)
   ' Set MyItem to the collection of items in the folder.
   Set MyItems = MyFolder.Items
   For Each SpecificItem in MyItems
      MsgBox SpecificItem.FullName
   Next
Using Items("This is the subject"):
You can also use the Items collection and specify a text string that
matches the Subject field of an item. This approach is not commonly used.
The following examples display an item in the Inbox whose subject contains
"Please help on Friday!"
   ' Automation code example.
   Set ol = New Outlook.Application
   Set olns = ol.GetNameSpace("MAPI")
   ' Set MyFolder to the default Inbox.
   Set MyFolder = olns.GetDefaultFolder(olFolderInbox)
   Set MyItem = MyFolder.Items("Please help on Friday!")
   MyItem.Display
   ' VBScript code example.
   Set olns = Item.Application.GetNameSpace("MAPI")
   ' Set MyFolder to the default Inbox.
   Set MyFolder = olns.GetDefaultFolder(6)
   Set MyItem = MyFolder.Items("Please help on Friday!")
   MyItem.Display
Find Method:
Use the Find method to search for an item in a folder based on the value of
one of its fields. If the Find is successful, you can then use the FindNext
method to check for additional items that meet the same search criteria.
The following examples search to see if you have any high priority
appointments.
   ' Automation code example.
   Set ol = New Outlook.Application
   Set olns = ol.GetNamespace("MAPI")
   Set myFolder = olns.GetDefaultFolder(olFolderTasks)
   Set MyTasks = myFolder.Items
   ' Importance corresponds to Priority on the task form.
   Set MyTask = MyTasks.Find("[Importance] = ""High""")
   If MyTask Is Nothing Then ' the Find failed
      MsgBox "Nothing important. Go party!"
   Else
      MsgBox "You have something important to do!"
   End If
   ' VBScript code example.
   Set olns = Item.Application.GetNamespace("MAPI")
   Set myFolder = olns.GetDefaultFolder(13)
   Set MyTasks = myFolder.Items
   ' Importance corresponds to Priority on the task form.
   Set MyTask = MyTasks.Find("[Importance] = ""High""")
   If MyTask Is Nothing Then ' the Find failed
      MsgBox "Nothing important. Go party!"
   Else
      MsgBox "You have something important to do!"
   End If
Restrict Method:
The Restrict method is similar to the Find method, but instead of returning
a single item, it returns a collection of items that meet the search
criteria. For example, you might use this method to find all contacts who
work at the same company.
The following examples display all of the contacts who work at ACME
Software:
   ' Automation code example.
   Set ol = New Outlook.Application
   Set olns = ol.GetNameSpace("MAPI")
   Set MyFolder = olns.GetDefaultFolder(olFolderContacts)
   Set MyItems = MyFolder.Items
   MyClause = "[CompanyName] = ""ACME Software"""
   Set MyACMEItems = MyItems.Restrict(MyClause)
   For Each MyItem in MyACMEItems
      MyItem.Display
   Next
   ' VBScript code example.
   ' Requires VBScript version 2.0 or later.
   Set olns = Item.Application.GetNameSpace("MAPI")
   Set MyFolder = olns.GetDefaultFolder(10)
   Set MyItems = MyFolder.Items
   MyClause = "[CompanyName] = ""ACME Software"""
   Set MyACMEItems = MyItems.Restrict(MyClause)
   For Each MyItem in MyACMEItems
      MyItem.Display
   Next
For more information about using the Restrict and Find methods, please see
the following article in the Microsoft Knowledge Base:
   ARTICLE-ID: Q181189
   TITLE     : OL98: How to Use the Restrict Method
GetItemFromID Method:
This method would typically be used only in more complex solutions where a
solution keeps track of both the StoreID and EntryID of an item so that it
can be quickly retrieved at a later time.
For more information about using the GetItemFromID method, please see the
following article in the Microsoft Knowledge Base:
   ARTICLE-ID: Q180980
   TITLE     : OL98: Programming with EntryIDs, StoreIDs and GetItemFromID
REFERENCES
==========
For more information about creating solutions with Microsoft Outlook 98,
please see the following articles in the Microsoft Knowledge Base:
   Article-ID: Q180826
   Title     : OL98: Resources for Custom Forms and Programming
   Article-ID: Q182349
   Title     : OL98: Custom Forms and Outlook Solutions
Keywords          : kbcode


Questions About Custom Forms and Outlook Solutions
---------------------------------------------------------------------
The information in this article applies to:
- Microsoft Outlook 98
---------------------------------------------------------------------
SUMMARY
=======
This article answers some of the more commonly asked questions about
creating custom Microsoft Outlook 98 forms or programming Outlook either
through Microsoft Visual Basic Scripting Edition (VBScript) or Visual Basic
for Applications Automation.
Topics covered include:
- Available resources for customizing Outlook
- Setting up a simple form for use in a folder (message classes)
- Custom forms and programming limitations
- Disabling the enable/disable macro virus warning
- Cannot select a control in design mode
- Data does not get saved with form
- Programmatically referring to folders and items
- How to use Automation with Outlook
- One-off forms
- Programmatically importing items into Outlook
- Using address books in a solution
- Using the SetColumns method
- Working with User-defined fields
- Using Outlook 98's new contact fields
- Control and field events
- General information about VBScript
MORE INFORMATION
================
- Where do I get started with custom forms or programming Outlook?
For more information about available resources for learning how to create
solutions with Outlook 98, please see the following article in the
Microsoft Knowledge Base:
   ARTICLE-ID: Q180826
   TITLE     : OL98: Resources for Custom Forms and Programming
- I have created a custom form for my Contacts, Tasks, and so on, but it
   is not being used when I create a new item, when I open an existing
   item I had before I created the form, or when I create a new item.
   Why is my form not being used?
To use a custom Outlook form, there are three steps you need to complete.
First, publish the form in the folder or forms library where you will use
it. On the custom form File menu, click Publish Form. The name of the form
is also the "Message Class," which is important for the next two steps.
Second, make this new form the default form for that folder. Using your
right mouse button, click the folder and from the shortcut menu, click
Properties. In the "When posting to this folder, use" list, click to
select your custom form. Now new items that you create will use the custom
form instead of the default Outlook form.
Third, if you have any existing items in the folder that should use this
new form, you must change the Message Class field in each of the items in
the folder. The Message Class field of an item tells Outlook which form to
use to display the item. You cannot directly change this field from an
Outlook since it is considered read-only, but you can use VBScript or
Automation code to change it.
For more information about changing the message class of an item in a
folder, please see the following articles in the Microsoft Knowledge Base:
   ARTICLE-ID: Q182274
   TITLE     : OL98: How to Update Existing Items to Use a New
               Custom Form
   ARTICLE-ID: Q182361
   TITLE     : OL98: Word 97 Document to Change Outlook Folder
               Message Class
- I am trying to do something but it doesn't seem possible. Is it?
Outlook does not support a "full" object model, such as those in Microsoft
Word or Microsoft Excel, so what you are trying to do may not be possible.
Also, there may be other Outlook design considerations or limitations that
may prevent you from using a desired approach in creating a solution.
For more information about some of the more common things that cannot be
done with custom forms, VBScript, or Outlook Automation, please see the
following article in the Microsoft Knowledge Base:
   ARTICLE-ID: Q182402
   TITLE     : OL98: Custom Forms and Programming Limitations
- How can I disable the macro warning?
If the form is not located in a trusted location, then Outlook will display
the macro warning for your protection against potentially
maliciously-written programming code, commonly called macro viruses.
Disable the Macro Warning
-------------------------
1. Open the form that causes the Macro Warning dialog to open.
2. On the Tools menu, point to Forms and click Design This Form.
3. On the Properties tab, click to uncheck Send form definition with item.
4. On the Tools menu, point to Forms, and click Publish Form.
5. Close the form. It is not necessary to save the changes if prompted.
For more information about this issue, please see the following article in
the Microsoft Knowledge Base:
   ARTICLE-ID: Q182376
   TITLE     : OL98: Why Outlook Displays a Security Warning Opening
               an Item
- I entered design mode to change a form I created, but now I can't select
   any of the controls on the form page. How come?
This is a problem in Outlook 98 that you can workaround by switching to a
different form page and then switching back to the page you wish to edit.
For more information about this issue, please see the following article in
the Microsoft Knowledge Base:
   ARTICLE-ID: Q183327
   TITLE     : OL98: Cannot Select Controls in Design Mode
- I created a custom form and it looks great, but when I close and then
   re-open the item, or when I send the form to someone else, all of the
   information I entered into my custom controls is gone. Why does this
   happen?
In Outlook, controls on a form are used to display information, but the
information is actually stored in fields. Therefore, in order for the
values of the controls to be preserved, the control must be bound to an
Outlook standard or user-defined field.
For more information about this issue, please see the following article in
the Microsoft Knowledge Base:
   ARTICLE-ID: Q182362
   TITLE     : OL98: How to Use Fields and Controls with VBScript
- Using VBScript or automation code, how do I refer to a particular
Outlook folder or a particular item in a folder? Or, how do I create a new
folder or item?
For more information about referencing folders and items, please see the
following article in the Microsoft Knowledge Base:
   ARTICLE-ID: Q182614
   TITLE     : OL98: Programming Examples for Referencing Items and Folders
- How do I get started automating Outlook from another program?
For information on automating Outlook from another program, please
see the following articles in the Microsoft Knowledge Base:
   ARTICLE-ID: Q181202
   TITLE     : OL98: How to Automate Outlook from Another Program
   ARTICLE-ID: Q170262
   TITLE     : HOWTO: Use OLE Automation with Microsoft Outlook 97
- Why do some items in a folder have different icons? Why do some
   of the items in the folder have an abnormally large size? Why does the
   Message Class of an item is change back to the default Message Class?
   How can I get a better understanding of how forms work in Outlook.
For more information about these questions, please see the following
article in the Microsoft Knowledge Base:
   ARTICLE-ID: Q181266
   TITLE     : OL98: Working with Form Definitions and One-Off Forms
- When importing contacts from an external database, how do I map some of
   the fields to the user-defined fields that I have set up in a custom
   contact form?
This is a limitation of the Outlook Import and Export Wizard. If you are
familiar with Visual Basic programming, you can create a solution that
programmatically imports or exports the information.
For more information about this issue, please see the following articles in
the Microsoft Knowledge Base:
   ARTICLE-ID: Q180829
   TITLE     : OL98: Cannot Import or Export User-defined Contact
               Fields
   ARTICLE-ID: Q180981
   TITLE     : OL98: Programmatically Import Outlook Items From MS Access
- How come when I export items from a folder, all of the items are not
   exported?
Outlook 98 does not export items if they are based on a custom form,
meaning that the message class field of the item is not the default message
class for that type of item, such as IPM.Contact or IPM.Note.
For more information about this issue, please see the following article in
the Microsoft Knowledge Base:
   ARTICLE-ID: Q184677
   TITLE     : OL98: OL98: All Items in Folder Are Not Exported
- How do I set a default form for my Inbox?
There is no direct way of doing this, but a workaround is to publish the
message form to the folder, and then create a new mail item by clicking the
form at the bottom of the Actions menu.
For more information about this issue, please see the following article in
the Microsoft Knowledge Base:
   ARTICLE-ID: Q180858
   TITLE     : OL98: Cannot Change the Default Mail Message Form
- How can I integrate address books with my solution?
For a top-level overview of working with address books in a custom
solution, please see the following article in the Microsoft Knowledge Base:
   ARTICLE-ID: Q193457
   TITLE     : OL98: Using Address Books in an Outlook Solution
- How do I use the SetColumns method?
For more information about using SetColumns, please see the following
article in the Microsoft Knowledge Base:
   ARTICLE-ID: Q184462
   TITLE     : OL98: How to Use the SetColumns Method
- User-defined fields aren't working the way I expect. How come?
For more information about using user-defined fields in Outlook, please see
the following article in the Microsoft Knowledge Base:
   ARTICLE-ID: Q185050
   TITLE     : OL98: Working With User-defined Fields in Solutions
- I see there's new fields available in Outlook 98 for the contact form,
   but how can I use them?
For more information about using the new Outlook 98 contact fields, please
see the following article in the Microsoft Knowledge Base:
   ARTICLE-ID: Q185239
   TITLE     : OL98: Using the New Contact Fields on a Custom Form
- When I use a custom control on my Outlook form, none of the
   events appear to work. Why?
Outlook only supports the Click event for controls. Many controls are
specifically designed to work with other events, and since these events are
not supported by Outlook, the controls may not function in Outlook.
To work around this situation, create a Visual Basic (VB) 5.0 ActiveX
component (formally called an OLE server application). You start the
component by using the CreateOject method in the Outlook object model. The
component would display a VB 5.0 form with the appropriate controls, get
the information that the user selects or types, and then automate Outlook
to send any necessary information back to the Outlook form.
This technique involves many aspects of programming. For information on how
to create an ActiveX component, please see your documentation for Visual
Basic 5.0.
- When I retrieve appointments and the folder has recurring appointments,
   the only items retrieved are the initial appointment. Why?
You have to set the IncludeRecurrences property of the items collection to
true. The default is false, which means that only the original appointment
is returned. This is commonly an issue when using the Find or Restrict
methods to return a collection of appointment items.
For more information about this issue, please see the following
articles in the Microsoft Knowledge Base:
   ARTICLE-ID: Q181190
   TITLE     : OL98: How to Retrieve All Recurring Appointments
               with Code
   ARTICLE-ID: Q181189
   TITLE     : OL98: How to Use the Restrict Method
- How can I tell what version of VBScript a computer is using? Where
   is the documentation for VBScript? Why doesn't Outlook support
   Visual Basic for Applications?
For more information about VBScript, please see the following
article in the Microsoft Knowledge Base:
   ARTICLE-ID: Q182446
   TITLE     : OL98: General Information About Using VBScript
               with Outlook
- How can I start another program or display a Web page from an
   Outlook form?
You can use the CreateObject method in VBScript to automate an
OLE-aware application.
For more information about this issue, please see the following articles
in the Microsoft Knowledge Base:
   ARTICLE-ID: Q181206
   TITLE     : OL98: How to Start Another Program from an Outlook
               Form
   ARTICLE-ID: Q181267
   TITLE     : OL98: How to Display a Web Page in an Outlook Form
Keywords          : kbprg
Version           : WINDOWS:98
Platform          : WINDOWS
Issue type        : kbinfo