Visio Guy

Visio Discussions => ShapeSheet & Smart Shapes => Topic started by: Visisthebest on February 14, 2023, 05:17:21 PM

Title: Can't get SETATREF() to do this with external data (in shape data cell)
Post by: Visisthebest on February 14, 2023, 05:17:21 PM
David Parker wrote a very useful article about how any shapesheet cell can be updated with external data:

https://bvisual.net/2022/04/18/update-any-visio-shapesheet-cell-with-external-data/

It involves very simply:

"This can be fixed by entering a SETATREF(...) function into the Shape Data value cells whose values you want to re-direct to another cell."

Unfortunately this doesn't work when I do this, even though the data graphics work fine, refreshing the data works fine, but not on the Shape Data row I put the SETATREF() on, as per David's instructions:

"This can be fixed by entering a SETATREF(...) function into the Shape Data value cells whose values you want to re-direct to another cell. In this case the formulas =SETATREF(Width) and =SETATREF(Height), are entered into the Prop._visDM_Width.Value and Prop._visDM_Height.Value cells."

Any idea why this doesn't work? Thank you for sharing your insights!
Title: Re: Can't get SETATREF() to do this with external data (in shape data cell)
Post by: wapperdude on February 14, 2023, 07:58:02 PM
Generally, you need to a more complete expression:  setatref(width,setatrefexpr(setatrefeval()))

In this case, you might be able to drop the setatrefexpr().  The data value will become inherited into the eval fcn upon execution, but you may want to "seed" it with some initial value.

See http://visguy.com/vgforum/index.php?topic=6383.0 (http://visguy.com/vgforum/index.php?topic=6383.0) for more indepth setatref() development.
See https://learn.microsoft.com/en-us/office/client-developer/visio/setatref-function (https://learn.microsoft.com/en-us/office/client-developer/visio/setatref-function) for definition/description.
Title: Re: Can't get SETATREF() to do this with external data (in shape data cell)
Post by: Visisthebest on February 15, 2023, 09:11:22 AM
I will try a more complete expression the simple solution doesn't work for data coming in from a data connection, if I change the data in the data source and refresh in Visio still nothing with the simple SETATREF expression.
Title: Re: Can't get SETATREF() to do this with external data (in shape data cell)
Post by: Croc on February 15, 2023, 09:31:24 AM
Visisthebest, please upload a Visio file with a non-working example (or two files: Visio + data source). I would like to take a look at it.
Title: Re: Can't get SETATREF() to do this with external data (in shape data cell)
Post by: Croc on February 15, 2023, 09:49:33 AM
This is the test result on my machine. Visio 2019.
Title: Re: Can't get SETATREF() to do this with external data (in shape data cell)
Post by: Visisthebest on February 15, 2023, 10:29:46 AM
Croc thank you I am using SETATREF to set the text in a subshape, pushing a string in the data connection with SETATREF(Sheet.ID!TheText), that doesn't work unfortunately. (ID = Shape ID of the subshape)
Title: Re: Can't get SETATREF() to do this with external data (in shape data cell)
Post by: Croc on February 15, 2023, 11:07:19 AM
I don't understand. The TheText cell is an event cell. How can you write data to it?
Title: Re: Can't get SETATREF() to do this with external data (in shape data cell)
Post by: Visisthebest on February 15, 2023, 11:43:55 AM
With Sheet.ID!TheText or is that read-only?
Title: Re: Can't get SETATREF() to do this with external data (in shape data cell)
Post by: Croc on February 15, 2023, 11:56:55 AM
QuoteTheText Cell (Events Section
An event cell that is evaluated when a shape's text or text composition changes.
Event cells are evaluated only when the event occurs, not upon formula entry. You can use the TheText cell to trigger recalculations, for example, to recalculate the text width and height with the TEXTWIDTH( ) and TEXTHEIGHT( ) functions.
Title: Re: Can't get SETATREF() to do this with external data (in shape data cell)
Post by: Visisthebest on February 15, 2023, 01:22:54 PM
Thank you Croc my mistake, now I'm reading the user data cell with a formula in the text, and this works fine and updates well.

I am so used to reading/writing Shape.Text in VBA, that I forget it is not possible to write to Text with a shapesheet formula.
Title: Re: Can't get SETATREF() to do this with external data (in shape data cell)
Post by: Visisthebest on February 15, 2023, 02:10:20 PM
If I create a User.Test row also it does not work (I get a 0 in User.Test not the text string). The issue is not confined to pushing data to TheText.
Title: Re: Can't get SETATREF() to do this with external data (in shape data cell)
Post by: Croc on February 15, 2023, 03:14:27 PM
Try to use User.Test.Prompt
Title: Re: Can't get SETATREF() to do this with external data (in shape data cell)
Post by: wapperdude on February 15, 2023, 03:43:03 PM
Couple things.  1st, setatref does not work with every shapesheet section.  See the 2nd link of post above.
2nd, technically, there is no "Text Cell", technically.  Or is there?!?  There is the Text Fields section of the shapesheet.  That ca n  be exploited.  See macro...

Sub addFieldTxt()
'tHIS MACRO ADDS TEXT TO A SHAPE WHERER NONE EXISTED.
'The desired text is stored i n variable MyTxt
'The code adds the Text Fields Section to the shapesheet, with desiredtext.
'If Text Field Section exists, then, no  need to add it, just nwrite to it.

    Dim shp As Visio.Shape
    Dim vChars1 As Visio.Characters
   
    MyTXT = "Some text"
   
    Set shp = ActiveWindow.Selection(1)
   
    If Not shp.SectionExists(visSectionTextField, False) Then
        Set vChars1 = shp.Characters
        vChars1.Begin = 0
        vChars1.End = 0
        vChars1.AddCustomFieldU Chr(34) & MyTXT & Chr(34), visFmtNumGenNoUnits
    End If
End Sub
Title: Re: Can't get SETATREF() to do this with external data (in shape data cell)
Post by: wapperdude on February 15, 2023, 04:22:28 PM
To give an idea beyond the simple of the above text, there's this code:

Sub addFieldTxt()
    Dim shp As Visio.Shape
    Dim vChar1 As Visio.Characters
    Dim lstChr As Integer
   
    Set shp = ActiveWindow.Selection(1)
   
    If Not shp.SectionExists(visSectionTextField, False) Then
        shp.Characters.AddCustomFieldU Chr(34) & "NewFieldText" & Chr(34), visFmtNumGenNoUnits
       
'The next 2 sections show two methods of adding additional lines.
'Both lines show the same info:  the name of the shape.
'The 1st use text entry, the 2nd uses AddFieldEx

        lstChr = Len(shp.CellsSRC(visSectionTextField, 0, visFieldValue).ResultStr(visNone))
        Set vChar1 = shp.Characters
        vChar1.Begin = lstChr           'sets reference at end of string
        vChar1.End = lstChr
        vChar1.Text = "" & Chr(10) & "" 'sets new line
        vChar1.Begin = lstChr + 1       'sets reference at beginning of new line
        vChar1.End = lstChr + 1
        vChar1.Text = shp.Name          'adds shape name on the 2nd line as normal shape text
       
        lstChr = lstChr + 1 + Len(shp.Name)
        vChar1.Begin = lstChr           'increments to end of 2nd line
        vChar1.End = lstChr
        vChar1.Text = "" & Chr(10) & ""
        vChar1.Begin = lstChr + 1
        vChar1.End = lstChr + 1

'This next line adds the shape name as Field Text
        vChar1.AddFieldEx visFCatObject, visFCodeObjectName, visFmtStrNormal, 1033, 0
    End If
   
End Sub
Title: Re: Can't get SETATREF() to do this with external data (in shape data cell)
Post by: Visisthebest on February 15, 2023, 04:30:58 PM
Thank you Wapperdude good to know what is possible with the shape text 'field'!
Title: Re: Can't get SETATREF() to do this with external data (in shape data cell)
Post by: Surrogate on February 15, 2023, 04:53:05 PM
Quote from: Visisthebest on February 15, 2023, 04:30:58 PM
Thank you Wapperdude good to know what is possible with the shape text 'field'!
But keep in mind
Quote from: MS in article addcustomfield (https://learn.microsoft.com/en-us/office/vba/api/visio.characters.addcustomfield#remarks)Using the AddCustomFieldU method is similar to clicking Field on the Insert tab and inserting a custom formula field in text. To add any other type of field (not custom), use the AddField method.
Title: Re: Can't get SETATREF() to do this with external data (in shape data cell)
Post by: wapperdude on February 15, 2023, 06:52:53 PM
For the curious, back in 2014 I did comparison of field vs shape text, for fun...must've been bored.  See the 4th reply in this topic:  http://visguy.com/vgforum/index.php?topic=5678.msg22615#msg22615 (http://visguy.com/vgforum/index.php?topic=5678.msg22615#msg22615)
Title: Re: Can't get SETATREF() to do this with external data (in shape data cell)
Post by: wapperdude on February 16, 2023, 09:23:50 PM
If I still have someone's attention...
New file that shows various coding techniques to add text to shape.  Mainly, the focus was on adding (inserting) field text as this creates an addressable shapesheet entry point (cell).  There are 3 shapes and 3 macros.  Each shape started as a simple, blank rectangle.  The macros did the rest of the work.

There are two take-aways.  One, it is possible to write text to a cell and have it displayed.  Two, regular shape text is much more user friendly wrt editing / formatting the text.  Field text can be embellished, but is not nearly as flexible and easy to manipulate.
Title: Re: Can't get SETATREF() to do this with external data (in shape data cell)
Post by: Visisthebest on February 16, 2023, 09:39:18 PM
Very useful Wapperdude thank you, on double-click on a shape you can show a pop up text editor to the user which is user-friendly enough.