If Master Is Nothing Problem

J

John

Hi there,

I always seem to have problems with the Is Nothing statement. Basically, I
want to delete a master from a stencil if it exists but it falls over at the
If Not line (Error: "object name not found").

Well I know it's not been found - that's the whole point of Is Nothing!

Can anyone point out what I'm doing wrong?

Thanks

John

'Remove Circle master from Doc stencil
If Not vDoc.Masters.Item("Circle") Is Nothing Then
vDoc.Masters.Item("Circle").Delete
End If
 
J

JuneTheSecond

An easy way may be to kill error trap, using "On Error Resume Next" at before
the line, "is nothing".
 
J

JuneTheSecond

If you can not remove error trap, an alternative may be to use for loop to
find the master.
Dim vsoMasters As Visio.Masters
Dim mst As Visio.Master
Dim I As Long, N As Long
Set vsoMasters = ThisDocument.Masters
N = vsoMasters.Count
For I = 1 To N
Set mst = vsoMasters(I)
If mst.Name = "Circle" Then
mst.Delete
End If
Next
 
J

John

Hi June,

This is actually what I've ended up doing (well something similar), but why
does it throw a error in the first place? Surely if there is no master
named "Circle" then Masters.Item("Circle") really IS nothing, so why doesn't
this work, or have I misunderstood "Is Nothing"?

Thanks

John
 
J

JuneTheSecond

If item circle does not exist.
vDoc.Masters.Item("Circle") causes error first before the result is examined
whether it is nothing or not nothing.
You would be able to test by,
Dim vsoMaster As Master
Set vsoMaster = ThisDocument.Masters("Line")
Set vsoMaster = ThisDocument.Masters("Circle")
If "Line" exists and "Circle" not exists, then error occurs at "Circle", but
vsoMaster is not nothing but still remains "Line".
 
J

John

I see. So is there an easier way to test for this condition (other than
looping as you suggested earlier?

Perhaps something like:

If vDoc.Masters.Item("Circle") Is Error ?

or

If vDoc.Masters.Item("Circle") Exists ?

I mean really this is just a true or false situation - either it does or
doesn't exist?

Regards

Curious John
 
C

Chris Roth [ Visio MVP ]

I have a hunch the error might be somewhere else, but I'm too lazy to check.
Here are some speculations:

Are you sure that (Master is nothing) is failing?

Often times the problem is that folks are using For...Each, and deleting
objects from the collection inside the loop. This is a no-no, because you
are changing the collection while iterating through it. You need to make a
For i loop and count backwards. Now that I think of it, this probably isn't
your problem, but it might helps someone else...

Another idea, perhaps the loop is happening very fast and Visio is in a
weird state. Say 3 shapes all reference the same master. You delete that
master after you hit the first shape, but when you hit the 2nd and 3rd
shapes, they are still reporting that they have masters. This would cause a
problem. Remember, I'm just guessing here...

You might try building a collection of masters-to-delete, then processing
them after you're done looking at all of the shapes. If the master is
already in the collection, then simply don't add it. I use this pattern for
lots of Visio scenarios. You can use the VisioIsIdle event to clear out the
collMastersToDelete collection that you build...

Anyway, hopefully food for thought.

--

Hope this helps,

Chris Roth
Visio MVP
 
J

John

Hello Chris,

Thanks for this. They're all interesting points, but I don't think they
apply to my particular problem. I certainly hadn't considered your second
point, but in at least one test case I know that the master does not exist
in the doc stencil, but this is why I'm curious about the use of Is Nothing.

We almost need a MasterExists property (and in fact that's what I was
expecting with the Is Nothing part).

Anyway, thanks for your advice.

Best regards

John

PS - I do want to delete the doc stencil master as I drop a new instance
from another master first, so that over time I only need to make changes to
a single master rather than masters in multiple docs.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top