Solved (Kinda): Visio Instance Closes when Documents.Count = 0

Started by bobsupercow, February 16, 2011, 11:01:06 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

bobsupercow

Hello All,

First off, thanks VisGuy for the site. It's a fantastic resource.

Issue:

I'm writing a VSTO add-in in VB.NET for Visio 2007. This add-in is part of a larger process, and as such, Visio is kicked off by opening a file from Internet Explorer. While I personally wish that portion opened Visio directly, the project doesn't allow the ASP webapp to have that level of permissions, so we are stuck with that workaround. Since opening the document is only used to open up Visio, we don't actually need to interact with the document ,and it needs to be closed before opening any drawing we need to interact with via the Add-In.

It is at this point that I have run into a strange problem.
The app works perfectly when opening drawings directly (by double-clicking) or via Visio's Menu, however, if a drawing is opened using Internet Explorer (version 7, but it happens with any other browser as well.) there are issues. If Application.Document.Count ever reaches 0, the entire Visio instance closes. At first I thought this only happened when the document that was opened was closed, but after digging a little, it turns out as long as another document is open you can close the orignal one without issue.

It sounds like maybe a permissions issue since it only occurs when the document is opened via a browser and not directly from Explorer. Additionally, debugging by opening Visio.Exe with Command Line arguments pointing to the file does not replicate the problem, so the fact that the drawing is already part of the Documents collection before the AddIn_Startup event fires doesn't seem to be the issue either.

HACK To get around this issue:
Creates a temporary document hidden from the user and closes the DummyFile opened via IE within the ThisAddIn_Startup event handler.

       
       If Application.Documents.Count > 0 Then
           Dim tempDoc As Visio.Document = Application.Documents.AddEx("", _
                                                           Visio.VisMeasurementSystem.visMSDefault, _
                                                           Visio.VisOpenSaveArgs.visAddHidden)
           For Each doc As Visio.Document In Application.Documents
               If doc.Name = My.Settings.DummyFileName Then
                   doc.Close()
               End If
           Next
       End If


Has anyone ran into this type of issue before or have any ideas on what could be causing it/how to fix it? I'm not sure if this behavior is a "Feature" or a "Bug". I've searched MSDN and the SDK to no avail. Input?

bobsupercow

UPDATE!

I still don't know what about the permissions is causing the bug in the first place, but I've apparently fixed the problem.
Visio will safely close the document, without crashing as long as another document is opened within the same clause that called doc.close(), before it returns control to its caller. Weird right?

Out of curiousity, anyone have any ideas or theories about why it behaves this way?


   Private Sub ThisAddIn_Startup(ByVal sender As Object, _
                                  ByVal e As System.EventArgs) _
                                  Handles Me.Startup

        If Application.Documents.Count > 0 Then
            'Loop through each currently open document
            For Each doc As Visio.Document In Application.Documents
                If doc.Name = My.Settings.DummyFileName Then
                    'Close the dummy document
                    doc.Close()
                    'A drawing must be opened with this If Then Clause or the entire Visio Instance will crash.
                    ThisAddIn.vsoDoc = OpenCurrentDrawing(Application)
                    Exit Sub
                End If
            Next
            Exit Sub
        End If
End Sub