Script 对象

         
Scripts (Script)

代表 Microsoft Word 文档、Microsoft Excel 电子表格或 Microsoft PowerPoint 幻灯片中的一段 HTML 脚本。Script 对象是 Scripts 集合的成员。

使用 Script 对象

Scripts.Item(index) 可返回一个 Script 对象,其中 index 是脚本的名称、ID 或索引号。每个 Script 对象都由 Id 属性所标识,该属性为访问脚本提供了一个易于使用的名称。以下示例向活动文档的 Scripts 集合添加一个脚本,并显示索引值为 1 的脚本的 ID。

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

可通过更改 Language 属性指定用于编写脚本的语言。以下示例将脚本“ScriptOne”的编写语言更改为“Active Server Page(ASP)”。

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

可通过 Location 属性检查 HTML 文档中相应的脚本标记图形的位置。以下示例将判断脚本“ScriptOne”是否位于活动 HTML 文档的正文中。

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

可通过 Extended 属性检查或设置添加到 <SCRIPT> 标记的属性(LANGUAGEID 属性例外)。以下示例检查活动文档中第一个脚本的附加属性。

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

可用 ScriptText 属性检查或设置与给定脚本相关的脚本文字。以下示例在一个消息框中显示与活动文档中的第一个脚本相关的脚本文字。

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