Author Topic: Visio VBA Macro Hiding/Showing Layers Issue  (Read 4094 times)

0 Members and 1 Guest are viewing this topic.

IS2021

  • Newbie
  • *
  • Posts: 3
Visio VBA Macro Hiding/Showing Layers Issue
« on: July 09, 2021, 03:52:53 PM »
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

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 4043
  • Ideas Visio-lized into solutions
Re: Visio VBA Macro Hiding/Showing Layers Issue
« Reply #1 on: July 10, 2021, 11:22:48 AM »
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
« Last Edit: July 11, 2021, 07:57:31 AM by wapperdude »
Visio 2019 Pro

IS2021

  • Newbie
  • *
  • Posts: 3
Re: Visio VBA Macro Hiding/Showing Layers Issue
« Reply #2 on: July 20, 2021, 10:35:18 AM »
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

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 4043
  • Ideas Visio-lized into solutions
Re: Visio VBA Macro Hiding/Showing Layers Issue
« Reply #3 on: July 20, 2021, 11:42:49 AM »
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

  • Sr. Member
  • ****
  • Posts: 324
    • PC Mimic Diagram
Re: Visio VBA Macro Hiding/Showing Layers Issue
« Reply #4 on: July 21, 2021, 03:16:27 AM »
You could try sprinkling a few DoEvents in your code
Live life with an open mind

IS2021

  • Newbie
  • *
  • Posts: 3
Re: Visio VBA Macro Hiding/Showing Layers Issue
« Reply #5 on: July 27, 2021, 10:15:13 PM »
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!