1
votes

We've developed a Word VSTO Add-In for Word 2010 with CustomTaskPanes and MVVM Support through VSTOContrib.
After upgrading to Word 2016/2019 our CustomTaskPanes show up randomly without any action from the user. It seems like that Word notices when a CustomTaskPane was used and wants to (re)open it automatically the next time.

For example, a CustomTaskPane opens while opening a new/exisitng document. Wouldn't be that bad, if it wouldn't glitch (open, close, open, close, ...) till it closes or stays open. If the CustomTaskPane stays open, it is unusable because it has no DataContext that was loaded by our Add-In.

This Code in ThisAddIn creates/removes CustomTaskPanes:

public CustomTaskPane AddTaskPane(UserControl userControl, string title, Window owner)
{
    return CustomTaskPanes.Add(userControl, title, owner);
}

public void RemoveTaskPane(CustomTaskPane taskPane)
{
    if (taskPane == null)
        return;

    CustomTaskPanes.Remove(taskPane);
}

The RibbonViewModel (ViewModel per Document/Window) calls the Code like this. The _addInHelper has events for creating/removing CustomTaskPanes to reach the ThisAddIn Code and returns the CustomTaskPane instance by callback. It also uses the IoC Container to resolve the view "CustomTaskPaneView".

// Gets called when a new Window opens or a new Document is opened
public override void Intialize(Document document) 
{
    // ...
    CreateCustomTaskPane();
    // ...
}

private void CreateCustomTaskPane()
{
    if (_customTaskPane != null)
        return;

    _addInHelper.AddTaskPane("CustomTaskPaneView", "Custom headline", CurrentWindow, result =>
    {
        _customTaskPane = result;
    });

    if (_customTaskPane == null)
    {
        _log.Error(...);
        return;
    }

    _customTaskPane.DockPositionRestrict = MsoCTPDockPositionRestrict.msoCTPDockPositionRestrictNoHorizontal;
    _customTaskPane.Width = Settings.Default.TaskPaneWidth;
    _customTaskPane.DockPosition = Settings.Default.TaskPanePosition;

    // TaskPane height and width are saved seperately for DockPositionFloating
    if (_customTaskPane.DockPosition != MsoCTPDockPosition.msoCTPDockPositionFloating)
    {
        // Set height and width for DockPositionFloating.
        // If the user drags the TaskPane to Floating, it will have the correct size.
        var oldDockPosition = _customTaskPane.DockPosition;

        _customTaskPane.DockPosition = MsoCTPDockPosition.msoCTPDockPositionFloating;
        _customTaskPane.Height = Settings.Default.TaskPaneHeight;
        _customTaskPane.Width = Settings.Default.TaskPaneWidth;
        _customTaskPane.DockPosition = oldDockPosition;
    }
    else
    {
        _customTaskPane.Height = Settings.Default.TaskPaneHeight;
        _customTaskPane.Width = Settings.Default.TaskPaneWidth;
    }

    // Saving/updating settings in these
    _customTaskPane.VisibleChanged += ContentControlsTaskPane_OnVisibleChanged;
    _customTaskPane.DockPositionChanged += ContentControlsTaskPane_OnDockPositionChanged;
}

When closing the Window/Document, this code is called:

public override void Cleanup()
{
    if (_customTaskPane != null)
    {
        SaveCustomTaskPaneProperties();

        _contentControlsTaskPane.VisibleChanged -= ContentControlsTaskPane_OnVisibleChanged;
        _contentControlsTaskPane.DockPositionChanged -= ContentControlsTaskPane_OnDockPositionChanged;

        // Checks if the COM Object was cleaned up already
        if (!_contentControlsTaskPane.IsDisposed())
        {
            // Tried to manually close the CustomTaskPane, but didn't help either
            if (_contentControlsTaskPane.Visible)
                _contentControlsTaskPane.Visible = false;

            // Cleanup the CustomTaskPane ViewModel instance
            var taskPaneViewModel = _contentControlsTaskPane.GetViewModel();
            taskPaneViewModel?.Dispose();

            _addInHelper.RemoveTaskPane(_contentControlsTaskPane);
        }
    }
}

This only happens while using Word 2016 and 2019 (we don't use 2013) and didn't happen with Word 2010 at all. After upgrading the VSTO Project to VSTO Add-In 2013 and 2016 for testing purposes, it doesn't get better.

Example:
enter image description here

I didn't find any Word options that could cause this. Any idea what this could cause and how to fix this / getting a workaround?


EDIT
Here is the updated code example WordTaskPanesBug

Steps to reproduce:

  1. Start Word / run project
  2. Click "Open" button
  3. Click "New document" button
  4. Click "New document" button, TaskPane gets opened (but won't glitch this time)

Also the CustomTaskPane glitches while closing the document in the example project, but not in our real project.

Old example gif

example gif

1
Does this happen with an empty add-in, or one with only an empty task pane? You probably need to work toward an MVCE unless what you posted is already that.Chris
@Chris I added an MVCE with the reproduced bugjulianstark999
I tried it in 2013 and it seems to do the same thing, but on step 5, which is what your gif shows as well. I don't see any issues when closing. But your project has multiple third party libraries, a binary resource, and a complex flow to the point that it doesn't seem like an MVCE. If that's really the minimum required to reproduce the issue, your problem is probably not with the VSTO API, it's with VSTOContrib, Prism, or Unity, and I don't know enough about those to say what's happening.Chris
@Chris The combination / programm structure could probably cause the glitches and the closing bug. But I don't think that Prism (which I didn't use except for ViewModel Binding) and Unity (just IoC creating/passing objects) do play a big role in this. Anyways I removed all dependencies and reduced all code I could from the repo and the opening bug still exists. I included thetest.docx resource to be able to quickly open the same document. Maybe you could have a look again? Thanks in advice.julianstark999

1 Answers

2
votes

I added indices to indicate which task pane is being displayed, which showed that the task pane being added when you create a new document the second time is from the first document (the one that closes when you create a new document for the first time, probably because it's empty).

I think the problem you're running into is this one: Creating and managing custom task panes for multiple documents in a VSTO Word addin