Share via


Document.execCommand Method

SharePoint Designer Developer Reference

Executes a specified action on the current document, current selection, or the specified range, and then returns a Boolean that indicates whether the command completed successfully.

Syntax

expression.execCommand(cmdID, showUI, value)

expression   Required. A variable that represents a Document object.

Parameters

Name Required/Optional Data Type Description
cmdID Required String A String that specifies the command to execute.
showUI Optional True True displays the dialog box associated with the command, if the command has a corresponding dialog box. The default value is False.
value Optional Variant A Variant that specifies any additional arguments associated with the cmdID parameter. For example, if the cmdID parameter is specifies the BackColor command (shown in the list below), the value parameter contains the color name for the background color of the selection.

Remarks

The cmdID parameter can be one of the following String values:

Command Description
BackColor

Sets the background color of the current selection. The value parameter must specify the color name of the background color. For available color names, see the Color Table.

The following code sets the background color for the active document to yellow.

Visual Basic for Applications
ActiveDocument.execCommand "backcolor", False, "yellow"

Note

You must use a color name and not a Web-safe RGB color value, such as #663399.

Copy

Copies the current selection to the Clipboard.

The following code copies the selection to the Clipboard.

Visual Basic for Applications
ActiveDocument.execCommand "copy"
CreateBookmark

Creates a bookmark anchor around the selected text.

The following code creates a bookmark anchor named "newbookmark" around the selected text.

Visual Basic for Applications
ActiveDocument.execCommand "createbookmark", False, "newbookmark"
CreateLink

Inserts a hyperlink on the current selection or displays a dialog box enabling the user to specify a URL to insert as a hyperlink on the current selection. The value parameter must specify the URL of the hyperlink.

The following code creates a hyperlink to the Microsoft Web site.

Visual Basic for Applications
ActiveDocument.execCommand "createlink", False, _
    "http://www.microsoft.com"
Cut

Cuts the current selection and copies it to the Clipboard.

The following code cuts the selected text and places a copy in the Clipboard.

Visual Basic for Applications
ActiveDocument.execCommand "cut"
Delete

Deletes the current selection without storing a copy of it.

The following code deletes the selected text.

Visual Basic for Applications
ActiveDocument.execCommand "delete"
FontName

Sets the font for the current selection. The value parameter must specify the name of the font to use.

The following code specifies the Stencil font for the selected text.

Visual Basic for Applications
ActiveDocument.execCommand "fontname", False, "stencil"

Note

If the font name specified in the value parameter is not available, the selected text appears in the browser in the default font.

FontSize

Sets the font size for the current selection. The value parameter must specify the size of the font to use. For possible values for font size, see the fontSize property.

The following code specifies a font size for the selected text of 115 percent of the size of the font setting for the parent element.

Visual Basic for Applications
ActiveDocument.execCommand "fontsize", False, "115%"
ForeColor

Sets the color of the selected text. The value parameter must specify the color name of the text color. For available color names, see the Color Table on MSDN.

The following code sets the color of the selected text to red.

Visual Basic for Applications
ActiveDocument.execCommand "forecolor", False, "red"

Note

You must use a color name and not a Web-safe RGB color value, such as #663399.

Indent

Indents the selected text by inserting a BLOCKQUOTE element around the selected text and any surrounding text that belongs to the same parent element.

The following code indents the selected text and related surrounding text.

Visual Basic for Applications
ActiveDocument.execCommand "indent"
InsertButton

Inserts a BUTTON element at the insertion point. If text is selected, the new element overwrites the selected text. Use the value parameter to specify an id attribute for the new element.

The following code inserts a BUTTON element with an id attribute value of "newbutton" at the insertion point.

Visual Basic for Applications
ActiveDocument.execCommand "insertbutton", False, "newbutton"
InsertFieldset

Inserts a FIELDSET element at the insertion point. If text is selected, the new element overwrites the selected text. Use the value parameter to specify an id attribute for the new element.

The following code inserts a FIELDSET element with an id attribute value of "newfield" at the insertion point.

Visual Basic for Applications
ActiveDocument.execCommand "insertfieldset", False, "newfield"
InsertHorizontalRule

Inserts an HR element at the insertion point. If text is selected, the new element overwrites the selected text.

The following code inserts an HR element at the insertion point.

Visual Basic for Applications
ActiveDocument.execCommand "inserthorizontalrule"
InsertIFrame

Inserts an IFRAME element at the insertion point. If text is selected, the new element overwrites the selected text. Use the value parameter to specify the source file displayed in the inline frame.

The following code inserts an IFRAME element at the insertion point with a src attribute of "form.htm".

Visual Basic for Applications
ActiveDocument.execCommand "insertiframe", False, "form.htm"
InsertImage

Inserts an IMG element at the insertion point. If text is selected, the new element overwrites the selected text. Use the value parameter to specify the image file name.

The following code inserts the chelan.jpg image file into the document at the insertion point.

Visual Basic for Applications
ActiveDocument.execCommand "insertimage", False, "chelan.jpg"
InsertInputButton

Inserts an INPUT element of type "button" at the insertion point. If text is selected, the new element overwrites the selected text. Use the value parameter to specify an id attribute for the new element.

The following code inserts a new input button with an id attribute value of "newbutton" at the insertion point.

Visual Basic for Applications
ActiveDocument.execCommand "insertinputbutton", False, "newbutton"
InsertInputCheckbox

Inserts an INPUT element of type "checkbox" at the insertion point. If text is selected, the new element overwrites the selected text. Use the value parameter to specify an id attribute for the new element.

The following code inserts a new check box with an id attribute value of "newcheckbox" at the insertion point.

Visual Basic for Applications
ActiveDocument.execCommand "insertinputcheckbox", False, "newcheckbox"
InsertInputFileUpload

Inserts an INPUT element of type "fileupload" at the insertion point. If text is selected, the new element overwrites the selected text. Use the value parameter to specify an id attribute for the new element.

The following code inserts an INPUT element of type "fileupload" at the insertion point.

Visual Basic for Applications
ActiveDocument.execCommand "insertinputfileupload", False, "newfile"
InsertInputHidden

Inserts an INPUT element of type "hidden" at the insertion point. If text is selected, the new element overwrites the selected text. Use the value parameter to specify an id attribute for the new element.

The following code inserts a hidden INPUT element at the insertion point.

Visual Basic for Applications
ActiveDocument.execCommand "insertinputhidden", False, "newhidden"
InsertInputPassword

Inserts an INPUT element of type "password" at the insertion point. If text is selected, the new element overwrites the selected text. Use the value parameter to specify an id attribute for the new element.

The following code inserts a text box for entering passwords at the insertion point.

Visual Basic for Applications
ActiveDocument.execCommand "insertinputpassword", False, "newpassword"
InsertInputReset

Inserts an INPUT element of type "reset" at the insertion point. If text is selected, the new element overwrites the selected text. Use the value parameter to specify an id attribute for the new element.

The following code inserts a Reset button at the insertion point.

Visual Basic for Applications
ActiveDocument.execCommand "insertinputreset", False, "newresetbutton"
InsertInputSubmit

Inserts an INPUT element of type "submit" at the insertion point. If text is selected, the new element overwrites the selected text. Use the value parameter to specify an id attribute for the new element.

The following code inserts a Submit button at the insertion point.

Visual Basic for Applications
ActiveDocument.execCommand "insertinputsubmit", False, "newsubmitbutton"
InsertInputText

Inserts an INPUT element at the insertion point. If text is selected, the new element overwrites the selected text. Use the value parameter to specify an id attribute for the new element.

The following code inserts a one-line text box at the insertion point.

Visual Basic for Applications
ActiveDocument.execCommand "insertinputtext", False, "newtextbox"
InsertMarquee

Inserts a MARQUEE element at the insertion point. If text is selected, the new element overwrites the selected text.

The following code inserts a new MARQUEE element at the insertion point.

Visual Basic for Applications
ActiveDocument.execCommand "insertmarquee"
InsertOrderedList

If text is selected, toggles the text selection between items in an ordered list and normal paragraphs. If no text is selected, inserts an OL element with one LI element.

The following inserts an ordered list at the insertion point or converts the selected text to an ordered list.

Visual Basic for Applications
ActiveDocument.execCommand "insertorderedlist"
InsertParagraph

Inserts a P element at the insertion point. If text is selected, the new element overwrites the selected text. Use the value parameter to specify an id attribute for the new element.

The following inserts a new paragraph at the insertion point.

Visual Basic for Applications
ActiveDocument.execCommand "insertparagraph", True, "newpara"
InsertSelectDropdown

Inserts a SELECT element at the insertion point. If text is selected, the new element overwrites the selected text. Use the value parameter to specify an id attribute for the new element.

The following inserts a new drop-down list at the insertion point.

Visual Basic for Applications
ActiveDocument.execCommand "insertselectdropdown", True, "newdropdownlist"
InsertSelectListbox

Inserts a SELECT element that contains the multiple attribute at the insertion point. If text is selected, the new element overwrites the selected text. Use the value parameter to specify an id attribute for the new element.

The following code inserts a list box at the insertion point.

Visual Basic for Applications
ActiveDocument.execCommand "insertselectlistbox", True, "newlistbox"
InsertTextArea

Inserts a TEXTAREA element at the insertion point. If text is selected, the new element overwrites the selected text. Use the value parameter to specify an id attribute for the new element.

The following code inserts a multiple-line text box at the insertion point.

Visual Basic for Applications
ActiveDocument.execCommand "inserttextarea", True, "newtextarea"
InsertUnorderedList

If text is selected, toggles the text selection between items in a bulleted list and normal paragraphs. If no text is selected, inserts a UL element with one LI element.

The following inserts a bulleted list at the insertion point or converts the selected text to a bulleted list.

Visual Basic for Applications
ActiveDocument.execCommand "insertunorderedlist"
Italic

Toggles the text selection between italic and roman characters.

The following toggles italic characters for the selected text.

Visual Basic for Applications
ActiveDocument.execCommand "italic"
JustifyCenter

Centers the selected text and any surrounding text that belongs to the same parent element. If the affected text alignment is already centered, this command returns the text to the default alignment.

The following code centers the selected text and any related text.

Visual Basic for Applications
ActiveDocument.execCommand "justifycenter"
JustifyLeft

Left-aligns the selected text and any surrounding text that belongs to the same parent element. If the affected text alignment is already left-aligned, then this command returns the text alignment to the default alignment.

The following code left-aligns the selected text and any related text.

Visual Basic for Applications
ActiveDocument.execCommand "justifyleft"
JustifyRight

Right-aligns the selected text and any surrounding text that belongs to the same parent element. If the affected text alignment is already right-aligned, this command returns the text alignment to the default alignment.

The following code right-aligns the selected text and any related text.

Visual Basic for Applications
ActiveDocument.execCommand "justifyright"
Outdent

Decreases by one increment the indentation of the selected text by removing a BLOCKQUOTE element from around the selected text and any surrounding text that belongs to the same parent element. If there are no surrounding BLOCKQUOTE elements, this command does nothing.

Decreases by one the indentation of the selected text and any related text.

Visual Basic for Applications
ActiveDocument.execCommand "outdent"
Paste

Overwrites the contents of the Clipboard on the current selection.

The following code pastes the contents of the Clipboard into the document at the cursor position or overwrites any selected text.

Visual Basic for Applications
ActiveDocument.execCommand "paste"
RemoveFormat

Removes the font and character formatting from the current selection.

The following code removes all formatting from the selected text.

Visual Basic for Applications
ActiveDocument.execCommand "removeformat"
SelectAll

Selects the entire document.

The following code selects all contents in the active document.

Visual Basic for Applications
ActiveDocument.execCommand "selectall"
UnBookmark

Removes any bookmark from the current selection. If the selected text is not all the text affected by a bookmark anchor, the bookmark anchor closes before and reopens after the selected text. If the selected text is all the text affected by a bookmark anchor, the entire bookmark is removed from the document. If multiple bookmarks are included in the selection, all bookmarks are removed from the selection.

The following code removes all bookmarks from the selected text.

Visual Basic for Applications
ActiveDocument.execCommand "unbookmark"
Underline

Toggles the current selection between underlined and not underlined.

The following code toggles underlining for the selected text.

Visual Basic for Applications
ActiveDocument.execCommand "underline"
Unlink

Removes any hyperlinks from the current selection. If the selected text is not all the text affected by a hyperlink, the hyperlink closes before and reopens after the selected text. If the selected text is all the text affected by a hyperlink, the entire hyperlink is removed from the document. If multiple hyperlinks are included in the selection, all hyperlinks are removed from the selection.

The following removes all hyperlinks from the selected text.

Visual Basic for Applications
ActiveDocument.execCommand "unlink"
Unselect

Clears the current selection.

The following code cancels the selection in the active document and places the cursor at the beginning of the document.

Visual Basic for Applications
ActiveDocument.execCommand "unselect"

If the command specified in the CmdID parameter sets a value, the value parameter is required.

If the value parameter contains a string that represents the value of the id attribute for an element and an element with the same id attribute value already exists, the execCommand method increments the id attribute string. For example, if you specify "newelement" for the value parameter and an element with the id attribute of "newelement" exists, the new element that the execCommand parameter adds is "newelement0". If there are several elements with the same id attribute (for example, "newelement", "newelement0", and "newelement1"), the newly added element increments up to the next available number (in this example, "newelement2").

Many of the commands used by the execCommand method require that the user select text in the page window. If no text is selected and the command requires text in the specified range, the execCommand method fails and displays an error.

See Also