Custom taskbar with multiple open books

I have successfully implemented the Microsoft sample Walkthrough: Synchronizing a Custom Taskbar Using the Ribbon Button, which is located here: http://msdn.microsoft.com/en-us/library/bb608590.aspx

Initially, I ran into a problem when the taskbar did not appear, which turned out to be the result of some conflict between my add-in and Microsoft Analysis Toolkit. As soon as I turned off the Analysis Toolpack, the custom taskbar started showing and hiding as expected.

Then I wrote code to change the cells in the selected range when the user clicks a button. Everything seemed to work just fine until I opened another book! Each book window has its own ribbon for adding, but when I clicked the switch button to open / close the taskbar, it will open / close the taskbar for the first window created. This is regardless of the window in which I press the toggle button.

The code creates an instance of the CustomTaskPane object in ThisAddIn_Startup (as in the code example). The only thing I added is the button action in UserControl:

using xl = Microsoft.Office.Interop.Excel; namespace SynchronizeTaskPaneAndRibbon { public partial class TaskPaneControl : UserControl { public TaskPaneControl() { InitializeComponent(); } private void actionButton1_Click(object sender, EventArgs e) { xl.Range selection_rng = Globals.ThisAddIn.Application.Selection; foreach (xl.Range cell in selection_rng.Cells) { if (cell.Value is string) { string v = cell.Value; cell.Value = v + "*"; } } } } } 

The rest of the code is the same as in the example.

Is there a way to change the example so that it works to open multiple books? I want the same Add-In to appear in every window of the book, with the same ribbon and with the same user panel. And, of course, in order for any actions with the ribbon (for example, pressing a button) to go to the user task panel, which appears in the same window.

+6
source share
1 answer

Yes, MSDN has another example that talks about managing custom tasks in several documents. Note that this is in terms of Word / InfoPath, but the basic idea will be the same for Excel.

In general, you need to add a new taskbar for the current book if it does not contain it. Therefore, move the addition of custom taskbar logic from running Addin to the ribbon by pressing a button. Doing this in a button click event gives you the ability to add a new taskbar to the current document when you click the ribbon.

Link: https://msdn.microsoft.com/en-us/library/bb264456(v=office.12).aspx?f=255&MSPPError=-2147217396#Anchor_2

Put useful snippets of code below in case the link is dead in the future:

 //Add a custom taskpane to active Word document public void AddCalendarTaskPane(Word.Document doc) { ctpCalendar = this.CustomTaskPanes.Add(new CalendarControl(), "Select a date", doc.ActiveWindow); ctpCalendar.Visible = true; } 

Instead of deleting, I recommend hiding the taskbar by switching the Visible flag to false, but YMMV. Hope this helps.

0
source

Source: https://habr.com/ru/post/979963/


All Articles