Main Menu

Find/Replace

Started by jarogers, September 01, 2009, 12:38:34 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

jarogers

Hello,

I have a bit of a problem... I think

I have a drawing with a lot of shapes, all the shapes have the same user defined cells with the same data in them
but i want to change the values in all, going through one by one is not going to be an option and the find/replace function doesn't seem to work for values in User-Defined Cells.

Is there anyway i can replace the values in all of my shapes?

Thanks
JR

Visio Guy

You need to use a bit of code to do this. Here is some VBA code that sets a new string value in the cell User.Something, if it exists:


Dim shp as Visio.Shape
Dim pg as Visio.Page

For Each pg in Visio.ActiveDocument.Pages
  For Each shp in pg.Shapes

    If ( shp.CellExists("User.Something", Visio.VisExistsFlags.visExistsAnywhere) ) Then

      shp.Cells("User.Something").FormulaForce = Chr( 34 ) & "New String Value" & Chr( 34 )

    End If

  Next shp
Next Pg



Note: Chr( 34 ) is just a double-quotes character, which needs to be wrapped around string values that are being written to ShapeSheet cells.
For articles, tips and free content, see the Visio Guy Website at http://www.visguy.com
Get my Visio Book! Using Microsoft Visio 2010

hill041

Great example, thanks.

Is it possible to use this to find/replace shape data for a shape within a group?

Surrogate

Yes, you need use recursion ! You can find example
Quote from: spyrule on September 27, 2013, 01:40:35 PM
So the solution is this...
I got this from :
http://visio.mvps.org/vba/

and modified it to change the font only (see my two commented lines) This worked beautifully.

hill041

Thanks Surrogate but I've tried that code and its stumbling at the Call ProcessShape(shp) step. is that because the shp variable is not yet defined?

Public Sub ListGroup()
Dim vsoSelect As Visio.Selection
Dim vsoShape As Visio.Shape
Dim vsoShapes As Visio.Shapes

Set vsoSelect = Visio.ActiveWindow.Selection

If vsoSelect.Count > 0 Then
For Each vsoShape In vsoSelect
Call ProcessShape(shp)
Next vsoShape
Else
MsgBox "You Must Have Something Selected"
End If

End Sub

Public Sub ProcessShape(shp As Visio.Shape)
Dim subshp As Visio.Shape

Debug.Print shp.Name

If shp.Shapes.Count > 0 Then
For Each subshp In shp.Shapes
Call ProcessShape(subshp)
Next subshp
End If

End Sub

MsgBox "You Must Have Something Selected"
end if




Paul Herber

Try:
Call ProcessShape(vsoShape)
Electronic and Electrical engineering, business and software stencils for Visio -

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

hill041

Thanks for you time and response. Its not stopping now but the particular property I'm trying to change is still not changing.

Here is my code as I've adapted it. Can you see where I'm going wrong?


Public Sub ClearConnectorNumbers()
Dim vsoSelect As Visio.Selection
Dim vsoShape As Visio.Shape
Dim vsoShapes As Visio.Shapes

Set vsoSelect = Visio.ActiveWindow.Selection

If vsoSelect.Count > 0 Then
For Each vsoShape In vsoSelect
Call NumberShape(vsoShape)
Next vsoShape
Else
MsgBox "You Must Have Something Selected"
End If

End Sub

Public Sub NumberShape(vsoShape As Visio.Shape)
Dim subshp As Visio.Shape

If vsoShape.CellExistsU("Prop.LabelStart", True) Then

vsoShape.CellsU("Prop.LabelStart").Formula = ""

End If

If vsoShape.Shapes.Count > 0 Then
For Each subshp In vsoShape.Shapes
Call NumberShape(subshp)
Next subshp
End If

End Sub

wapperdude

I would use the "formula" syntax that Visio Guy shows with the chr(34)'s. 

Nonetheless, in general, if you have both the drawing window and VBA windows open, next to each other, so both can be seen...in drawing window, select your shape, then mouse click somewhere in the main code block.  Now, press "f8",  and the code will execute line by line with each f8 press, including the macro calls and their code.  (Oh, you should also open the shapesheet and make it visible to watch changes to it.)

This process allows you to see result of each step, and to see if code executes as expected.  Note, after a step executes, hover mouse over a portion of the step (or previous steps) to see what values the code is using for each variable.
Visio 2019 Pro

wapperdude

The code as you provided it, does run.  However, the displayed result shows "0" not a blank, which is presumably preferred.

Adding the chr(34) syntax corrects this.  Since this makes the entry a null string, rather than an empty numeric value.  For convenience, here's update to your code:

Sub ClearConnectorNumbers()
Dim vsoSelect As Visio.Selection
Dim vsoShape As Visio.Shape
Dim vsoShapes As Visio.Shapes

Set vsoSelect = Visio.ActiveWindow.Selection

If vsoSelect.Count > 0 Then
    For Each vsoShape In vsoSelect
        Call NumberShape(vsoShape)
    Next vsoShape
Else
    MsgBox "You Must Have Something Selected"
End If

End Sub
Visio 2019 Pro

wapperdude

...ooops, not all of the code pasted.

...ooops 2, can't paste right now ...

Work around:  attach Visio drawing with code
Visio 2019 Pro

hill041

Thanks Wapperdude, that seems to have done the trick!!  8)