TaskPanes Instances on different workbooks Excel 2013

Add-in Express™ Support Service
That's what is more important than anything else

TaskPanes Instances on different workbooks Excel 2013
Try to find how to get the right task pane instance for the activeworkbook 
Sylvain PIRON




Posts: 24
Joined: 2013-02-26
Hi,

I'm trying to find how to get the right taskpane instance when i activate a workbook.

I got a taskpane that show me the sheets in one workbook and when i activate another workbook, it will show his sheet. But when i got two or more workbooks open, I don't know how to get the right taskpane instance to show or refresh the sheets. I know the taskpanes are in

AddinExpress.XL.ADXExcelTaskPanesCollectionItem item = this.TaskPaneManager.Items[1];
foreach (AddinExpress.XL.ADXExcelTaskPane taskPane in item.TaskPaneInstances)
{
//My problem is how to find the good one for the active workbook
}


Hope you can help me.

Best regards,
Sylvain.
Posted 30 Jun, 2013 09:52:36 Top
Andrei Smolin


Add-in Express team


Posts: 18830
Joined: 2006-05-11
Hello Sylvain,

You're right, ADXExcelTaskPane should provide some way to find the window it belongs to. Try this workaround:

private IntPtr hostHandle = IntPtr.Zero;
private void ADXExcelTaskPane1_ADXBeforeTaskPaneShow(object sender, ADXBeforeTaskPaneShowEventArgs e) {
    hostHandle = (IntPtr)GetType().InvokeMember("HostHandle", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.GetProperty | System.Reflection.BindingFlags.Instance, null, this, null);            
}
public void SetLabel() {
    string text = "";
    Excel._Application ExcelApp = this.ExcelAppObj as Excel._Application;
    Excel.Windows windows = ExcelApp.Windows;
    int hwnd = (int)hostHandle;
    for (int i = 1; i <= windows.Count; i++) {
        Excel.Window window = windows[i];
        if (window.Hwnd == hwnd) {
            text = window.Caption.ToString();                    
        }
        Marshal.ReleaseComObject(window);
        if (text != "") break;
    }
    Marshal.ReleaseComObject(windows);
    this.label1.Text = text;
}



Andrei Smolin
Add-in Express Team Leader
Posted 01 Jul, 2013 03:56:43 Top
sylvain.piron




Posts: 24
Joined: 2013-02-26
Hi Andrei,

Thanks for your answer, it does work for the second, third, ... workbook but not for the first one because the event ADXBeforeTaskPaneShow is raise when the start page in Excel 2013 is show and it is not raise a second time when the first workbook is open. Any workaround for this part?

It does work when i use it in a new Add-in but if I put it in my addin i got this error. Any idea?:
'Microsoft.Office.Interop.Excel.Window' does not contain a definition for 'Hwnd' and no extension method 'Hwnd' accepting a first argument of type 'Microsoft.Office.Interop.Excel.Window' could be found (are you missing a using directive or an assembly reference?)

Thanks again,

Regards

Sylvain.
Posted 01 Jul, 2013 05:36:36 Top
Andrei Smolin


Add-in Express team


Posts: 18830
Joined: 2006-05-11
Hello Sylvain,

Actually, you can call the code line below whenever you need:

hostHandle = (IntPtr)GetType().InvokeMember("HostHandle", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.GetProperty | System.Reflection.BindingFlags.Instance, null, this, null); 


Still, from your description I cannot understand if your form is shown for the first workbook.

Also, please try calling the SetLabel above in this way:

private void adxRibbonButton1_OnClick(object sender, IRibbonControl control, bool pressed) {
    foreach (AddinExpress.XL.ADXExcelTaskPane taskPane in adxExcelTaskPanesCollectionItem1.TaskPaneInstances) {
        (taskPane as ADXExcelTaskPane1).SetLabel();
    }


For me, it prints window captions correctly for all windows opened in Excel 2013.


Andrei Smolin
Add-in Express Team Leader
Posted 01 Jul, 2013 06:17:00 Top
Andrei Smolin


Add-in Express team


Posts: 18830
Joined: 2006-05-11
sylvain.piron writes:
'Microsoft.Office.Interop.Excel.Window' does not contain a definition for 'Hwnd' and no extension method 'Hwnd' accepting a first argument of type 'Microsoft.Office.Interop.Excel.Window' could be found (are you missing a using directive or an assembly reference?)


You use an interop for an earlier Excel version. You get this becasue Window.Hwnd was introduce in Excel 2013. If you need to support an earlier Excel version, you can use an interop for that Excel version and invoke Window.Hwnd via late binding. See also http://www.add-in-express.com/creating-addins-blog/2010/03/16/interop-assemblies-late-binding/


Andrei Smolin
Add-in Express Team Leader
Posted 01 Jul, 2013 06:20:36 Top
Sylvain Piron




Posts: 24
Joined: 2013-02-26
Thanks for the interop answer.

It does work well when i use a ribbon_button. But when i use:

        private void adxExcelEvents_WorkbookActivate(object sender, object hostObj)
        {
            foreach (AddinExpress.XL.ADXExcelTaskPane taskPane in adxExcelTaskPanesCollectionItem1.TaskPaneInstances)
            {
                (taskPane as ADXExcelTaskPane1).SetLabel();
            } 
        }


It does work well for then first one this time but not for the next. It will refresh the pane after 2 workbook_activate event. Any idea?

Regards

Sylvain.
Posted 01 Jul, 2013 07:38:22 Top
Andrei Smolin


Add-in Express team


Posts: 18830
Joined: 2006-05-11
Sylvain,

I suppose this occur because WorkbookActivate and showing a pane occur indepently. I suggest that you rewrite the code so that it is initiated from the ADXBeforeTaskPaneShow event.

That is, in the ADXBeforeTaskPaneShow event, a pane finds out the Excel.Window it is associated with, retrives the Workbook which is a Window.Parent and modifies the label to reflect the workbook name. What do you think?


Andrei Smolin
Add-in Express Team Leader
Posted 01 Jul, 2013 09:52:43 Top
Sylvain Piron




Posts: 24
Joined: 2013-02-26
Andrei,

If I do understand well i should do:

private void ADXExcelTaskPane1_ADXBeforeTaskPaneShow(object sender, ADXBeforeTaskPaneShowEventArgs e) { 
    hostHandle = (IntPtr)GetType().InvokeMember("HostHandle", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.GetProperty | System.Reflection.BindingFlags.Instance, null, this, null);
SetLabel();             
} 

I will get back to :

it does work for the second, third, ... workbook but not for the first one because the event ADXBeforeTaskPaneShow is raise when the start page in Excel 2013 is show and it is not raise a second time when the first workbook is open. Any workaround for this part?


Maybe I misunderstood because English is not my first language.

Thank again for your help and your patience.

Sylvain.
Posted 01 Jul, 2013 11:39:42 Top
Andrei Smolin


Add-in Express team


Posts: 18830
Joined: 2006-05-11
Sylvain Piron writes:
If I do understand well i should do:


Exactly.

Sylvain Piron writes:
I will get back to :

===
it does work for the second, third, ... workbook but not for the first one because the event ADXBeforeTaskPaneShow is raise when the start page in Excel 2013 is show and it is not raise a second time when the first workbook is open. Any workaround for this part?
===


Sylvain, the above is an equivalent of this statement: "the task pane isn't shown for the very first workbook opened in Excel 2013". But is the statement correct? Is the task pane shown or not when you open the first workbook in Excel 2013?

The add-in below works fine for me in Excel 2013 32bit:
http://www.add-in-express.com/files/support/MyAddin255.zip


Andrei Smolin
Add-in Express Team Leader
Posted 02 Jul, 2013 07:03:21 Top
Sylvain Piron




Posts: 24
Joined: 2013-02-26
Andrei,

The taskpane is visible when the first workbook is open and the label is empty because the event ADXBeforeTaskPaneShow is call at the excel 2013 startpage, before the first workbook is open.
The result of this is that the setlabel function is executed before the first workbook was open and then it doesn't show the windows caption.

Best regards
Sylvain.
Posted 02 Jul, 2013 07:14:00 Top