WordApp.ActiveDocument is not available when the file is a word template (dotx, dotm)

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

WordApp.ActiveDocument is not available when the file is a word template (dotx, dotm)
 
Niels Ziegler


Guest


I am developing an addin for Word 2010. I have multiple custom task panes. I can open and close documents and each time the task pane will open fine on a ribbon button click. Sometimes it didn'T and I realized this happens only when the file is of type word template (dotx,dotm). This is the method I call for opening the task pane of the needed type <T>

 private void SetTaskPaneVisibility<T>(bool visible, int itemIndex, int instanceIndex = 0)
        {
            if (TaskPanesManager.Items.Count > 0)
            {
                var taskpaneItem = TaskPanesManager.Items[itemIndex];
                            
                if (taskpaneItem.TaskPaneInstances.Count > 0 && taskpaneItem.TaskPaneInstances[WordApp.ActiveDocument.ActiveWindow.Index - 1].GetType() == typeof(T))
                {
                    _taskpanesVisible = visible;
                  
                        // get the instance for the current window
                        MyCustomTaskPane ctp = ((MyCustomTaskPane)taskpaneItem.TaskPaneInstances[WordApp.ActiveDocument.ActiveWindow.Index - 1]);
                        ctp.ShouldBeVisible = visible;

                        taskpaneItem.ShowTaskPane(); // needs to be called before activate, otherwise the second and following task panes will not go to the front

                        ctp.Activate();                

                }
            }
            else
                MessageBox.Show("Taskpane instance could not be found!", "Error", MessageBoxButtons.OK);
        }


Whenever I have a document of type template open, "WordApp.ActiveDocument" will throw an exception but the code continues regardless.('WordApp.ActiveDocument.ActiveWindow' threw an exception of type 'System.MissingMethodException').

I managed to go around it because I found that there is still a reference available in the HostApplication object ((Word.Application)HostApplication).ActiveDocument.

But is this by design, that the active document is lost on template files?
Posted 28 Feb, 2017 11:17:54 Top
Andrei Smolin


Add-in Express team


Posts: 18847
Joined: 2006-05-11
Hello Niels,

First, use this code:


[DllImport("user32.dll")]
internal static extern IntPtr GetActiveWindow();

private void adxRibbonButton1_OnClick(object sender, IRibbonControl control, bool pressed)
{
    IntPtr activeWindowHandle = GetActiveWindow();
    foreach (ADXWordTaskPane1 pane in taskPaneItem1.TaskPaneInstances)
    {
        if (pane.CheckHostHandle(activeWindowHandle))
        {
            pane.Show();
            break;
        }
    }
}


Second, modify your task pane class as follows:


public partial class ADXWordTaskPane1: AddinExpress.WD.ADXWordTaskPane
{
    public ADXWordTaskPane1()
    {
        InitializeComponent();
    }

    bool visibility = false;

    private void ADXWordTaskPane1_ADXBeforeTaskPaneShow(object sender, ADXBeforeTaskPaneShowEventArgs e)
    {
        Visible = visibility;
    }

    public new void Show()
    {
        visibility = true;
        base.Show();
        visibility = false;
    }

    public bool CheckHostHandle(IntPtr activeWindowHandle)
    {
        return HostHandle == activeWindowHandle;
    }
}



Andrei Smolin
Add-in Express Team Leader
Posted 01 Mar, 2017 06:08:15 Top
Niels Ziegler


Guest


So was I calling Show on the wrong window/instance of the task pane? But the pane would not open, even with only a single document.
Posted 01 Mar, 2017 07:15:16 Top
Andrei Smolin


Add-in Express team


Posts: 18847
Joined: 2006-05-11
Niels,

Niels Ziegler writes:
MyCustomTaskPane ctp = ((MyCustomTaskPane)taskpaneItem.TaskPaneInstances[WordApp.ActiveDocument.ActiveWindow.Index - 1]);


In the code above, you use the index of the Word.Windows collection to access the TaskPaneInstances collection but these are two different collections and the indices may differ. For a single document in Word 2010, you'll get a single task pane instance and an unknown number of Word.Window objects.


Andrei Smolin
Add-in Express Team Leader
Posted 01 Mar, 2017 07:35:43 Top