julianstark999
julianstark999

Reputation: 3616

VSTO Add-In CustomTaskPane opens itself after upgrading to Word 2016/2019

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

Upvotes: 1

Views: 404

Answers (1)

Chris
Chris

Reputation: 3519

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

Upvotes: 2

Related Questions