OCCommand Object

Microsoft Office Web Components Object Model

OCCommand Object

         
OCCommands OCCommand

Represents a single command in the specified Microsoft Office Web Component.

Using the OCCommand object

Use the Item property of the OCCommands collection to return a single OCCommand object.

The OCCommandId, ChartCommandIdEnum, PivotCommandId, and SpreadsheetCommandId constants contain lists of the supported commands for each Microsoft Office Web Component.

Use the Execute method of the OCCommand object to execute a particular command. The following example uses the Execute method to select the upper-left cell in the active sheet of Spreadsheet1.

Sub SelectUpperLeft()

    Dim ssConstants

    Set ssConstants = Spreadsheet1.Constants

    ' Select the upper-left cell in the active worksheet.
    Spreadsheet1.Commands(ssConstants.ssCommandMoveToOrigin).Execute

End Sub
Use the CommandBeforeExecute event to impose certain restrictions before a command is executed, or to cancel a command. The following example refreshes PivotTable1 when the Export command is invoked so that the latest data is exported to Microsoft Excel.
Sub PivotTable1_CommandBeforeExecute(Command, Cancel)

   Dim ptConstants

   Set ptConstants = PivotTable1.Constants

   ' Check to see if the Export command
   ' has been invoked.
   If Command = ptConstants.plCommandExport Then

       ' Refresh the PivotTable list.
       PivotTable1.Refresh

   End If

End Sub
The following example prevents the user from cutting, copying, or exporting the contents of Spreadsheet1 to Microsoft Excel.
Sub Spreadsheet1_CommandBeforeExecute(Command, Cancel)

    Dim ssConstants

    Set ssConstants = Spreadsheet1.Constants

    Select Case Command

        ' Check to see if the Export command has
        ' been invoked.
        Case ssConstants.ssCommandExport

            ' Cancel the command.
            Cancel.Value = True

            ' Display a message to the user.
            MsgBox "Export of the data is prohibited."

        ' Check to see if the Cut or Copy commands
        ' have been invoked.
        Case ssConstants.ssCommandCopy, ssConstants.ssCommandCut

            ' Cancel the command.
            Cancel.Value = True

            ' Display a message to the user.
            MsgBox "Cutting or Copying the data is prohibited."
    End Select

End Sub
Use the CommandExecute event when you want to execute a set of commands when a particular command is executed. The following example writes the current date and time to an HTML text box control every time that PivotTable1 is refreshed.
Sub PivotTable1_CommandExecute(Command)

   Dim ptConstants

   Set ptConstants = PivotTable1.Constants

   ' Check to see if the PivotTable list has been refreshed.
   If Command = ptConstants.plCommandRefresh Then

      ' Write the current data and time to the text box.
      TextBox.Value = "PivotTable last refeshed on " & _
                      Date & " at " & Time

   End If

End Sub