Introduction: similar to " scissor painting " is a few command bars in Excel . where "document Operation" is freely customizable, the document operator panel cannot be used directly (not visible), as can be seen in this diagram, because I wrote the code on the above added a TextBox control.
The command bar is a member of the CommandBars collection and belongs to the CommandBar type. Use the VBA write snippet code to list all the command bars:
1 SubShowcommandbar ()2 DimBars asOffice.commandbars3 SetBars =Application.CommandBars4 DimBar asOffice.CommandBar5 for eachBarinchBars6i = i +17Cells (I,1) = bar. Name'name8Cells (I,2) = bar. NameLocal'Localized name9Cells (I,3) = bar. Index'IndexTen Next One End Sub
An excerpt of the corresponding relationship with the previous command bars is as follows.
The Task Pane is the target to be customized and cannot be added directly to the control we want, but the control can be added to the ActionsPane object.
Relationship:
This relational model means that to add a control you need to find ActionsPane, and to control the overall position of the elements on the worksheet depends on the task Pane.
Yes, be sure to remember that task pane is a command bar (Commanbar). But to add controls to the command bar, channelled through ActionsPane.
1 Public Sealed Partial classThisWorkbook:Microsoft.Office.Tools.Excel.WorkbookBase {2 InternalMicrosoft.Office.Tools.ActionsPane ActionsPane; 3 //omit part of the code4 Private voidInitializecontrols () {5 This. ActionsPane = Globals.Factory.CreateActionsPane (NULL,NULL,"ActionsPane","ActionsPane", This);6 }7 //omit part of the code8}
The ActionsPane object is defined in ThisWorkbook.Designer.cs and can be called directly in the ThisWorkbook class:
1 usingOffice =Microsoft.Office.Core;2 3 Private voidThisWorkbook_Startup (Objectsender, System.EventArgs e)4 {5 //add a control to ActionsPane (after adding a control, the task pane is displayed by default when the workbook is opened)6TextBox box=NewTextBox ();7 ActionsPane.Controls.Add (box);8 //Cancel Docking9Box. Dock =Dockstyle.none;Ten //Setting anchoring OneBox. Anchor =Anchorstyles.top; A //width of the text box -Box. Width = -; - //set the display position of a task pane theOffice.CommandBar bar = globals.thisworkbook.application.commandbars[ the]; -Bar. Position =Office.MsoBarPosition.msoBarLeft; -}
Programmatically you can add controls dynamically to ActionsPane, but sometimes you need to use a well-designed surface, ActionsPane no design surface, you can borrow Actionspanecontrol or UserControl, The latter can also act as the designer for the program-level Project Actions pane.
Right-click the solution to add the user control:
Usercontrl is a control container that drags controls onto the design surface:
Then use the code to add the control to ActionsPane that completes the customization of the task pane:
1 private void ThisWorkbook_Startup (object sender, System.EventArgs e) 2 { 3 add controls to ActionsPane 4 ActionsPane.Controls.Add ( UserControl1 ()); 5 When the control is added, the visibility is automatically true, and the following sentence is not required 6 // actionspane.visible = true; 7 }
After the workbook is started, you can find the Document Actions button in the View tab to control its display and hide (by default the workbook is not visible).
Finally, to point out the individual's understanding (not sure whether it is correct):
The Document-level task pane is "Singleton" -that is, any time you get the same task pane. Even if you can dynamically add or reduce controls to it, you can make the controls visible or invisible in the pane as needed. But the objects of all these operations are the same task Pane, the same actionspane on the same CommandBar.
This is different from the task pane in the program-level addin project.
VSTO Learning Path: task pane for document-level projects