Scripts Collection Object

Microsoft Office Object Model

Scripts Collection Object

         
Scripts (Script)

A collection of Script objects that represent the collection of HTML scripts in the specified document.

Using the Scripts Collection

The Scripts collection contains all of the Script objects in a given document, in source order (the order in which Script objects were added to the source file). Source order isn’t affected by the location (header or body text) of the script in the document. You can use Script objects to access a script or to add a script to a Microsoft Word document, a Microsoft Excel worksheet, or a Microsoft PowerPoint slide. You can also use the Scripts collection to access any HTML page or script that’s opened in a Microsoft Office application.

Note   Microsoft Access doesn’t use this shared Office component.

Adding a Script

When you add a Script object to the Scripts collection, a Shape object of type msoScriptAnchor is automatically added to the document. On an Excel worksheet or a PowerPoint slide, the shape is added to the Shapes collection; in a Word Document, the shape is added to the InlineShapes collection. You add a Script to a document by using the Add method. The following example adds a simple script to the active Word document.

myScript = ActiveDocument.Scripts.Add( _
    , msoScriptLocationInBody, _
    msoScriptLanguageVisualBasic, _
    "ScriptOne", , _
    "MsgBox ""This is ScriptOne.""")

To access a particular item in the Scripts collection, use the Item method, and supply either the ID attribute of the <SCRIPT> tag or the index number that indicates the position of the script in the collection. The ID must be unique within the document. In the case of duplicate ID attributes, the first script found that has that ID is returned. The following example displays a message box indicating the language of the first script found that uses the ID "ScriptOne".

MsgBox (ActiveDocument.Scripts.Item("ScriptOne").Language)

Use the Count property to determine the number of Script objects in the specified document. The following example displays the number of scripts in the active document.

If ActiveDocument.Scripts.Count = 0 Then
    MsgBox ("There are no " & _
    "scripts in this document. ")
End If
If ActiveDocument.Scripts.Count = 1 Then
    MsgBox ("There is " & _
    ActiveDocument.Scripts.Count & _
    " script in this document. ")
End If
If ActiveDocument.Scripts.Count > 1 Then
    MsgBox ("There are " & _
    ActiveDocument.Scripts.Count & _
    " scripts in this document. ")
End If

Use the Delete method to remove a script from the Scripts collection, as in the following example.

ActiveDocument.Scripts("ScriptOne").Delete