how to make a selction inside a macro or vba?
how to make a selction inside a macro or vba?
(OP)
Hi,
The following simple code below generates a messagebox with the text of the feature that´s been (and needs to be) selected before running the macro:
But what if I would like to use the opposite order, first run the macro and then select feature and get the messagebox? How do I do that??
Sub CATMain()
Dim USel As Selection
Set USel = CATIA.ActiveDocument.Selection
If USel.Count > 0 Then
For I = 1 to USel.Count
MsgBox(USel.Item(I).Value.Name), ,"VBScript"
Next
End If
End Sub
Best regards
JO
The following simple code below generates a messagebox with the text of the feature that´s been (and needs to be) selected before running the macro:
But what if I would like to use the opposite order, first run the macro and then select feature and get the messagebox? How do I do that??
Sub CATMain()
Dim USel As Selection
Set USel = CATIA.ActiveDocument.Selection
If USel.Count > 0 Then
For I = 1 to USel.Count
MsgBox(USel.Item(I).Value.Name), ,"VBScript"
Next
End If
End Sub
Best regards
JO





RE: how to make a selction inside a macro or vba?
CODE --> CATScript
Sub CATMain() Dim USel As Selection Dim InputObject(0) Dim oStatus InputObject(0) = "AnyObject" Set USel = CATIA.ActiveDocument.Selection Msgbox "This macro will give you the name of a selected element, you have to hit ESCAPE key when you want to finish" & vbCrLf & vbCrLf & "Maximum number of loops is 100. " & "Names will depend if you select in Specification Tree or Graphic Area" For i = 1 To 100 oStatus = USel.SelectElement2(InputObject, "Select something in Specification Tree", True) If (oStatus = "Cancel") Then Exit Sub Else End If MsgBox(USel.Item(1).Value.Name), ,"VBScript" USel.Clear Next End SubRegards
Fernando
https://picasaweb.google.com/102257836106335725208
https://picasaweb.google.com/103462806772634246699...
RE: how to make a selction inside a macro or vba?
I have seen your nice work here at the forum and read many posts that you have solved and now you solved my problem! :) I´m really greatful!
Sorry for another newbe question, but if I would like to use this code inside a catvba? Is it possible? I get "compile error" on USel.SelectElement2...
Thanx once again!
best regards from jowr
RE: how to make a selction inside a macro or vba?
RE: how to make a selction inside a macro or vba?
Yes, that´s it. It says:
"Compile Error:
Function or interface marked as restricted, or the function uses an Automation typ not supported in Visual Basic."
I understand what it says but I suppose there must be a workaround for this? Or is it not possible to make a feature selection inside a userform?
RE: how to make a selction inside a macro or vba?
1. COMMENT OUT THE VARIABLE TYPE
Dim Usel 'As Selection
2. ADD A SECOND SELECTION VARIABLE THAT IS LATE BOUND and use the new variable when making the selection
Sub CATMain()
Dim USel As Selection
Dim USelLB
Dim InputObject(0)
Dim oStatus
InputObject(0) = "AnyObject"
Set USel = CATIA.ActiveDocument.Selection
Set USelLB = USel
Msgbox "This macro will give you the name of a selected element, you have to hit ESCAPE key when you want to finish" & vbCrLf & vbCrLf & "Maximum number of loops is 100. " & "Names will depend if you select in Specification Tree or Graphic Area"
For i = 1 To 100
oStatus = USelLB.SelectElement2(InputObject, "Select something in Specification Tree", True)
If (oStatus = "Cancel") Then
Exit Sub
Else
End If
MsgBox(USel.Item(1).Value.Name), ,"VBScript"
USel.Clear
Next
End Sub
RE: how to make a selction inside a macro or vba?
This totally made my day, Thank you very much!
With best regards
JOWR
RE: how to make a selction inside a macro or vba?
If I would like to populate a listbox with the selections?
wouldn´t the code below together with your code (comment out the msgboxes) do the trick?
text = USel.Item(1).Value.Name
myListBox.AddItem (text)
I tried it but it would only populate one feature at a time.
best regards
JOWR
RE: how to make a selction inside a macro or vba?
Sub CATMain()
Dim USel As Selection
Dim USelLB
Dim InputObject(0)
Dim oStatus
InputObject(0) = "AnyObject"'selection filter forces user to select specific objects, AnyObject allows selection of any object
Set USel = CATIA.ActiveDocument.Selection
Set USelLB = USel
Msgbox "This macro will give you the name of a selected element, you have to select the FINISH button on the Tools Palette when you want to finish selecting" & vbCrLf & "Names will depend if you select in Specification Tree or Graphic Area"
USel.Clear'You should clear the selection before making a selection
oStatus = USelLB.SelectElement3(InputObject, "Select objects to list names", True,CATMultiSelTriggWhenUserValidatesSelection, False)
If (oStatus = "Cancel") Then'User hit esc on keyboard
MsgBox "Macro canceled by user"
Exit Sub
Else'Loop through selected objects and list names
For i = 1 to USel.Count
sName = USel.Item(I).Value.Name
oListBox.AddItem (sName)
Next
'Insert the code you are using to display the ListBox and please share with us!
End If
USel.Clear
Next
End Sub
RE: how to make a selction inside a macro or vba?
On the other hand, is good when you are developing something, to think from the very beginning what you want to do.
Regards
Fernando
https://picasaweb.google.com/102257836106335725208
https://picasaweb.google.com/103462806772634246699...
RE: how to make a selction inside a macro or vba?
It worked perfectly!
With best regards
JOWR
Private Sub btn_start_Click()
btn_start.Enabled = False 'Disable button while running script
Dim USel 'As Selection
Dim USelLB
Dim InputObject(0)
Dim oStatus
InputObject(0) = "AnyObject"
Set USel = CATIA.ActiveDocument.Selection
If USel.Count > 0 Then 'Added to clear current selection to not be added from start
USel.Clear
End If
Set USelLB = USel
Label1.Caption = "Selection started ESC to stop." 'Added to prompt to user how to stop selection.
For i = 1 To 100
oStatus = USelLB.SelectElement2(InputObject, "Select something in Specification Tree", True)
If (oStatus = "Cancel") Then
Exit For 'Original row "Exit Sub"
End If
text = USel.Item(1).Value.Name
myListBox.AddItem (text)
USel.Clear
Next
Label1.Caption = "" 'Added to clear label
btn_start.Enabled = True 'Enabel button again
End Sub
RE: how to make a selction inside a macro or vba?
Regards
Fernando
https://picasaweb.google.com/102257836106335725208
https://picasaweb.google.com/103462806772634246699...