I currently have a set of VB CATIA Macro codes that, when a user runs the code, it opens up a form that prompts the user to input the text that he/she wants to change.(See image for the form that will pop up) The code will then run through the whole drawings to search for and replace the text inputted by the user accordingly.
Currently, I am modifying the code so that the changes is to be applied to all the opened CAT Drawings.
What I want the code to do: User opens 5 CATIA drawings for example --> When user runs the code --> the Form opens up to prompt user to input Text to search for and Text to be replaced for --> Once user fills in for the two inputs and hit the 'Apply to all opened sheets' command button --> the code runs and changes all the 5 opened CATDrawings accordingly.
I'm new to the VB scripting language and have little to no experience with VB scripting language. Any help will be greatly appreciated.
Below are the working codes that will only apply the changes to 1 CATIA drawing opened.
'For the Module,
Sub CATMain()
MainForm.Show
End Sub
'For the Form,
Dim b As Variant
Dim a As String
Dim c As String
Dim selection1 As Selection
Dim drawingDocument1 As DrawingDocument
Private Sub CommandButton1_Click()
a = TextBox1.Value
b = TextBox2.Value
c = "CATDrwSearch.DrwText.TextString_CAP=*" & a & "*,all"
Set drawingDocument1 = CATIA.ActiveDocument
Set DrawSheets1 = drawingDocument1.Sheets
Set DrawSheet1 = DrawSheets1.ActiveSheet
Set selection1 = drawingDocument1.Selection
selection1.Search c
TextBox3 = selection1.Count
End Sub
Private Sub CommandButton2_Click()
a = TextBox1.Value
b = TextBox2.Value
c = "CATDrwSearch.DrwText.TextString_CAP=*" & a & "*,all"
Set drawingDocument1 = CATIA.ActiveDocument
Set selection1 = drawingDocument1.Selection
selection1.Search c
Text2 = selection1.Count
i = 1
For i = 1 To Text2
If Text2 > 0 Then
selection1.Item(i).Value.Text = b
End If
Next
End Sub
Private Sub CommandButton3_Click()
On Error Resume Next
Err.Clear
Set myCATIA = GetObject(, "CATIA.Application")
Dim myDocs As Documents
Set myDocs = myCATIA.Documents
Dim numofopeneddoc, n, j As Integer
numofopeneddoc = myDocs.Count
For n = 1 To numofopeneddoc
a = TextBox1.Value
b = TextBox2.Value
c = "CATDrwSearch.DrwText.TextString_CAP=*" & a & "*,all"
Set drawingDocument1 = CATIA.ActiveDocument
Set selection1 = drawingDocument1.Selection
selection1.Search c 'Starts the search
Text2 = selection1.Count
i = 1
For i = 1 To Text2
If Text2 > 0 Then
selection1.Item(i).Value.Text = b 'actual text replacement
End If
Next
Next n
End Sub
Alternatively, I also tried this method to no avail as well. Apparently, I can get around the .Selection portion enter image description here