How to access an already open visio page from a script attached to a different document

109 Views Asked by At

I have one .vssm script file in vba attached to a .vsdm script-enabled document on one side, and hundreds of .vsd and .vsdx files on the other.

With the .vssm script file open in the vba editor in the background, I want to be able to manually open any number of .vsd or .vsdx files, and run macros on any one of them, depending on the requirements specific to those files. I want to be able to access the selected shape in the selected page in the Visio document on the top of the pile.

When I use the code ActivePage or Application.ActiveWindow.page in the script I don't get the page of the document that currently is on top of the focus for Visio, but rather I get the page of the .vssm file to which the script is attached.

I already have a loop in place for batch processing a series of files with code like. This WORKS, but it is NOT WHAT I AM TRYING TO DO:

    ' Create a new instance of Visio
    Set visioApp = CreateObject("Visio.Application")
    ' Disable alerts so that Visio doesn't prompt for confirmation to overwrite files
    visioApp.AlertResponse = 7
    ' Loop through each Visio file in the folder
    filePath = Dir(visioPath & "\*.vsd*")
    Do While filePath <> ""
        ' Open the Visio file
        Set visioDoc = visioApp.Documents.Open(visioPath & "\" & filePath)
        ' Loop through each page in the Visio file
        For Each visioPage In visioDoc.Pages

I want to be able to open any file I choose and custom-process it, depending on the requirements of that specific file. I want to be able to run a macro on the currently selected shape inside the current page of that file, without having to attach the script to that file.

EDIT ****************************************************

I tried this code as suggested by @Paul_Herber, but I ONLY got the pages of the .vsdm doc that has the script attached, NOT other already open Visio files:

Dim doc As Document, p As page 
For Each doc In Application.Documents 
    For Each p In doc.Pages 
       Debug.Print (p.name) 
    Next 
Next 
End Sub
2

There are 2 best solutions below

1
Paul Herber On

ActivePage always refers to the page in the active document, but this is a read-only property. If you want to refer to another document then you will need to explicitly do so, either by index or by full path and name. e.g. .Documents[3].

or

.Documents['fred/documentname.vsdx'].

3
Surrogate On

but I only got the pages of the .vsdm doc that has the script attached, not other open Visio files

Application and visioApp are different instances!
Application is instance which related with document contained this macro…

Sub ttt()
Set visioApp = CreateObject("Visio.Application")
    ' Disable alerts so that Visio doesn't prompt for confirmation to overwrite files
    visioApp.AlertResponse = 7
    visioPath = "C:\7-40\" ' my test folder
    ' Loop through each Visio file in the folder
    filePath = Dir(visioPath & "*.vsd*")
    Do While filePath <> ""
        ' Open the Visio file
        Set visioDoc = visioApp.Documents.Open(visioPath & "\" & filePath)
        ' Loop through each page in the Visio file
        For Each visioPage In visioDoc.Pages
            Debug.Print visioDoc.Name, visioPage.Name
        Next
    filePath = Dir()
    Loop
End Sub

This code works at my side


PS

When I use the code ActivePage or Application.ActiveWindow.page in the script I don't get the page of the document that currently is on top of the focus for Visio, but rather I get the page of the .vssm file to which the script is attached.

ActivePage and visioApp.ActivePage/ Application.ActiveWindow.Page and visioApp.ActiveWindow.Page also are different!!!