I suppose I should explain what I mean by dirty text first 🙂
I often display the value of a Shape Data row in Visio text, but sometimes the solution requires that the value is also editable. This is ok if the client accepts that all editing is done with the Shape Data window or dialog, but not if the user more naturally wants to edit the text in the shape. In my current solution, I want to pass the manually edited text to another process for checking, and then only update the Shape Data row when validated or accepted, before re-seting the text with the inserted field. My first task was to spot with text had been manually overtyped, so I decided that checking the value of a new User-defined Cell, called IsDirty, was best, and then this value can be used to change the color of the text (or it could be the LineColor or FillForegnd) to make this value visible.
When Insert / Field is used in Visio, it creates a Text Fields section which can hold the reference to the inserted field.
If the text is manually overtyped, then the rows in the Text Fields section get deleted.
So, I wanted to highlight when this has happened so it can be handled in code.
First, I added a User.IsDirty row with the formula:
=ISERR(Fields.Value)
This returns False when the Fields.Value cell exists, returns False if it does not. Unfortunately, the User.IsDirty formula is automatically reset to ISERR(REF()) when the text is overtyped because the cell that it referred to is now deleted.
So the next issue is to display this change in the UI, so I edited the Character.Color formula to change from Black to Red if IsDirty = True:
=GUARD(IF(User.IsDirty,2,0))
Now, I just needed to reset the User.IsDirty formula, so it will work again. Fortunately, the TheText cell in the Events section can react to a change in the text, so the following formula will re-instate the formula in the User.IsDirty cell:
=SETF(GetRef(User.IsDirty),"=ISERR(Fields.Value)")
This is possible because the Fields.Value is textin the above formula, so it will not get replaced with REF() in the SETF(…) formula.
So, I manually insert the Name Shape Data row back into the text, the color will automatically change back to Black.
Of course, the text field code be updated with some code, as follows, using the CALLTHIS(…) function:
The VBA sub-function ensures that the whole body of text is replaced with an inserted field:
Public Sub InsertField(ByVal shp As Visio.Shape, ByVal cellName As String)
Dim UndoScopeID1 As Long
UndoScopeID1 = Application.BeginUndoScope("Set Text Field")
Dim vsoChars As Visio.Characters
Set vsoChars = shp.Characters
vsoChars.Begin = 0
vsoChars.End = Len(shp.Characters)
vsoChars.AddCustomFieldU cellName, visFmtNumGenNoUnits
Application.EndUndoScope UndoScopeID1, True
End Sub
Note that this solution only works for a single block of text, and not for large amounts of text where there are multiple inserted fields.
Related blogs
Using Notepad++ to Edit Visio ShapeSheet formulas
Visio shapes get their smartness from the formulas that are entered into the ShapeSheet, but editing these formulas can be extremely tricky and prone to error because of the lack of a modern programmer’s interface. Formulas can be quite long (up to 64k characters) but even medium size ones like the one in User.GetWorkdays cell…
Adding a second Function header bar to Visio swimlanes
I was recently asked if a second function header bar can be added to the swimlanes in the cross-functional flowchart templates in Visio. Some swimlanes can get quite wide, so it can be useful to have a duplicate function header shape on the far-side too. It is quite simple to duplicate the existing function header…
More Parsing XML Data in Visio Shapes
My last article looked at parsing an XML string with a known structure and order of elements and attributes. This can be acceptable in some scenarios, but what if the elements or attributes are in a different order? Then it is necessary to use the element and attribute names rather than their index position. This…
Parsing XML data in Visio Shapes
One of my current projects uses XML data, and some of the values in the XML data control the display and content of Visio shapes. Therefore, I looked deeper into how the XML data can be used directly to control parts of the graphics. Although the external data linking feature in Visio Professional and Visio…
Counting glued and connected shapes in Visio
I got a surprise in one of my projects when I counted the shapes glued together using the Shape.GluedShapes(…) method … the sum of the filtered glued shapes just didn’t add up to the unfiltered count. So, I thought I should check the Shape.ConnectedShapes(…) method too … and there is a scenario where that has…
My new book on Visualizing Processes with Microsoft Visio has launched
Back in the early 1990s, there was an application called ABC Flowcharter that was the market leader for diagramming business flowcharts, but some of the brains behind Aldus PageMaker saw an opportunity to create something smarter, and left to write the Visio product, with the stated aim to overtake ABC Flowcharter within 2 years. They…
Leave a Reply