You are currently reviewing an older revision of this page.
Background Information
In this second part, the functionality will be expanded. The text eloements 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
sToReplace
' Replace with this text
isComplex
Boolean
Sub
TxtRep_complex()
Dim
CmdLine()
Ee
ElementEnumerator
Sc
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
End
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
UBound(CmdLine) > 1
InStr(CmdLine(2),
"complex"
) > 0
And
"yes"
True
' If not looking at cells, only filter texts and text nodes:
Sc.ExcludeAllTypes
Sc.IncludeType msdElementTypeText
Sc.IncludeType msdElementTypeTextNode
' Also text node always search
Else
Sc.ExcludeNonGraphical
Set
Ee = ActiveModelReference.Scan(Sc)
' Browse current model and start the test routine
:
Do
While
Ee.MoveNext
Call
complexSearch(Ee.Current)
Loop
' Subroutine for recursively browsing nested complex elements
complexSearch(oEle
Element)
EeSub
' If a complex element or text node is found, check all sub-elements:
(oEle.IsComplexElement
isComplex)
Or
(oEle.Type = msdElementTypeTextNode)
EeSub = oEle.AsComplexElement.GetSubElements
EeSub.MoveNext
complexSearch(EeSub.Current)
' Otherwise string compare, if a text is present:
oEle.Type = msdElementTypeText
oEle.AsTextElement.Text = sToFind
oEle.AsTextElement.Text = sToReplace
oEle.Rewrite
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
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 <<