Microsoft Office 2000/Visual Basic Programmer's Guide   

Understanding Script Object Properties

The Scripts collection contains all the Script objects in an Office document. A Script object represents a <SCRIPT> tag pair, its attribute settings, and all the text contained between the tag pair. An Office document or an HTML page can contain several script blocks and each script block can contain any number of procedures. For example, the following HTML code contains three script blocks. The first block initializes an array representing the days of the week, the second block contains a procedure that executes when the page loads, and the third block contains the WriteDay and WriteDate procedures that create a part of the text that is displayed on the page itself.

<HTML>
<HEAD>
<TITLE>Office Programmer's Guide Chapter 6</TITLE>
<SCRIPT LANGUAGE="VBSCRIPT" ID="scrDayArray">
<!--
   Option Explicit
   Dim arrDays(6)
   Dim strDay
 
   arrDays(0) = "Sunday"
   arrDays(1) = "Monday"
   arrDays(2) = "Tuesday"
   arrDays(3) = "Wednesday"
   arrDays(4) = "Thursday"
   arrDays(5) = "Friday"
   arrDays(6) = "Saturday"
-->
</SCRIPT>
<SCRIPT LANGUAGE="VBSCRIPT" ID="scrShowDay">
<!--
   Option Explicit
   Function ShowDayMessage()
      Dim intDay
      Dim strDayOfWeek

      intDay = WeekDay(Date())
      strDayOfWeek = arrDays(intDay - 1)
      MsgBox "Today is " & strDayOfWeek
      Call WriteDay(strDayOfWeek)
      Call WriteDate()
   End Function
-->
</SCRIPT>
</HEAD>

<BODY ONLOAD="ShowDayMessage()">
<H2>Office Programmer's Guide Chapter 6:</H2>
<H3>Shared Office Components</H3>
<HR>

<DIV ID=DayText>
<!-- Day and date text is inserted here. -->
</DIV>

<SCRIPT LANGUAGE="VBSCRIPT" ID="scrWriteDay">
<!--
   Option Explicit
   Function WriteDay(strDay)
      DayText.innerText = "Today is " & strDay
   End Function

   Function WriteDate()
      Dim strDay
      Dim strNum

      strDay = Day(Date())
      If Len(strDay) = 2 Then
         If left(strDay, 1) = 1 Then
            strNum = 0
         Else   
            strNum = Right(strDay, 1)
         End If
      Else
         strNum = strDay
      End If
      Select Case CInt(strNum)
         Case 1
            strDay = strDay & "st"
         Case 2
            strDay = strDay & "nd"
         Case 3
            strDay = strDay & "rd"
         Case else
            strDay = strDay & "th"
      End Select

      DayText.innerText = DayText.innerText & _
         ", the " & strDay & " day of the month."
   End Function
-->
</SCRIPT>
</BODY>
</HTML>

The HTML code and script in the previous example is from the ScriptSamples.doc file in the ODETools\V9\Samples\OPG\Samples\CH06 subfolder on the Office 2000 Developer CD-ROM. (There is also an HTML version in the ScriptSamples.htm file.) If you open this document in Word and then open the Immediate window in the Visual Basic Editor, you can use the Scripts collection and Script object to discover the information you can get using the collection and object properties, as shown in Figure 6.6.

Figure 6.6 Listing of Various Script Properties

You access a Script object within the Scripts collection by using either the index for the object within the collection, or the value of the object's ID attribute. If a <SCRIPT> tag has an ID attribute, the value of that attribute becomes the value of the Script object's Id property. If the <SCRIPT> tag does not have an ID attribute setting, an index value is the only way to locate the Script object within the Scripts collection.

When a document that contains script is opened in an Office application, script blocks are added to the Scripts collection in the order in which they appear in the document. When you add a Script object to a document, the new object is added at the end of the collection of Script objects, regardless of the value of the object's Location property. However, once the document is closed and reopened, that same script will appear in the Scripts collection in the order in which it appears in the document.

Since a Script object's index position within the Scripts collection can change between the time the object is added and the time the document is saved and reopened, it is never a good idea to try to locate a specific Script object within the Scripts collection by using its index position in the collection. Instead, you should make a habit of specifying an Id property value when creating a Script object and using that Id property value to locate the Script object within the Scripts collection. It is useful to use the index when you are looping through all the Script objects in a document, however.

Important   If a <SCRIPT> tag in a document uses the same ID attribute as another <SCRIPT> tag, the Scripts collection will contain only the first Script object that uses the duplicate ID attribute. Any other tags that uses the same ID attribute will not be included in the collection.

The Script object's Location property returns a long integer representing an msoScriptLocation constant that specifies whether the script is located in the <HEAD> element or the <BODY> element of the HTML code. If you don't specify a value for this argument, the default location is within the <BODY> element. Similarly, the Language property returns a long integer representing an msoScriptLanguage constant that specifies the LANGUAGE attribute of the <SCRIPT> tag. If you don't specify a value, the default is VBScript, except in Access, where it is JavaScript.

Important   The Script object's ScriptText property returns everything between the <SCRIPT> tags, but not the <SCRIPT> tags themselves. You must account for this when you are using the Script object's Add method to programmatically add your own script to a document. For more information about adding Script objects, see the following section, "Adding and Removing Script from a Document."