MORE INFORMATION
IMPORTANT: All references to Visual Basic for Applications in this article refer to code that is part of the embedded document and not Visual Basic for Applications code in Outlook.
With Outlook, you can create an Office Form that includes an embedded
Word document, Excel workbook, Excel chart, or PowerPoint presentation. You
typically would use these types of custom Outlook forms as part of a
solution that routes Office documents or shares (posts) Office documents in
a public folder.
Steps to Create an Office Form
To create an Office Form, follow these steps:
- Start Outlook.
- On the File menu, click New and then click Office Document.
- In the New Office Document box, click Microsoft Word Document (or other type of Office Form) and then click OK.
- Click to select either Post the document in this folder, or Send the document to someone, depending on the type of Office Form you want to create, and then click OK.
If you use Office forms as part of a custom solution, you can use VBScript
or Visual Basic for Applications to automatically initialize a form or to
transfer information between Outlook and the embedded Office document
object.
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:
For more information about the support options that are available and about how to contact Microsoft, visit the following Microsoft Web site:
NOTE: Because each custom solution has different requirements in order to achieve the desired end result, the methods outlined below are offered as a starting point for achieving the end result and should be implemented in a way that best suits the solution's needs.
Using Events from Embedded Objects
When using Office Forms to create a custom solution, you may want to have
Visual Basic for Applications code in the embedded object automatically
run. An example of this is having code automatically calculate dates based
on today's date and insert those calculated dates into an embedded Excel
spreadsheet. To implement this, you create a
Workbook_Open() event
procedure in the embedded spreadsheet and use Visual Basic for Applications
code to calculate the dates. Because the
Workbook_Open() procedure is run
automatically when the Office Form opens, the code will execute every time
you open the Office Form.
NOTE: Word will not run any auto-start macros or procedures (typically
AutoOpen or
Document_Open) when a document is embedded in another program,
or container. You can, however, place a CommandButton on the Word document
object and create a click event procedure for the CommandButton. The user
will have to click the button to run the Visual Basic for Applications code
in the Word document. If this work around is not appropriate for your
solution, you can use the
GetObject method described below to automate the
Word document from VBScript.
Create an Auto Event for an Excel Spreadsheet
To create an auto event for an Excel spreadsheet use these steps:
- Follow the steps in the "Steps to Create an Office Form" section to create a new Office Form based on an Excel spreadsheet. It does not matter whether it is a post or message form.
- On the item's Tools menu, point to Forms and then click Design Outlook Form.
- On the Tools menu, click Macro, and then click Visual Basic Editor.
- In the Project Explorer window, double-click the ThisWorkbook object to open a code window for that object.
- Enter the following Visual Basic for Applications code:
Sub Workbook_Open()
MsgBox "Hello from Visual Basic for Applications!"
End Sub
- Close the Visual Basic Editor.
- On the item's File menu, click Close. When prompted, save changes to the item to save it in your Inbox folder (if it was a message form), or the currently selected folder (if it was a post form).
- Double-click the item in the folder to open it. You should see a message indicating the spreadsheet contains macros and then the message "Hello from Visual Basic for Applications!"
Using GetObject to Automate an Embedded Object
There may be circumstances where you want to use VBScript to put
information into the embedded object or set some attributes of the object.
An example of this is creating an
Item_Open event in VBScript that
automatically inserts a list of your contacts and their addresses into a
Word Office Form document.
You can use the VBScript
GetObject method to access the embedded Office documents. The following sample code will automatically insert "This is from VBScript!" into the embedded Word document in a Word Office Form. Because the code is in an
Item_Open event, the code runs automatically when the Office Form item opens.
Sub Item_Open()
Set objWord = GetObject(, "Word.Application")
' Use Word methods to insert some sample text
objWord.Selection.TypeText "This is from VBScript!"
End Sub
The following table lists various objects that you can use with the
GetObject method to access embedded objects. Once you have a reference to
the object, consult the object model documentation for that program to
manipulate that type of object.
Form type Object name*
--------- ------------
Word document Word.Application or Word.Document
Excel spreadsheet Excel.Application or Excel.Sheet
Excel chart Excel.Application or Excel.Chart
PowerPoint presentation PowerPoint.Application or PowerPoint.Show
* The object you choose depends on which methods and properties
of the application's object model you need to use.