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-setting 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 True 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 text in 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
Linking Data to Shapes in Visio after using Data Visualizer
Data Visualizer (DV) in Visio Plan 2 (Data | Create from Data | Create ) is great because it provides a way of automatically creating a diagram from data, but it also prevents some of the other data-linking features in Visio from being used. This is because DV wants to take control of the data…
Synchronizing Visio Shape Fill Color (or almost any cell) across pages
I was recently asked how the color of one shape can be changed and for other shapes to be automatically updated to the same color … even if they are on different pages! Well, it is possible with Microsoft Visio’s awesome ShapeSheet formulas. In fact, this capability is not limited to the FillForegnd cell ……
Positioning Visio Shape Text Block with a Control Handle
I was recently asked how a control handle can be added to a Visio shape so that it can be used to re-position the text block. Fortunately, it is extremely easy to setup, and requires just two formulas to be updated in the ShapeSheet. This is a great use of the SETATREF(…) function. (more…)
New Requirement for VBA Digital Signatures in Visio
Like most developers, I have to buy a new digital certificate every 3 years to sign my Visio add-ins and VBA projects. Usually that means verifying my bone fides, paying the fee and downloading the certificate, but security has been increased, and now, like everyone else, I have to use a USB key with it…
Understanding Segments of Visio Geometry
I recently had to revise my understanding of the POINTALONGPATH(…) function in Visio because I was getting a #REF! error in some cases. My particular scenario requires a line with a number of vertices that are initially all in a straight line but can be moved by dragging controls around that each vertex is bound…
Custom Color Themes in Visio?
I was recently looking into custom color themes for corporate branding in desktop Microsoft Visio and became re-aware how different Visio still is from the rest of the Microsoft Office applications. A Visio page or document does not need to have any theme applied, but the documents of the other Office applications always have a…