Georg Lackner
Georg Lackner

Reputation: 42

direct access to visio shape does not work

I have a question regarding accessing a shape in visio 2003

...
dim ovp as visio.page
dim ovs as visio.shape
...
set ovs = ovp.shapes("#shapename#")
...do something with the shape

This sometimes does not work!

It gives an error like shape not found or similar

...
dim ovp as visio.page
dim ovs as visio.shape
...
for each ovs in ovp.shapes

   if ovs.name = "#shapename#" then
   ...do something with the shape

   end if

next

This always works. Any idea why?

Upvotes: 0

Views: 291

Answers (2)

John Visio MVP
John Visio MVP

Reputation: 71

Where are you getting #shapename# from? As a test, inside the foreach ovs, put a debug.print ovs.name to see what names you are dealing with in that collection.

Upvotes: 0

Roger Rowland
Roger Rowland

Reputation: 26259

Here's a possibility:

When you do set ovs = ovp.shapes("#shapename#"), VBA is looking for an exact match for the shape name, in a case-sensitive manner.

When you do if ovs.name = "#shapename#" and if you have Option Compare Text defined in your module, then it will do a case-insensitive comparison.

So, in that particular situation, you may get the results you describe if both of the following are true:

  1. You have declared Option Compare Text
  2. You have a mismatch in the case of the shape name you're searching for. E.g the shape is named "#ShapeName#" but you are searching for "#shapename#".

Can you comment to clarify if any of this might apply?

Upvotes: 1

Related Questions