Exit Application after entering through Document_DocumentOpened

Started by Earl, September 22, 2014, 05:35:08 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Earl

Have Visio document that someone else is opening through C# code.  Once the Visio is opened, my VBA code takes over through the Document_DocumentOpened procedure, which called another procedure in a module.  During testing, I just had a button on my Visio document that I could press to execute the same code that the Document_DocumentOpened calls.  When calling using that button, the following code at the end of everything worked as a I desired:

    ThisDocument.Save
    Application.AlertResponse = 1
    Application.Quit

The document was saved and Visio was exited completely.

However, when I changed things to have the Document_DocumentOpened call the same procedure, the document was saved, but Visio would not exit.

For the time being, I've changed the code to this:

    ThisDocument.Save
    ActiveDocument.Close

This at least saves the document and then closes it, but the Visio application is still open.

I believe I have read that if you enter through Document_DocumentOpened, then once you do:
ActiveDocument.Close

all code would stop at that point, so even if I added these lines after that:
   Application.AlertResponse = 1
    Application.Quit

they would not be executed.

So, my question is, since I have to enter through Document_DocumentOpened, is there a way to have it run whatever VBA code I want it to, then save the document and then exit Visio completely?

Thanks
Earl

Nikolay

You are not allowed to close application from within a VBA event handler. "DocumentOpened" is an event handler.

Besides, it does not seem to makes much sense..
if you control Visio externally, then you control it's lifetime also (when it is started and when it is finished)..
If you are not in control of Visio, then you better not close it, or the user might get angry :)

Why would you need to close Visio from within?
I mean, there might be a way to do it, but what is the point?

AndyW

Have your document opened event just start a timer for a short period. Let the timer handler do the processing that was in the document opened event, this can then close the application as it it not within a Visio event.
Live life with an open mind

Jumpy

Or you could could force the system to kill the Visio process. But be careful with this, because it could kill every instance of Visio, not only the one you desire. Only found an example with Excel:


Sub Kill_Excel()
  Dim sKillExcel As String
  sKillExcel = "TASKKILL /F /IM Excel.exe"
  Shell sKillExcel, vbHide
End Sub


It uses TASKILL from Microsoft's Sysinternals, but I'd guess there are other way's to do this, too. Perhaps with API-funcitons.