Problem with number of shapes

Started by Kris, April 01, 2012, 04:33:11 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Kris

Hello All,
First of all, I'd like to say that I'm starting with VB in Visio so don't judge me too hard ;-)

As I'm working on visio files which have the same structure always and I have to replace some texts there, I wanted to create some macro which would do it in couple of seconds than making this in 30 minutes or more.

Unfortunately I'm only half-successfull. I prepared macro which replaces some predefined texts in objects and it works. But only on same pages.
I don't know why. Macro itself is very simple. I tried to use "For Each ..." etc. but maybe I did something wrong.


Dim intCounter As Integer
    Dim intCounter2 As Integer
    Dim i As Integer
    Dim j As Integer
   
   
    intCounter = Application.ActiveWindow.Page.Shapes.Count
    intCounter2 = Visio.ActiveDocument.Pages.Count
       
       
    j = 0
   
    For j = 1 To intCounter2
   
    i = 0
       
    intCounter = ActiveDocument.Pages.Item(j).Shapes.Count
   
        For i = 1 To intCounter
   
   
        Set vsoCharacters1 = ActiveDocument.Pages.Item(j).Shapes.ItemFromID(i).Characters
        vsoCharacters1.Text = Replace(vsoCharacters1, "Text1", "Text2")
       
        Next i
   
    Next j


As you can see I check on every page all shapes and replace Text1 with Text2. The problem is that for example on page 2 I have more than 10 objects on page, but  intCounter = ActiveDocument.Pages.Item(2).Shapes.Count  shows me about 2 or 3 only. Can you explain me why ? Is it about sub-shapes or grouping ?

aledlund

when you use "ItemFromID(i)" it is looking for a shape.id, not an item. ShapeIds on a page are not necessarily contiguous (i.e they are not 1,2,3 but may be something like 1, 5, 7).

you might try something like

dim visShape as visio.shape
for each visShape in activedocument.pages.item(j).shapes

        Set vsoCharacters1 = visShape.Characters
        vsoCharacters1.Text = Replace(vsoCharacters1, "Text1", "Text2")

next visShape


al

Kris

#2
It doesn't work. I tried even running macro step by step. It doesn't even change previous pages which my program from first message was changing.

Paul Herber

Al's code won't do what you want of itself, it's just a code fragment showing you how to loop through all the shapes on a page, a working alternative to that part of your code.
Download and have a look at the Visio SDK, there are many code examples there. There are also many examples on this forum.

Electronic and Electrical engineering, business and software stencils for Visio -

https://www.paulherber.co.uk/