MSHTML Methods: queryCommandText

Scripting Syntax

sText = object.queryCommandText (sCommand);

Object

The queryCommandText method applies to the document and TextRange objects and the controlRange collection.

Arguments

The required sCommand argument names a command to execute. There are many supported commands.

Return Value

The method returns the localised name of the command.

Behaviour

The queryCommandText method provides scriptable access to the object’s IOleCommandTarget functionality, specifically to call the QueryStatus method and obtain just the localised name of the command.

The case-insensitive command name given as sCommand translates to a numeric command ID in the MSHTML command group represented programmatically by CGID_MSHTML. Even within this command group, only a small subset of the commands that are supported through the IOleCommandTarget interface are supported for scripting.

The queryCommandText method fails, in the sense of causing a runtime script error, if the given command name is not supported for scripting. It also fails, again with a runtime script error, if the call to QueryStatus fails. Otherwise, it returns (a copy of) whatever text is produced by the call to QueryStatus, which may be null.

What happens in the call to QueryStatus to obtain the command text varies with the command and is anyway left for the different topic of MSHTML’s IOleCommandTarget functionality.

There is a demonstration here. To see it, please enable scripting and then refresh this page.

function Test_document_queryCommandText ()
{
    var cmd = prompt ("Enter the name of an MSHTML command to query for the command text:", "");
    if (cmd == null) return;

    var text = document.queryCommandText (cmd);
    text = text == null ? "null" : '"' + text + '"';
    alert ('queryCommandText ("' + cmd + '") for the document returned ' + text);
}