Script Object

Microsoft Office Object Model

Script Object

         
Scripts (Script)

Represents a block of HTML script in a Microsoft Word document, on a Microsoft Excel spreadsheet, or on a Microsoft PowerPoint slide. The Script object is a member of the Scripts collection.

Using the Script Object

Use Scripts.Item(index), where index is the name, ID, or index number of a script, to return a single Script object. Each Script object is identified by the Id property, which provides a convenient name you can use to access the script. The following example adds a single script to the Scripts collection for the active document and displays the ID of the script at index value 1.

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

You can specify the scripting language used in the script by changing the Language property. The following example changes the scripting language of script one to Active Server Pages (ASP).

ActiveDocument.Scripts.Item("ScriptOne") _
    .Language = msoScriptLanguageASP

You can check the location of the script anchor shape within an HTML document by using the Location property. The following example checks to determine whether ScriptOne is in the body of the active HTML document.

If ActiveDocument.Scripts("ScriptOne").Location = _
    msoScriptLocationInBody Then
    MsgBox ("Script is in the HTML document body.")
Else
    MsgBox ("Script is located in the header. ")
End If

You can check or set attributes added to the <SCRIPT> tag (with the exception of the LANGUAGE and ID attributes) by using the Extended property. The following example checks for additional attributes in script one in the active document.

If ActiveDocument.Scripts(1).Extended = "" Then
    MsgBox ("No additional attributes are present " & _
    "in Script " &
     ActiveDocument.Scripts(1).Id)

You can check or set the script text associated with a given script by using the ScriptText property. The following example displays a message box containing the script text associated with script one in the active document.

MsgBox (ActiveDocument.Scripts("ScriptOne").ScriptText)