Script 对象

优质
小牛编辑
118浏览
2023-12-01

代表 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)
Scripts (Script)