thank you for the files you posted
as for the dwg one I need an Autocad 2010 version to open it.
but reviewing the xlsm one I can go on trying and help you out
and to this end I think reviewing those 5 points out of post#2 (and adding some new ones...) could be a good leading way for me
1) Autocad application and active document handling
you added
Code:
Public acad As AcadApplication
at the very top of your module before any subs beginning
but then in your ExtraireAttributs() sub you also
- declare and use "AcadApp" throughout the entire sub with the same purpose
- use "acad" in only; so it's useless
just choose what variable you use and get rid of the other
As a personal choice of mine I try not using "Public" variables but module scope ones only and pass them to subs and functions as parameters
in
Code:
On Error Resume Next
Set AcadApp = GetObject(, "AutoCAD.Application")
If AcadApp Is Nothing Then
Set AcadDoc = AcadApp.ActiveDocument
AcadApp.Visible = True
End If
there's a logic flaw, since if "AcadApp" has not been set (as an application object), then any attempt to use it (like
Code:
Set AcadDoc = AcadApp.ActiveDocument
) would lead to an error
most probably you're not experiencing any error yet since you always run your sub with AutoCAD running already. and besides you never use "AcadDoc" variable, also (you use "AcadApp.ActiveDocument" instead, to get the active document)
but this way you're not handling a quite probable condition of the user starting your macro without any Autocad session running.
the simplest (for you) handling of any Autocad session running could be just throwing a message to inform the user that he has to start an Autocad session with a valid drawing open before runnong this macro, and then exit the macro
4) selection set handling
adding before exiting your sub prevents you from improperly adding elements to your selectionset on each run of your macro
next, you should get this issue some steps further
with
Code:
On Error Resume Next
Set SelSet = AcadApp.ActiveDocument.SelectionSets.Add("SELSET")
you're first asking VBA interpreter to ignore errors and then trying to set a new selection set name "SELSET".
so you're not handling the case of a selectionset named "SELSET" being in your drawing already. And this could very well happen (should you ever exit form your previous run before reaching the "SelSet.Delete" statement (due to any code error before it, or the user stopping the macro, or a power failure, and so on...) resulting in no "SelSet" object set and thus making any subsequent statement using it (like
Code:
SelSet.Select acSelectionSetAll, , , FiltersType, FiltersData"
) fail.
to handle this condition you could act in a pretty similar way you do with AcadApp variable: after
Code:
Set SelSet = AcadApp.ActiveDocument.SelectionSets.Add("SELSET")
check if "SelSet" is nothing. if it is, the a selection set named "SELSET" is already there so you just set your "SelSet" object to it (use selectionset object's "Item" property instead of its "Add" method).
5) you replaced any previous with this way you're checking if the running attribute name (namely, its TagString property) equals the string in your activesheet's cell placed in the current column (which seems correct) and in the current row (which however gets updated to the next down after each "SelSet" entity iteration). just check and setthe proper row reference to where you actually must find the attribute name in "Attributes" sheet
6) still open issue
furthermore, I think it's time to add the following issues
8 ) cells references
after properly using statements like
Code:
excelSheet.Range(Cells(1, 1), Cells(40, 11)).Clear
and the likes, you also use statements like
Code:
Cells(1, 1).Value = "BLOCCO"
but be aware that omitting "excelSheet." reference leads to referring to the active excel sheet. which could not be the one you'd want to use!
this could happen if you'd place a button to run the macro in a sheet different than "Attributes" (or in a different workbook even!) so that all those "Cells" references would point to the button sheet
it's much safer alway using a complete reference like "mySheet.Cells(" to be sure you're always pointing to cells in the proper sheet (and Woorkbook too, if you take care of it when setting your worksheet object).
in such cases, you may also avoid the boring recurring of a most probably long string by means of the "with - end with" block statement like follows:
Code:
With excelSheet
.Cells(1, 1).Value = "BLOCCO"
.Cells(1, 2).Value = "Handle"
.Cells(1, 3).Value = "TAG1"
.Cells(1, 4).Value = "TERM01"
.Cells(1, 5).Value = "TERM02"
.Cells(1, 6).Value = "TERM03"
.Cells(1, 7).Value = "POS1"
.Cells(1, 8).Value = "POS2"
.Cells(1, 9).Value = "POS3"
.Cells(1, 10).Value = "COLOR"
.Cells(1, 11).Value = "XREF"
End With
it's a handy feature but you mast also carefully always check which "with-end with" block you're in, to avoid misleading references
9 ) code polishing
9.1) "Dim" statements - 1
check all you "Dim" statements to find those referring to variables you don't use (like "mspace", and some others) or with double declaration (like "Excel")
you can simply place the cursor inside a variable name and use the "find" button (that one with a binoculars icon) or pressing Crtl+F to find all its occurrences.
9.2) "Dim" statements -2
every variable mus be followed by "as" keyword and the proper type variable name
otherwise a "Variant" type would be assumed as default
this means that a statement like
Code:
Dim i, Row, j, Column As Integer
would result in "i", "Row" and "j" variables declared as of variant type and "Column" as of integer type.
this should not affect your macro as it is, but could lead to some error should you ever use some methods that require specific variable types.
So it's good programming practice to have total control over variable declaration
last but not least, I wonder whether you're already using the debugging tools the VBA IDE offers. They're really helpful, and among all others I think you should try using "break points" for stepping through your code and switching between VBA IDE, Excel and Autocad windows to see what's going on after each step of your macro. you can also check variables current values through "Local variable window" and the "Immediate Window" (you can activate both from the "View" menu). I've always found this as the most proficient learning tool.