Visio Guy

Visio Discussions => Programming & Code => Topic started by: Nepherim on May 23, 2018, 06:33:45 PM

Title: Set Shape Attribute with Custom Function
Post by: Nepherim on May 23, 2018, 06:33:45 PM
My shape has two properties:

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

~ ~ David
Title: Re: Set Shape Attribute with Custom Function
Post by: Surrogate on May 23, 2018, 08:47:55 PM
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


Title: Re: Set Shape Attribute with Custom Function
Post by: Nepherim on May 23, 2018, 09:23:54 PM
I'm missing how the function tst() is being called -- that's the key part of this question I'm fumbling on.
Title: Re: Set Shape Attribute with Custom Function
Post by: Surrogate on May 23, 2018, 09:39:32 PM
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
Title: Re: Set Shape Attribute with Custom Function
Post by: Nepherim on May 23, 2018, 09:48:59 PM
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.
Title: Re: Set Shape Attribute with Custom Function
Post by: Surrogate on May 23, 2018, 11:09:24 PM
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))
Title: Re: Set Shape Attribute with Custom Function
Post by: Nepherim on May 24, 2018, 12:47:00 AM
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.
Title: Re: Set Shape Attribute with Custom Function
Post by: wapperdude on May 24, 2018, 06:10:19 AM
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
Title: Re: Set Shape Attribute with Custom Function
Post by: Croc on May 24, 2018, 07:35:22 AM
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
Title: Re: Set Shape Attribute with Custom Function
Post by: Croc on May 24, 2018, 07:39:29 AM
.
Title: Re: Set Shape Attribute with Custom Function
Post by: Nepherim on May 24, 2018, 03:00:51 PM
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!