Search code examples
c#.netoffice-interopvisio

Visio Interop Application events causing undesired behaviour


I'm trying to use Visio Application events. When instantiating a new Application object, and setting any event (i.e. BeforeDocumentClose), this appears to result in unable to restore the Visio window after minimizing it.

I'm using VS/C# 2013, Windows Forms, Visio 2013 (on Windows 7). Though my main code project is huge implementing exchange between various office applications using Add-Ins, the following simple code reproduces the same issue. It is a Windows Forms project (with added Reference to Microsoft.Office.Interop.Visio).

using Visio = Microsoft.Office.Interop.Visio;

Visio.Application app;
bool initialised = false;

private void visioButton_Click(object sender, EventArgs e)
{
    init();

    app.Documents.Add("c:\\test.vst"); // creates new document from template
}

void init()
{
    if (!initialised)
    {
        // only initialise once
        app = new Visio.Application();
        app.BeforeDocumentClose += app_BeforeDocumentClose;
        initialised = true;
    }
}

void app_BeforeDocumentClose(Visio.Document doc)
{
}

Issue #1: This is the main issue. Creating one or more Visio Documents, the Visio Window is not maximized after being minimized. No Exceptions thrown as far as I can see. Windows just does it's audible error 'ping'.

Issue #2: This is a secondary issue. Creating two or more Visio Documents, hovering over the Windows Taskbar, the preview windows show the waiting cursor instead of normal document preview.

Conditions: Issue #1 only occurs when using an event on the Application. Document, Page/Shape events don't cause any problem. All events are captured fine. Issue #2 always occurs, but this is less important for me.

I've been searching for this issue for a while, but can't find anything related to it, so any help is greatly appreciated.


Solution

  • I am not quite sure what is causing Visio to not respond to restore, but you can try the approach with "AddAdvise" instead:

    [ComVisible(true)]
    public partial class Form1 : Form, Visio.IVisEventProc
    {
        public Form1()
        {
            InitializeComponent();
        }
    
        Visio.Application app;
        bool initialised = false;
    
        private void button1_Click(object sender, EventArgs e)
        {
            init();
    
            app.Documents.Add("C:\\test.vst"); // creates new document from template
        }
    
        void init()
        {
            if (!initialised)
            {
                // only initialise once
                app = new Visio.Application();
                // app.BeforeDocumentClose += app_BeforeDocumentClose;
                app.EventList.AddAdvise(DocCloseEventCode, this, null, null);
                initialised = true;
    
                Application.DoEvents();
            }
        }
    
        const short DocCloseEventCode = unchecked((short)Visio.VisEventCodes.visEvtDoc + (short)Visio.VisEventCodes.visEvtDel);
    
        object Visio.IVisEventProc.VisEventProc(short eventCode, object source, int eventID, int eventSeqNum, object subject,object moreInfo)
        {
            if (eventCode == DocCloseEventCode)
                app_BeforeDocumentClose(subject as Visio.Document);
    
            return null;
        }
    
        void app_BeforeDocumentClose(Visio.Document doc)
        {
        }
    }