Share via


Adding and Removing Script from a Document

This content is no longer actively maintained. It is provided as is, for anyone who may still be using these technologies, with no warranties or claims of accuracy with regard to the most recent product version or service release.

You add script to a document by using the Scripts collection's Add method. The Add method uses optional arguments that make it possible for you specify the script's location, language, ID attribute, additional <SCRIPT> tag attributes, and the script to be contained within the <SCRIPT> tags. The Add method also automatically generates HTML comment tags (<!— and —>) around your script, so browsers that do not recognize script can ignore it. If you use the Add method without specifying any of the method's arguments, you create an empty <SCRIPT> tag pair that looks similar to this:

<SCRIPT ID="" LANGUAGE="VBScript">
<!--

-->
</SCRIPT>

You use the Anchor argument of the Add method to specify where the Script object should be located in a document.

  • In Microsoft® Word, if you do not specify a value for the Anchor argument, the Script object is inserted at the current location of the insertion point (cursor). In addition, you can add a Script object to a Selection object or an InLineShape object. When you specify a Selection object in the Anchor argument, the Script object is inserted at the end of the Selection object. When you specify an InLineShape object in the Anchor argument, the Script object is inserted before the paragraph marker for the paragraph that is the anchor point for the shape. You cannot add a Script object to a Range object in Word.

  • In Microsoft® Excel, if you do not specify a value for the Anchor argument, the Script object is inserted in the currently active cell. You can also add a Script object to a Range object by specifying the Range object in the Anchor argument. You cannot add a Script object to a Shape object in Excel. If you do specify a Shape object in the Anchor argument, the argument is ignored and the Script object is inserted in the currently active cell.

  • In Microsoft® PowerPoint®, you can only add a Script object to a Slide object. If you specify a Shape object in the Anchor argument, the argument is ignored and the Script is inserted in the specified Slide object.

    Note   To see the Shape objects that are inserted when script is added to an Office document, point to Macro on the Tools menu, and then click Show All Script. The Show All Script is not on the menu by default; you must add it from the Customize dialog box on the Tools menu.

The following procedure uses the Scripts collection's Add method to add a <SCRIPT> tag pair and some Microsoft® Visual Basic ® Scripting Edition (VBScript) code to the <HEAD> element of the current Word document:

Function AddScriptToDocumentDemo()
   ' This procedure illustrates how to use the Scripts collection
   ' to add VBScript code to an Office document.
   Dim strScriptCode As String
   Dim scrArrayScript As Script
   
   On Error Resume Next
   
   Set scrArrayScript = ActiveDocument.Scripts("scrDayArray")
   If Err = 0 Then
      ' The script is already in the document so no
      ' need to add it again.
      Exit Function
   End If
   
   strScriptCode = vbTab & "Option Explicit" & vbCrLf & vbTab _
      & "Dim arrDays(6)" & vbCrLf & vbTab _
      & "arrDays(0) = " & """Sunday""" & vbCrLf & vbTab _
      & "arrDays(1) = " & """Monday""" & vbCrLf & vbTab _
      & "arrDays(2) = " & """Tuesday""" & vbCrLf & vbTab _
      & "arrDays(3) = " & """Wednesday""" & vbCrLf & vbTab _
      & "arrDays(4) = " & """Thursday""" & vbCrLf & vbTab _
      & "arrDays(5) = " & """Friday""" & vbCrLf & vbTab _
      & "arrDays(6) = " & """Saturday"""
      
   With Application.ActiveDocument
      .Scripts.Add Location:=msoScriptLocationInHead, _
         Language:=msoScriptLanguageVisualBasic, ID:="scrDayArray", _
         ScriptText:=strScriptCode
   End With
End Function

You can remove all the script and <SCRIPT> tags from a document by using the Scripts collection's Delete method. You remove a single script from the Scripts collection by using the Script object's Delete method.

See Also

Working with Scripts | Understanding Script Object Properties | Working with Document Properties