Set Shape Attribute with Custom Function

Started by Nepherim, May 23, 2018, 06:33:45 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Nepherim

My shape has two properties:

  • Prop.A: Set by the user
  • Prop.B: Not user-modifiable. Derived with a 'complex' custom function, based on Prop.A (complex meaning multi-step VBA processing)

What's the approach to setting the value in Prop.B? Pointers appreciated!

~ ~ David

Surrogate

#1
You can hide Prop.B shape data via Define shape data window (you can do it if you run Visio in Developer Mode)
Or via ShapeSheet environment - set value TRUE in cell named Invisible
Hope my simple code can help
Sub tst()
Dim sh As Shape, pa As Integer, pb As Integer
Set sh = ActiveWindow.Selection.PrimaryItem
pa = sh.Cells("prop.a")
' i don't know what complex calculations you need
' i just devide this value by 2
pb = pa / 2
sh.Cells("prop.b") = pb
End Sub



Nepherim

I'm missing how the function tst() is being called -- that's the key part of this question I'm fumbling on.

Surrogate

1. Press keys Alt+F8
2. Select this sub-routine in Macros window
3. Press Run button in this window or just press key Enter on keyboard

Nepherim

Appreciate the help, but I'm actually looking to have this function applied to Prop.B automatically. So if the user changes Prop.A, then the function on Prop.B is auto-applied.

Initially I thought that CALLTHIS might help. But, CALLTHIS doesn't return a value, and the value would over-write the initial function anyway, so subsequent updates to Prop.A would not be captured.

Seems like SETREF should play into this somehow, but my brain has locked up.

Surrogate

We don't know what do you mean as 'complex' custom function?
May be you don't need use SETF. Just write formula with Prop.A argument
Prop.B = SQRT(POW(Prop.A,2)+POW(5,2))

Nepherim

I'm calculating a unique numeric value, based on user selections from two properties based on pull-down lists.
1. Determine index of values for each pull-down property
2. Use the index values in a formula to generate a unique number
3. Store this value in a non-editable property on the shape

At the moment I run the function post-edit, and OnSave. Just trying to see if there is a means by which I can simply make a function call from the shape object itself.

wapperdude

It would seem that the simplist solution is to do this all in the shapesheet and not use a macro at all.  The user makes the two index choices and the formula, say in a scratchpad cell, calculates the new value.  There ought to be sufficient shapesheet functions to make the necessary math equation.  When you say make a function call from the shape object itself, you can certainly create a call via double clicking the shape.  But, if you want this to be more automatic, then, I think you must invoke some sort of event triggering, and that is not from within the shape per se.

Wapperdude
Visio 2019 Pro

Croc

You can use the third cell. For example,
Sub ttt(shp As Visio.Shape)
    On Error Resume Next
    n = shp.Cells("Prop.A").ResultStr(0)
    n2 = CDbl(n)
    n1 = CDbl(n2) ^ 2
    If Err.Number <> 0 Then
        shp.Cells("Prop.B").Formula = Chr(34) & Err.Description & Chr(34)
    Else
        shp.Cells("Prop.B").Formula = Chr(34) & CStr(n1) & Chr(34)
    End If
    On Error GoTo 0
End Sub


Nepherim

I think the idea outlined by wapperdude, and demonstrated by Croc are what I'm looking for! Thanks all for the advice, and helping to think it through!