OL97: Programmatically Accessing Office Form Documents

ID: Q168021

The information in this article applies to:

SUMMARY

This article provides an overview of how you can use Microsoft Visual Basic Scripting Edition (VBScript) or Microsoft Visual Basic for Applications to programmatically access Office components that are embedded in a Microsoft Outlook 97 Office Form.

MORE INFORMATION

With Outlook 97, 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:

1. Start Outlook.

2. On the File menu, click New and then click Office Document.

3. In the New Office Document box, click Microsoft Word Document

   (or other type of Office Form) and then click OK.

4. 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 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: 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 execute any auto 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 workaround 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:

1. 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.

2. On the item's Tools menu, click Design Outlook Form.

3. On the Tools menu, click Macro, and then click Visual Basic Editor.

4. In the Project Explorer window, double-click the ThisWorkbook

   object to open a code window for that object.

5. Enter the following Visual Basic for Applications code:

      Sub Workbook_Open()
      MsgBox "Hello from Visual Basic for Applications!"
      End Sub

6. Close the Visual Basic Editor.

7. On the item's File menu, click Close. When prompted, save changes to the

   item to save it in your Inbox.

8. Double-click the item in the Inbox to open it. You should see
   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 version 2.0 GetObject method to access the embedded Office documents. The following sample VBScript 2.0 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

NOTE: This functionality does not work with VBScript version 3.0 or older versions of VBScript version 3.1. When using the GetObject function with these versions, GetObject will launch another instance of the application instead of using the existing instance. This problem has been fixed in an update to VBScript 3.1 (VBscript.dll version 3.1.0.2430, file date 12/30/97), which is available for download at:

   http://www.microsoft.com/scripting

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.

REFERENCES

For more information on obtaining VBScript version 2.0, please see the following article in the Microsoft Knowledge Base:

   Article-ID: Q167138
   Title     : OL97: General Information About Using VBScript With Outlook

For more information about creating solutions with Microsoft Outlook 97, please see the following articles in the Microsoft Knowledge Base:

   Article-ID: Q166368
   Title     : OL97: How to Get Help Programming with Outlook

   Article-ID: Q170783
   Title     : OL97: Q&A: Questions about Customizing or
               Programming Outlook

Additional query words: OutSol OutSol97
Keywords          : kbcode 
Version           : 97
Platform          : WINDOWS

Last Reviewed: May 17, 1999