How do I select the current object in VBA code, in Autocad?

How do I select the current object in VBA code, in Autocad?

Markintx
Enthusiast Enthusiast
10,483 Views
11 Replies
Message 1 of 12

How do I select the current object in VBA code, in Autocad?

Markintx
Enthusiast
Enthusiast

I am trying to cycle thru and select each item and then perform actions on them.

What I have right now is..

 

  For Each ObjEnt In ThisDrawing.ModelSpace
 
   If TypeOf ObjEnt Is AcadMText Then

 

*Here I need to select the current object in Autocad*

 

 

Any help would be greatly appreciated.

0 Likes
Accepted solutions (1)
10,484 Views
11 Replies
Replies (11)
Message 2 of 12

Alfred.NESWADBA
Consultant
Consultant

Hi,

 

I guess the question is not clear and that might be the reason for no response.

You ask for:

>> I need to select the current object

And now what is unclear (at least for me):

You have a current object, so what is to select? You already have it, otherwise it would not be currently selected.

 

Describe more from what you try to accomplish, that will make it more clear for us and so the answers will arrive (hopefully).

 

- alfred -

------------------------------------------------------------------------------------
Alfred NESWADBA
ISH-Solutions GmbH / Ingenieur Studio HOLLAUS
www.ish-solutions.at ... blog.ish-solutions.at ... LinkedIn ... CDay 2026
------------------------------------------------------------------------------------

(not an Autodesk consultant)
0 Likes
Message 3 of 12

Markintx
Enthusiast
Enthusiast

I want to select the current object in the onscreen in autocad,or maybe I want to make a selection set out of the current object would be a better way to put it.

 

My goal is to take each of those objects and use SendCommand to move or modify each one.

 

I hope this clarifies my question more.

 

Thanks again.

0 Likes
Message 4 of 12

Alfred.NESWADBA
Consultant
Consultant

Hi,

 

with VBA and your wish to run that with SendCommand (which I would try to avoid as often as possible) you can use the handle and LISP to make an object selected.

The LISP statement therefor is:

 

      (sssetfirst (setq SELSET (ssadd (handent "287"))) SELSET)

 

That makes the object with handle &h287 selected/gripped.

(whereas "287" is a handle (hex value of a handle), you should take your handle from your routine.)

 

If you want to use move with that single object you can start

     _MOVE

     (handent "287")

 

     0,0,0,

     1,1,1

 

...also with SendCommand.

 

But take care, SendCommand is easy to use in some cases, but generates a lot of conflicts as you don't get feedback (e.g if the user cancels within the command).

 

Good luck, - alfred -

------------------------------------------------------------------------------------
Alfred NESWADBA
ISH-Solutions GmbH / Ingenieur Studio HOLLAUS
www.ish-solutions.at ... blog.ish-solutions.at ... LinkedIn ... CDay 2026
------------------------------------------------------------------------------------

(not an Autodesk consultant)
Message 5 of 12

Markintx
Enthusiast
Enthusiast

Do you know how to select the object in my example from VBA (I dont use LISP)

Answering that would be a great help.

0 Likes
Message 6 of 12

Alfred.NESWADBA
Consultant
Consultant

Hi,

 

with VBA I don't know a way to get a object selected/gripped.

But you can use SendCommand to send the LISP statement 😉

 

- alfred -

------------------------------------------------------------------------------------
Alfred NESWADBA
ISH-Solutions GmbH / Ingenieur Studio HOLLAUS
www.ish-solutions.at ... blog.ish-solutions.at ... LinkedIn ... CDay 2026
------------------------------------------------------------------------------------

(not an Autodesk consultant)
0 Likes
Message 7 of 12

Markintx
Enthusiast
Enthusiast
Accepted solution

I was able to achieve the desired results using the handet command you suggested

Here is what is I was able to get to work.

 

For Each ObjEnt In ThisDrawing.ModelSpace
 
   If TypeOf ObjEnt Is AcadMText Then
   ObHandle = ObjEnt.Handle
   lisp = "(handent " & Chr(34) & ObHandle & Chr(34) & ")  "
   ThisDrawing.SendCommand ("select ")
   ThisDrawing.SendCommand (lisp)

 

 

Thanks again for the help.

0 Likes
Message 8 of 12

Markintx
Enthusiast
Enthusiast

I selected my repost as the answer to make it easier for somone to find exactly what worked.

Thanks to Alfred.NESWADBA for pushing me in the right direction.

0 Likes
Message 9 of 12

Anonymous
Not applicable

I also want to do the same action. I tried to make a code based on your solution but failed.

 can you please post your whole code.

thanx

 

0 Likes
Message 10 of 12

Markintx
Enthusiast
Enthusiast

Here is a simplified version that works.

Open a blank drawing and create 3 or 4 pieces of Mtext

then Step through this code with F8 instead of just running the code and you can watch each step.

It will select each object and then erase it, then select the next one, and erase it.

 

Sub SelectVBA()
Dim ObjEnt As AcadEntity
Dim ObHandle As Variant
Dim lisp As String

For Each ObjEnt In ThisDrawing.ModelSpace
 
   If TypeOf ObjEnt Is AcadMText Then
   ObHandle = ObjEnt.Handle
   lisp = "(handent " & Chr(34) & ObHandle & Chr(34) & ")  "
   ThisDrawing.SendCommand ("select ")
   ThisDrawing.SendCommand (lisp)
   ThisDrawing.SendCommand ("erase ")
End If

Next


End Sub

Message 11 of 12

Anonymous
Not applicable

EXcuse me, but after  ThisDrawing.SendCommand ("select ") AutoCAD waits for selecting an object by the user instead of selecting all MText automatically. What is the cause of that? 

0 Likes
Message 12 of 12

Ed__Jobe
Mentor
Mentor

Because that's the way the SELECT command works. When you use SendCommand, you can't change the way the command works. You have to use SelectionSet filtering. Here is a post that has some code for filtering for mtext. You can search the forum for other samples.

Ed


Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
How to post your code.

EESignature

0 Likes