Understanding Script Object Properties
This content is no longer actively maintained. It is provided as is, for anyone who may still be using these technologies, with no warranties or claims of accuracy with regard to the most recent product version or service release.
The Scripts collection contains all the Script objects in a Microsoft® 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>Developing Office Developer Applications</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>Developer Office Developer VBA and Workflow Solutions:</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>
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, when 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.
Because 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.
**Note **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 use 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 do not 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 do not specify a value, the default is Microsoft® Visual Basic® Scripting Edition (VBScript), except in Microsoft® Access, where it is Microsoft® JScript®.
**Note **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.
See Also
Working with Scripts | Adding and Removing Script from a Document | Working with Document Properties