'Is there a way to keep the original formatting of the text after searching and replacing?

I want to search for a certain number and replace it with another. Both will be inputted by the user.

The problem is some of the text has multiple formatting styles in the text box and after searching and replacing, the format of the text is set to the same.

Before the code
enter image description here

After the code
enter image description here

Sub Catmain()

Dim oDoc As Document
Dim oView As DrawingView
Dim oText As DrawingTexts
Dim txt_to_src As String
Dim txt_to_place As String
Dim result As String

Dim n As Integer

n = 0
Set oDoc = CATIA.ActiveDocument
Set oSheets = oDoc.Sheets
Set oViews = oSheets.ActiveSheet.Views
Set oView = oViews.ActiveView
Set oTexts = oView.Texts
txt_to_src = InputBox("Give me some input")
txt_to_place = InputBox("Give me some input")

For Each SrcText In oTexts
    result = SrcText.Text
    If InStr(result, txt_to_src) Then
    result = Replace(result, txt_to_src, txt_to_place)
    SrcText.Text = result
        n = n + 1
    End If
Next
MsgBox n & " text frames have been replaced"

End Sub


Solution 1:[1]

This happens if part of the string are different formatted.
One complex solution could be:

  • check if the DrawingText includes the searched text
  • for each char get and save the text properties (using GetParameterOnSubString with most or all properties of the CatTextProperty-enum) in a buffer
  • replace the text
  • format each char of the DrawingText using SetParameterOnSubString with the buffered information

EDIT: The following routine shows a rough possible process.

  • Tested as CATScript only
  • Substitution works only with the same number of characters
  • only two text properties considered

The code needs further improvements.

Sub ReplaceStringInDrawingText(oDrawingText as DrawingText, sSearch as String, sReplace as String)
    
    Dim sText as String
    Dim vTextPropBuffer() as Variant
    Dim oTextProperty() as CatTextProperty 
    Dim iChar as Integer
    Dim iProperty as Integer    
    
    sText = oDrawingText.Text
    if InStr(sText,sSearch)= 0 then
        Exit Sub
    end if

    'define text properties
    ReDim oTextProperty(1)
    oTextProperty(0) = catBold
    oTextProperty(1) = catItalic
    
    'buffer text properties per char
    ReDim vTextPropBuffer(Len(sText) - 1, UBound(oTextProperty) )

    For iChar = 1 to Len(sText)
        For iProperty = LBound(oTextProperty) to UBound(oTextProperty)
            vTextPropBuffer(iChar-1, iProperty) = oDrawingText.GetParameterOnsubString(oTextProperty(iProperty), iChar, 1)      
        Next
    Next
    
    'replace text
    oDrawingText.Text = Replace(sText, sSearch, sReplace)
    
    'set properties back to chars
    For iChar = 1 to Len(sText)
        For iProperty = LBound(oTextProperty) to UBound(oTextProperty)
            oDrawingText.SetParameterOnsubString oTextProperty(iProperty), iChar, 1, vTextPropBuffer(iChar-1, iProperty)    
        Next
    Next
End Sub

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1