×
INTELLIGENT WORK FORUMS
FOR ENGINEERING PROFESSIONALS

Are you an
Engineering professional?
Join Eng-Tips Forums!
• Talk With Other Members
• Be Notified Of Responses
• Keyword Search
Favorite Forums
• Automated Signatures
• Best Of All, It's Free!

*Eng-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

#### Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

# how to make a selction inside a macro or vba?3

## 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

### RE: how to make a selction inside a macro or vba?

Hi

#### 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 Sub 

### RE: how to make a selction inside a macro or vba?

(OP)
WOW Ferdo!

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?

Does it say "Compile error: function marked as restricted… "?

### RE: how to make a selction inside a macro or vba?

(OP)
Hi Lardman363,

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?

Yes, if you are using CATScript or catvbs, they are late bound languages...meaning the compiler determines the variable type as the code runs...not ahead of time when the variable is declared. So there are two things you can try:
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?

(OP)
GREAT lardman! It worked!

This totally made my day, Thank you very much!

With best regards
JOWR

### RE: how to make a selction inside a macro or vba?

(OP)
Oh, if i dare to ask another question...? :)

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

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?

SelectElement2 only allows you to select 1 object at a time, but you are kind of tricking yourself because the selection is made in a loop. So, you would need to either store all your data in an array or use SelectElement3, which allows you to multi-select. Note: After you multi-select, a "tools palette" will open and you have to select the green ball icon called "finish" to complete the selection. Sometimes it is quirky and you have to click it twice or just hold the click for a second to get it to work consistently.

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
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?

Or another solution could be after each selection (if you don't want to use multiple selection, maybe you want to see something in 3D or whatever) write the names in a temporary text file and open it when cancel selection.

On the other hand, is good when you are developing something, to think from the very beginning what you want to do.

### RE: how to make a selction inside a macro or vba?

(OP)
Hello Lardman and Ferdo, thank you very much for your help!
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

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?

Star for both of you, I like to see peoples collaborating to solve problems .

#### Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

#### Red Flag Submitted

Thank you for helping keep Eng-Tips Forums free from inappropriate posts.
The Eng-Tips staff will check this out and take appropriate action.

#### Resources

eBook - The Future of Product Development is Here
Looking to make the design and manufacturing of your products more agile? For engineering and manufacturing organizations, the need for digital transformation of product development processes just became more urgent than ever so we wanted to share an eBook that will help you build a practical roadmap for your journey. Download Now

Close Box

# Join Eng-Tips® Today!

Join your peers on the Internet's largest technical engineering professional community.
It's easy to join and it's free.

Here's Why Members Love Eng-Tips Forums:

• Talk To Other Members
• Notification Of Responses To Questions
• Favorite Forums One Click Access
• Keyword Search Of All Posts, And More...

Register now while it's still free!