Visio VBA Macro Hiding/Showing Layers Issue

Started by IS2021, July 09, 2021, 08:52:53 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

IS2021

Problem Background:
I have written a VBA macro to hide and show several different layers on several different page. I begin with a page that has three bubbles each assigned to three different layers. The macro creates four pages identical to the first except for hiding and showing layers based on the following layout:

Original:
Page 1: Layers 1, 2, and 3 visible
Created by Macro:
Page 2: Layer 1 Visible
Page 3 Layers 1 and 2 Visible
Page 4: Layers 1 and 3 Visible
Page 5: Layers 1, 2, and 3 Visible

This is implemented in VBA by duplicating the previous page and then using loops to hide and show layers with the following formulas

Hiding:      Application.ActivePage.Layers(j).CellsC(visLayerVisible).Formula = "0"
Showing:  Application.ActivePage.Layers(j).CellsC(visLayerVisible).Formula = "1"

Problem:
The issue presents itself on the page 5. Although all layers are checked "visible" in the layer properties window, only layers 1 and 3 are actually displayed. Additionally, when I step through my code line-by-line in the debugger, the last page appears correctly, with all Layers visible. It is only when I run my code from start to finish that the problem arises. 

A method to recover the layers not displayed is to open layer properties, uncheck any layer's visible box, click apply, then recheck the box and click apply again. This seems to indicate that the layers were indeed visible before, but Visio failed to display them. My hypothesis is that the page needs to be visually refreshed somehow after I run the macro. Does anyone know of a VBA command that would accomplish this? Or can you think of something else that could help solve this problem?

Thanks in advance!

wapperdude

#1
Have you tried, after adding page,  first setting all layers to hidden, then loop thru and setting desied layers to visible?  you can turn screen refresh off initially, then do your programming chores, and finally turn screen update back on.

See https://docs.microsoft.com/en-us/office/vba/api/visio.application.screenupdating
Visio 2019 Pro

IS2021

Hello There. Thank you for the reply. I tried what you suggested but experienced the same result. The main issue still appears to be that the code behaves differently when run line-by-line as opposed to all at once (line-by-line my code works; all at once, it fails). Is this something you've experienced before?

wapperdude

Occasionally.  Would be helpful if you upload Visio example to see if the problem is consistent.  Also, might allow more in-depth investigation.
Visio 2019 Pro

AndyW

You could try sprinkling a few DoEvents in your code
Live life with an open mind

IS2021

Thanks for the feedback. After trying these suggestions I discovered that the issue is a Visio idiosyncrasy that occurs when programmatically changing the active page after editing the layers. This is something I can live with for what I'm trying to do. Thanks for helping me get on the right track!