VBA to select all the shapes of all pages

Started by rodsoares101, March 28, 2009, 12:52:56 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

rodsoares101

Is there an way to select all the shapes of all pages via VBA?
Sub datagraphics()

' Select all shapes of active page
Application.ActiveWindow.selectall

' Apply datagraphics
Visio.ActiveWindow.Selection.DataGraphic = Visio.ActiveDocument.Masters("DataGraphic")

End Sub


In this macro all shapes are selected and then the data graphic is applied, but only on the active page. Is there a code to either select all shapes of every page and then apply the data graphics or select all the shapes of the active page apply the data graphics then go to the next page and keep doing it until all the pages were selected?

Trick one!

Paul Herber

Sorry, no, only the shapes on the active page can be selected, changing page causes any shapes on other pages to be deselected.

Electronic and Electrical engineering, business and software stencils for Visio -

https://www.paulherber.co.uk/

rodsoares101

Paul is there a looping function? Like run the code the go back to the begin and start over.

rodsoares101

Paul are you sure it can't be done via VBA, because I have a drawing that does exacly that, I just don't think it's done with VBA, it has a DLL and an access database linked to it. I can't send you the drawing because it's part of a software own by the company I work for and I can get in trouble for sharing it, but I can send you a video of the drawing seaching every page and adding icons to it. Let me know if you want it, It might give you some insight.

wapperdude

Does the video show each page being activated one at a time?

What Paul is saying is that you can only select shapes on the active page.  In VBA you can loop thru the document with For / Next, activate one page at a time and then do your shape thing.

Visio 2019 Pro

rodsoares101

Hey Wapperdude, I've been looking all over for an solution and I actually got it.
Test it out please

Sub datagraphics()
Dim PagsObj As Visio.Pages
Dim PagObj As Visio.Page

Set PagsObj = ActiveDocument.Pages

' iterate through the collection of pages
For Each PagObj In PagsObj
ActiveWindow.Page = PagObj.Name

' Select all shapes
Application.ActiveWindow.selectall

' Apply the data graphic
Visio.ActiveWindow.Selection.DataGraphic = Visio.ActiveDocument.Masters("Data Graphic Name")
Next
End Sub


Take care.
Rod.

wapperdude

Looks like it works just fine! I don't have the datagraphics aspect, so commented it out, but it does go thru the pages and selects the shapes as expected -- at least in my limited test case.
Visio 2019 Pro

John Distai

How would I modify this if I only wanted to select a specific type of shape on each page, such as a decision shape?

Thanks!

wapperdude

#8
One simple way is to add another For / next loop to iterate thru the shapes on the active page, and then use an IF then to examine for the desired shape and execute the desired code.

Such as:

Sub SAS()                                                   'Select A Shape
Dim PagObj As Visio.Page
Dim visShp As Visio.Shape

For Each PagObj In ActiveDocument.Pages     ' iterate through the collection of pages in the document.
      For Each visShp In PagObj.Shapes          ' iterate throught the shapes on active page.
            If visShp.Master = "Manager" Then   ' check for desired shape.
                '{enter code here}                    ' execute code pertaining to desired shape.
            End If
      Next
Next
End Sub

Substitute Manager with the desired shape name.

HTH
Wapperdude
Visio 2019 Pro

Paul Herber

Quote from: rodsoares101 on March 28, 2009, 01:34:30 PM
Paul are you sure it can't be done via VBA
D'oh, that'll teach me to read the question properly! Sorry.
Electronic and Electrical engineering, business and software stencils for Visio -

https://www.paulherber.co.uk/

John Distai

Thanks, Wapperdude!

One question though.  When I try that code, I get the following error:

"Runtime Error '91': Object variable or With block variable not set".

If I click on Debug, the following line is highlighted:

If visShp.Master = "Decision" Then

Any idea how to fix this?  I have the Dim statements included, and checked the spelling and word case.  Any help would be appreciated!

Thanks!


wapperdude

Did you copy the entire code?  If you don't have the For / Next loops, the variables for the If / Then loop won't get set properly.  Just having the wrong name in quotes shouldn't be a problem, just means the IF test fails.  The name does have to be inside double quotes.

I just re-tried this on a simple flowchart, simple copy/past into VBA editor...
a.)  left the manager in, and it found nothing
b.)  changed Manager to Decision, and it found both decision shapes.

Visio 2019 Pro

John Distai

I originally typed the code in.  I just retried it, and received the same error.  I did not include the code which originally started the thread.

I'm using Visio 2007.  Would that make a difference?

Thanks!

wapperdude

I'm using V2007, but, should be OK for both V2003 & V2007.  Not sure about previous to that.

Have you tried searching for that error?
Visio 2019 Pro

John Distai

I have, and it says that I'm not setting a variable.  Perhaps I need to use some sort of "set" statement.  Any idea why it would work on your system but not on mine?

Thanks!