Scripts 集合对象

         
Scripts (Script)

Script 对象的集合,它代表指定文档中的 HTML 脚本的集合。

使用 Scripts 集合

Scripts 集合包含了指定文档中所有的 Script 对象,它们在该集合中按源顺序(即将 Script 对象添加到源文件中的顺序)排列。源顺序不受脚本在文档中的位置(页眉或正文)的影响。可用 Script 对象访问脚本,或向 Microsoft Word 文档、Microsoft Excel 工作表或 Microsoft PowerPoint 幻灯片中添加脚本。也可用 Scripts 集合访问在 Microsoft Office 应用程序中打开的任何 HTML 页或脚本。

注意  Microsoft Access 不使用这一共享的 Office 组件。

添加 Script 对象

在向 Scripts 集合中添加 Script 对象时,一个 msoScriptAnchor 类型的 Shape 对象会被自动添加到该文档中。在 Excel 工作表或 PowerPoint 幻灯片中,该对象会被添加到 Shapes 集合中;在 Word 文档中,该对象会被添加到 InlineShapes 集合中。可通过 Add 方法向文档中添加一个 Script 对象。以下示例向活动 Word 文档添加一个简单的脚本。

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

若要访问 Scripts 集合中的特定项,请用 Item 方法,并提供 <SCRIPT> 标记的 ID 属性或标识脚本在集合中的位置的索引号。该 ID 属性在文档中必须唯一。如果 ID 属性不唯一,则返回找到的第一个具有该 ID 的脚本。以下示例在一个消息框中显示找到的第一个以 "ScriptOne" 为 ID 的脚本的语言。

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

Count 属性判断指定文档中 Script 对象的个数。以下示例显示活动文档中脚本的个数。

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

如下例所示,可以用 Delete 方法从 Scripts 集合中删除一个脚本。

ActiveDocument.Scripts("ScriptOne").Delete