Validation Rules are grouped within ValidationRuleSets. The UI provides the ability to import a rule set from another open Visio document, but the programmer can use the Add(NameU as String) or AddCopy(RuleSet as ValidationRuleSet[, NameU]) methods to create a new one.
You can retrieve a rule set by its index position in the collection, using ValidationRuleSets.Item(index), or by its ID using ValidationRuleSets.ItemFromID(ID). Once you have retrieved a rule set, you can read its Name (which can be edited to be a localized version), NameU, Description (which is displayed as the tooltip in the UI), or check if the RuleSets is enabled for validation.
The RuleSetFlags value determines if the rule set is visible in the Rules to Check drop-down in the UI.
The default value is 0 (VisRuleSetFlags.visRuleSetDefault), but you could change it to 1 (VisRuleSetFlags.visRuleSetHidden) if you do not want it to appear in the Rules to Check menu.
The following macro, EnumerateRuleSets, displays a list of the rule sets in the active document:
Public Sub EnumerateRuleSets()
Dim doc As Visio.Document
Dim ruleSet As Visio.ValidationRuleSet
Set doc = Visio.ActiveDocument
Debug.Print "EnumerateRuleSets : Count = _
" & doc.Validation.RuleSets.Count
Debug.Print , "ID", "Enabled", "RuleSetFlags", _
"Name", "Description"
For Each ruleSet In doc.Validation.RuleSets
With ruleSet
Debug.Print , .ID, .Enabled, ._
RuleSetFlags, .NameU, , .Description
End With
Next
End Sub
This will produce an output similar to the following:
EnumerateRuleSets : Count = 2
|
ID
|
Enabled
|
RuleSetFlags
|
Name
|
Description
|
1
|
True
|
0
|
Flowchart
|
Verify that Flowchart shapes are connected properly.
|
2
|
True
|
0
|
bVisual
|
Sample RuleSet
|
How do I add or update a rule set?
Well, you can always copy a rule
set from another document in the UI, but you can also create a new one
in code, or update an existing one. This can be done as follows:
Public Sub AddOrUpdateRuleSet()
Dim ruleSet As Visio.ValidationRuleSet
Dim ruleSetNameU As String
ruleSetNameU = "bVisual"
'Check if the rule set exists already
Set ruleSet = _
getRuleSet(Visio.ActiveDocument, ruleSetNameU)
If ruleSet Is Nothing Then
'Create the new rule set
Set ruleSet = Visio.ActiveDocument.Validation.RuleSets.Add(ruleSetNameU)
End If
ruleSet.Name = "Be Visual"
ruleSet.Description = "Example Rule Set"
ruleSet.Enabled = True
ruleSet.RuleSetFlags = visRuleSetDefault
End Sub
ValidationRuleSets collectionruleset, updatingPrivate Function getRuleSet(ByVal doc As Visio.Document, _
ByVal nameU As String) As Visio.ValidationRuleSet
Dim retVal As Visio.ValidationRuleSet
Dim ruleSet As Visio.ValidationRuleSet
Set retVal = Nothing
For Each ruleSet In doc.Validation.RuleSets
If UCase(ruleSet.nameU) = UCase(nameU) Then
Set retVal = ruleSet
Exit For
End If
Next
Set getRuleSet = retVal
End Function
Notice how the tooltip and displayed name are updated in the UI:
Of course, you can also delete a rule set which is done as follows:
Public Sub DeleteRuleSet()
Dim ruleSetNameU As String
ruleSetNameU = "bVisual"
'Check if the rule set exists already
If Not getRuleSet(Visio.ActiveDocument, ruleSetNameU) Is Nothing Then
'Delete the rule set
Visio.ActiveDocument.Validation.RuleSets(ruleSetNameU).Delete
End If
End Sub
You can use the NameU or Index of a rule set to retrieve it from the Validation.RuleSets collection.