Microsoft Office 2000/Visual Basic Programmer's Guide |
The Microsoft Script Editor has been added to the Office applications so you can work with the HTML code, DHTML objects, and script in an Office document from within an Office application. To open the Script Editor, point to Macro on the Tools menu, and then click Microsoft Script Editor.
Note The Microsoft Script Editor is not used in Microsoft Outlook. To work with script in Outlook, you use the Outlook Script Editor. For more information about using the Outlook Script Editor, see "Working with the Outlook Object Model" in Chapter 4, "Understanding Office Objects and Object Models."
The Script Editor was designed to work much like the Visual Basic Editor. Unlike the Visual Basic Editor, there is a single instance of the Script Editor open at one time, which means that you can use the Script Editor's Project Explorer to access every open Office document that also has the Script Editor open.
When you view an Office document in the Script Editor, you will see the HTML and XML code that constitute the Office document when it is rendered as a Web page.
When you view an Office document in the Script Editor or any other HTML editor, you may see a great deal of information you do not recognize as part of a typical HTML document. Most likely, you are looking at the Extensible Markup Language (XML) elements the Office application has added to the document. You can identify the XML code in an Office document as the information between the <XML>
tags.
The XML data that appears by default in an Office document depends on the application used to create the document. In Microsoft Excel, Microsoft PowerPoint, and Word, the XML data is used to preserve document-specific information, such as document properties or option settings, so that the page can be accurately rendered as an Office document when it is opened in an Office application. In an Access data access page, XML data is used to supply data to the hidden Microsoft Office Data Source control (MSODSC). It should never be necessary for you to manipulate the XML code that appears in an Office document when it is viewed in an HTML editor.
Caution You should never manipulate the XML code associated with a Data Source control in an Access data access page.
A complete discussion of XML is beyond the scope of this chapter. You can find a complete discussion of XML on the Microsoft Site Builder Workshop Web site at http://www.microsoft.com/workshop/c-frame.htm#/xml/default.asp.
In the Script Editor, you work with the HTML code and script in an Office document by using the Source tab at the bottom of the editor window. You can see the results of your work on the page by using the Quick View tab instead of having to switch back to the host Office application or viewing the document in a Web browser.
Note The Script Editor also displays a Design tab, but Design view is not enabled when you are working with an Office document.
In addition to the tabs for switching between different views of the document, the left side of the editor window allows you to access a toolbox, an HTML Outline window, and a Script Outline window. As when you are working with UserForms in the Visual Basic Editor, you use the toolbox in the Script Editor to add controls to the HTML code in the page. To see the hierarchy of HTML objects on a page, you use the HTML Outline window; to see the various DHTML objects in a document, you use the Script Outline window. Much like the Visual Basic Editor, the Script Editor's Source view includes a color-coded view of the HTML code in a document along with drop-down lists that display the available objects and their related methods and properties.
Note For complete documentation about the Script Editor's user environment, click Contents on the Help menu in the Script Editor.
The Script Outline window makes it easy to add event procedures to an Office document. For example, to add an event procedure for the document object's onclick event, click the "+" sign next to the document object in the Script Outline window and then double-click onclick. The Script Editor inserts a VBScript <SCRIPT>
block in the document and adds the onclick event procedure, as shown in Figure 12.2.
Figure 12.2 Script Outline View in the Microsoft Script Editor
There is one very important difference between what happens when you use the Script Editor to add event procedures and what happens when you use the Visual Basic Editor to add event procedures to Office documents: The Script Editor does not add the parentheses or the required arguments (if any) at the end of the event procedures.
For event procedures that do not require arguments, the absence of the empty parentheses does not prevent the script from running when the page is viewed in a browser. However, for event procedures that do require arguments, their absence prevents the script from running even if the code in the procedure does not use the missing arguments. If you are unaware of this, the missing arguments can cause you some headaches when you are trying to debug your script.
It is up to you do determine which event procedures require additional arguments and what those arguments are. Once you determine this information, you add the arguments by hand. You can find complete documentation for an event procedure related to an object in that object's Help file or by using the Object Browser. The following event procedure for the Spreadsheet control's Calculate event is an example of an event procedure that requires an argument that is not added by the Script Editor:
Sub Spreadsheet1_Calculate(EventInfo)
document.all("txtTotal").value = formatNumber(document _
.all("Spreadsheet1").Range("A7").value, 2, TristateTrue, TristateTrue)
End Sub
The parentheses and the EventInfo argument in the preceding example were added by hand. The argument information was obtained by viewing the Help topic for the Calculate event in the Spreadsheet control's Help file. In this example, EventInfo is the actual name for the argument as it appears in the Help file, but this argument could have been named anything. You are not required to use the same name for an argument as long as there are placeholders provided for each argument.