Trying to understand how to navigate the object model

Started by freshlychurnedbutter, February 11, 2022, 01:06:08 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

freshlychurnedbutter

I've generated these lines of code which goes into the Test Stencil and drops a "TerminalBrace" master onto the "Test Macros" page. And it works fine.

Dim mst As Visio.Master
   Dim pag As Visio.Page
   Set pag = Application.ActiveWindow.Page
   Set mst = Application.Documents.Item("Test Stencil.vssx").Masters.ItemU("TerminalBrace")
   Application.Windows.ItemEx("Test Macros").Activate
   Application.ActiveWindow.Page.Drop mst, 1, 1


However if I replace "Application.ActiveWindow.Page" with "pag" in the last line, VBA pops up with the error "Object variable or With block variable not set". In my mind it should work because I've set "pag" equal to that exact string of text. Any thoughts on why this is happening? Thanks all!

Croc

ActiveWindow points to the window that is currently active. But different windows can be active at different times. The Activate method activates another window.
Add the line
Debug.print Application.ActiveWindow.Page.Name
before and after the line
Application.Windows.ItemEx("Test Macros").Activate

freshlychurnedbutter

I tried adding "Debug.print Application.ActiveWindow.Page.Name"but it says that the object doesn't support this property or method.

freshlychurnedbutter

Okay I replaced Name with ID and I am able to see the printout of the ID numbers of the windows. I understand that different windows are activated but once I activate the "Test Macros" window, shouldn't "pag" still be pointing to the active window which is "Test Macros"? Or is it because "pag" was set when a different window was opened so it only points to that window?

Croc

Yes, it is because "pag" was set when a different window was opened so it points to that window?

freshlychurnedbutter

Ahh I see. I guess that's where the confusion lies. I thought "ActiveWindow" would refer to which ever window is currently active even if it's SET to an object variable. Learn something new everyday. Thanks!

Visisthebest

#6
freshlychurnedbutter trying to use objects and experimenting is a good way to discover the object model.

Microsoft's Visio object model documentation is pretty good:
https://docs.microsoft.com/en-us/office/vba/api/overview/visio

And this forum is amazing to learn all the stuff that Microsoft forgot to document! :D

The more I learn about Visio (Desktop), the more I discover what an amazing product it is!

(I forgot: use the macro recorder frequently, in the Visio-generated code you'll see one way you can use the objects to get a desired user result! Often you can optimize this code further for your purposes)
Visio 2021 Professional

wapperdude

Quote from: freshlychurnedbutter on February 11, 2022, 02:03:13 PM
Ahh I see. I guess that's where the confusion lies. I thought "ActiveWindow" would refer to which ever window is currently active even if it's SET to an object variable. Learn something new everyday. Thanks!

Yes...and no.  ActiveWindow DOES refer to the currently active window.

But, the SET statement assigns the currently active window to the variable, a static assignment.  It doesn't do a dynamic assignment such that variable will assume the value of whichever window is active.    It's a snapshot as it were.
Visio 2019 Pro