Jump to content

VBA code to iterate thru specific block references


abraxus

Recommended Posts

i cant believe i have spent as much time already as i have trying to find a solution to such a simple problem - the code i am about to post explains itself, i hope - i am trying to iterate thru the block references collection in VBA, so that i can process a set of code (the code is irrelevant as i already know how to do that part)

 

here's the simple code i am trying to use, and it wont even start the loop so i must have something defined wrong

 

please explain what part i am not comprehending

 

(it's all modelspace, no layoutspace)

 

i hope this website emails me when people reply... i've never posted here before and i'm not sure how this works

 

Sub PW()
 Dim blkEndcap As AcadBlockReference
 
 ' iterate thru a list of blocks named "fixture_block" and also have ScaleX set to 15 and ScaleY set to 5
 For Each blkEndcap In ThisDrawing.Blocks
   If blkEndcap.Name = "fixture_block" And blkEndcap.ScaleX = 15 And blkEndcap.ScaleY = 5 Then
     debug.print "found one"
   End If
 Next
End Sub

Edited by abraxus
Link to comment
Share on other sites

It's been awhile since using VBA, but try this:

 

Dim AENT As AcadEntity
Dim blkEndcap As AcadBlockReference

       For Each AENT In ThisDrawing.ModelSpace
           If TypeOf AENT Is AcadBlockReference Then
               Set blkEndcap = AENT
               If blkEndcap.Name = "fixture_block" And And blkEndcap.ScaleX = 15.0 And blkEndcap.ScaleY = 5.0  Then
                    debug.print "found one"
               End If
           End If
       Next AENT

Link to comment
Share on other sites

Though, I should mention - if the block reference in question is a dynamic block then you will have to use

AcadBlockR​eference.E​ffectiveNa​me

Link to comment
Share on other sites

that helped... but i still cannot seem to reference the scale part - here's how i broke it down

 


Sub pw3()
Dim AENT As AcadEntity
Dim blkEndcap As AcadBlockReference

       For Each AENT In ThisDrawing.ModelSpace
           If TypeOf AENT Is AcadBlockReference Then
               Set blkEndcap = AENT
               If blkEndcap.Name = "fixture_block" Then
                 If blkEndcap.ScaleX = 15# Then
                   If blkEndcap.ScaleY = 5# Then
                    Debug.Print "found one"
                   End If
                 End If
               End If
           End If
       Next AENT
End Sub

Link to comment
Share on other sites

OMG thank you - that worked

 

the documentation is horrible when trying to google things like this

 

thank you thank you thank you thank you thank you

Link to comment
Share on other sites

You're welcome. I'm happy you are up and running.

 

AutoCAD used to come with a fairly good VBA reference. I think it was removed back when Autodesk planned on eliminating the VBA customization option. Look around on Autodesk.com, it may still be available for download.

Link to comment
Share on other sites

oh i know they removed it from the default install, because it can be abused like any other VBA code

 

which leads me to another question that you probably wont be able to answer, due to the complexity of corporate network administration, but when i write VBA code for autocad on my home computer, it works just fine, but when i try to run it at work, i get a weird error (i forget what it was, but i can write it down tomorrow and explain it if you might be able to help me with that too) - i'm guessing it's related to windows 7 and maybe some network restrictions, or registry restrictions or something... all i'm doing in the code that has the problem is trying to create VBA code that sets up some dimension variables, but it's like i dont have "permission" to change those variables with VBA code... any ideas? if not, that's cool... you already helped me get past the stupid issue i was having *puts on coder hat*

Link to comment
Share on other sites

in short, it errors out on this line of code

 

' set dimension variables

ThisDrawing.SetVariable "DIMCLRD", FDS_DIMCLRD

 

do i need some sort of admin permissions to set variables in "ThisDrawing"?

 

(it's not an error in variable declaration, because it works just fine with the same file on my home computer)

Link to comment
Share on other sites

I haven't heard anything specific regarding network restriction. That particular variable is saved in the drawing, so it shouldn't even be flagged by some corporate policy concerning Registry modification on your work computer.

 

Unfortunately, I can't look into it in any detail as I no longer have access to a computer with VBA installed.

 

If you do figure out the problem, be sure to post a follow up.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...