Solved

VBS to check if Visio file open and if so close it

Posted on 2016-11-11
4
156 Views
Last Modified: 2016-11-14
Option Explicit
MacroExample

Sub MacroExample()

	Dim AppVisio 
	Set AppVisio = CreateObject("visio.InvisibleApp")
    	Dim vsoDoc

    	Set vsoDoc = AppVisio.Documents.Open("PathName\Drawing1.vsd")

    	vsoDoc.ExecuteLine ("PageSel")

	vsoDoc.close
	appvisio.quit
    	Set vsoDoc = Nothing
    	set AppVisio = nothing
End Sub

Open in new window


Hi Experts,

I have the following code in a VBS/VB Script file which works to open another Visio file. The issue seems to be that it is not working when the file was already opened. Currently if file is open, it runs but does not call the macro (in the code)

Is there a one liner/force close the specific file if it was opened by the user already?

Appreciate help!
0
Comment
Question by:Dingo212
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 20

Expert Comment

by:darbid73
ID: 41884490
First thing you need to do is to use the get object instead which will try to find an existing already open Visio.

Dim AppVisio As Visio.Application

   On Error Resume Next

   Set AppVisio = GetObject(, "visio.application")

   If AppVisio Is Nothing Then
      Set AppVisio = CreateObject("visio.application")
   End If

Open in new window


Source of the code and explanation is here.

The above is pretty standard for all office programs.  The next part I honestly have never done as I have never needed to code for Visio, but it all seems similar to Word.

If you successfully use the get object method then Visio is already running.  Then you use the Documents collection. If it has a count of more than 0, then something is already open.  You need to loop through this list and check what is open.  Each item in the documents collection will be a Document from which you can check its Name for example to see if it is the same document you want.

Alternatively you could use the ActiveDocument property which would give you the currently active document the user is using.

You MAY be able to use the openEX to open a second version of the same document, but I doubt it.
0
 
LVL 56

Accepted Solution

by:
Bill Prew earned 500 total points
ID: 41884704
This seems to be working close to what I think you described, adjust and try it out there and see how it goes.

Option Explicit

MacroExample

Sub MacroExample()
   ' Define local variables
   Dim objVisio 
   Dim objDoc
   Dim blnVisioCreated

   ' Initialize Vision application reference variable
   Set objVisio = Nothing

   ' Assume Visio is already running, then try and get a reference to it
   blnVisioCreated = False
   On Error Resume Next
   Set objVisio = GetObject(, "Visio.Application")
   On Error Goto 0

   ' If Visio was not running already load it and remember that we created it
   If objVisio Is Nothing Then
      Set objVisio = CreateObject("Visio.InvisibleApp")
      blnVisioCreated = True
   End If

   ' See if out drawing is already open, if so close it
   For Each objDoc In objVisio.Documents
      If LCase(objDoc.Name) = "ee28982576.vsdm" Then
         objDoc.Close
         Exit For
      End If
   Next

   ' Open desired drawing
   Set objDoc = objVisio.Documents.Open("b:\ee\ee28982576\ee28982576.vsdm")

   ' Execute macro
   objDoc.ExecuteLine("PageSel")

   ' Close our drawing
   objDoc.Close

   ' If we loaded Visio then exit it now
   If blnVisioCreated = True Then
      objVisio.Quit
   End If
End Sub

Open in new window

~bp
0
 

Author Closing Comment

by:Dingo212
ID: 41886490
Thank you
0
 

Author Comment

by:Dingo212
ID: 41886491
I've added

   ' Close our drawing
   objDoc.Close [b]false[/b]

Open in new window


To close file without saving

I also have regrets on not giving some points to others who have given some input. My apologies. I am back in here after a while and the new way of assigning points messed it up!
0

Featured Post

Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
This article describes a serious pitfall that can happen when deleting shapes using VBA.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question