Background Information
In this second part, the functionality will be expanded. The text elements that have been placed directly in the active model and those who are in text nodes and cells of any depth will be checked.
Steps
To carry out the investigation of nested cells, I have created a subroutine called Complex Search. This is a recursive subroutine used for the iteration of the cells.
In addition, further call parameters are supported, where you can indicate whether or not the replcaement should be performed in the complex elements. With no parameter, this subroutine simply functions the same as the example in the first part with one exception. Text nodes are no processed without the option used.
Here is an example of how to do this:
Option Explicit ' Searches in text strings and nested cells Private sToFind As String ' Find text Private sToReplace As String ' Replace with this text Private isComplex As Boolean Sub TxtRep_complex() Dim CmdLine() As String Dim Ee As ElementEnumerator Dim Sc As New ElementScanCriteria ' As a separator between parameters, "|" is used. CmdLine = Split(KeyinArguments, "|") ' Cancel if incorrect parameters are given: If UBound(CmdLine) < 1 Then ' no parameter given MessageCenter.AddMessage "Replace Text: Missing Parameters, see details:", "Call was made with: " + KeyinArguments, msdMessageCenterPriorityError Exit Sub End If sToFind = Trim(CmdLine(0)) '1. Parameter for search text sToReplace = Trim(CmdLine(1)) '2. Parameter for new text ' Check if optional parameter = yes complex was given isComplex = False If UBound(CmdLine) > 1 Then If InStr(CmdLine(2), "complex") > 0 And InStr(CmdLine(2), "yes") > 0 Then isComplex = True End If ' If not looking at cells, only filter texts and text nodes: If isComplex = False Then Sc.ExcludeAllTypes Sc.IncludeType msdElementTypeText Sc.IncludeType msdElementTypeTextNode ' Also text node always search Else Sc.ExcludeNonGraphical End If Set Ee = ActiveModelReference.Scan(Sc) ' Browse current model and start the test routine: Do While Ee.MoveNext Call complexSearch(Ee.Current) Loop End Sub ' Subroutine for recursively browsing nested complex elements Sub complexSearch(oEle As Element) Dim EeSub As ElementEnumerator ' If a complex element or text node is found, check all sub-elements: If (oEle.IsComplexElement And isComplex) Or (oEle.Type = msdElementTypeTextNode) Then Set EeSub = oEle.AsComplexElement.GetSubElements Do While EeSub.MoveNext Call complexSearch(EeSub.Current) Loop ' Otherwise string compare, if a text is present: Else If oEle.Type = msdElementTypeText Then If oEle.AsTextElement.Text = sToFind Then oEle.AsTextElement.Text = sToReplace oEle.Rewrite End If End If End If End Sub
The call is made in this example about the subroutine TxtRep_complex, which can be called as in the previous example with 2 parameters:
vba run TxtRep_complex Findtext
The optional parameter Complex = yes specifies whether you want to search for text in the complex structures or not:
vba run TxtRep_complex Findtext | Replacetext | [complex=yes]
These 3 variables:
Private sToFind As String ' Find Text to replace Private sToReplace As String ' Replace found text with this Private isComplex As Boolean
have been declared outside the routine as private, so they are valid in all routines of this module and do not need to be entered when calling routines as parameters.
<< RETURN TO PART 1 <<