Visio Guy

Visio Discussions => Programming & Code => Topic started by: healthNut on August 11, 2012, 02:41:50 AM

Title: Besides iterating through each item, is there way to check if layer exists?
Post by: healthNut on August 11, 2012, 02:41:50 AM
I'm accessing a layer from the layers collection by name:
Set layerObj = layersObj( "layerNameStr")
This crashes when layerNameStr item does not exist

I seen the for loop through each item to check if it exists but that seems inefficient
There's gotta be a better ways
-catch the error?
-layer.exist function?
Title: Re: Besides iterating through each item, is there way to check if layer exists?
Post by: aledlund on August 11, 2012, 10:59:36 AM
There are several places where assigning an object (i.e. layer, shape, etc.) can return 'nothing' or 'null'. Good programming will wrap these in some form of error catching. Example in .Net is try...catch..end try or in VBA 'on error goto something'. Remember when you build these mini-handlers to point your code back to the main handler when completed.

al


on error goto layerErrHandler
set layerObj = layersObj("layerNameStr")
layerErrHandler:
if layerObj is Nothing then
    ' go do something because you couldn't find it
end if
on error goto mainErrHandler

....

al
Title: Re: Besides iterating through each item, is there way to check if layer exists?
Post by: healthNut on August 11, 2012, 02:54:57 PM
Thanks, took your advice and error caught. Would appreciate help on the handling part though:
How do I Negate the "Is" operator? In other words, what's the VBA equivalent for != in C++?

I  have a getLayer function that returns nothing if the layer does not exist.
-Set layerObj = getLayer(layersObj, strSpecialty)

Then checking if nothing is returned
-If Nothing Is layerObj Then
which works fine

What if I want to negate it? I tried
-If Nothing <> layerObj and
-If Nothing Is Not layerObj
but get compilation errors of type mismatch.
What I got works, just looking for logical understanding.
Title: Re: Besides iterating through each item, is there way to check if layer exists?
Post by: aledlund on August 11, 2012, 06:29:57 PM
you might try

if Not (layerObj Is Nothing) then
end if


if layerObj is Nothing then
  ' did not find the layer
else
  ' found the layer
end if

this is of interest to know

http://www.tek-tips.com/faqs.cfm?fid=3710

al

Title: Re: Besides iterating through each item, is there way to check if layer exists?
Post by: healthNut on August 11, 2012, 09:37:41 PM
Thanks!
Title: Re: Besides iterating through each item, is there way to check if layer exists?
Post by: Visio Guy on August 13, 2012, 01:43:08 PM
I use a lot of "loop-scanners" in my code, because reading from Visio is quite fast. I don't like using Try-Catch for things like layers or shapes, since they can execute quite slowly if the error condition pops up.

I think it depends on if your code is expecting something or not. If you are trying to get a layer, and you expect it to be there 99.5% of the time, then a try-catch is in order.

If you are going to prompt the user to create a layer if it's not there, and it might very likely not exist, you should probably scan to see if it exists. Even 50 layers will be read very quickly (especially in relation to any user interaction that might follow) so it won't hurt to loop through them.
Title: Re: Besides iterating through each item, is there way to check if layer exists?
Post by: healthNut on August 13, 2012, 11:48:33 PM
Cool, thanks for the tips. Had no idea, looping would be faster but I did notice that error handling does take time.
Browser ID: smf (is_webkit)
Templates: 1: Printpage (default).
Sub templates: 4: init, print_above, main, print_below.
Language files: 1: index+Modifications.english (default).
Style sheets: 0: .
Hooks called: 59 (show)
Files included: 25 - 925KB. (show)
Memory used: 765KB.
Tokens: post-login.
Cache hits: 7: 0.00131s for 22,301 bytes (show)
Cache misses: 1: (show)
Queries used: 9.

[Show Queries]