Vba code to list tables on drawing sheet

Vba code to list tables on drawing sheet

Gshahane01
Explorer Explorer
1,077 Views
8 Replies
Message 1 of 9

Vba code to list tables on drawing sheet

Gshahane01
Explorer
Explorer

Dear all, 

I want to list all tables standard and custom which are on my drawing sheet via vba code. Can someone help me with this. 

 

0 Likes
1,078 Views
8 Replies
Replies (8)
Message 2 of 9

grobnik
Collaborator
Collaborator

@Gshahane01 Hi, please could you explain better what do you mean with term "list" ? 

0 Likes
Message 3 of 9

Gshahane01
Explorer
Explorer

List means I want to know name of all tables on drawing and print those names. Intention is then to go in those tables and modify some values

0 Likes
Message 4 of 9

Ed__Jobe
Mentor
Mentor

The reason he asks is that AutoCAD tables don’t have names. They only have ObjectID’s. You may be looking at the content of a cell and calling it a name. You can post a dwg to help you explain. The other question I have is, do you have any programming experience?

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
Message 5 of 9

grobnik
Collaborator
Collaborator

@Gshahane01  Thank you for explanation, now the request it's more clear. I agreed with Ed @Ed__Jobe, Table object does not have name, but only Id, which as I know it's not visible during drawing editor but only if you use a development tools (this the reason because Ed asked you about your developing or programming experiences), as others objects in the drawing (with exception of blocks which has a name).

What you can do, We are suggesting you as trick, is to add a sort of "NAME" in a title row to your tables always in the same row/col of all the tables inside the drawing, search (by VBA I guess) for the text as "NAME" write before in the title row for all the tables in the drawing and read  / write the selected table's contents. 

In addition do you need to "input" somewhere the "NAME" of the table to search or the "NAME" it will be always the same ?

The second step of your question, concerning the "print", where do you want to "print" on command bar ? on message box ? export to excel the related table contents ? on a text file ?.

Hope we answer to your question.

If you can share a part of of your drawing I guess I can prepare a sample code in order to show you what we are suggesting, of course it will be an example for your better understand.

Thank you bye

0 Likes
Message 6 of 9

grobnik
Collaborator
Collaborator

@Gshahane01 here below an example

 

Sub Table_List()
Dim MyObject As AcadObject
Dim MyTable As AcadTable
Dim MyTitle As Variant

For Each MyObject In ThisDrawing.ModelSpace

    If TypeOf MyObject Is AcadTable Then
        Set MyTable = MyObject
        MyTitle = MyTable.GetCellValue(0, 0)
        Debug.Print MyTitle
    End If
Next
End Sub

 

grobnik_0-1736664738414.png

grobnik_1-1736664759886.png

As you can see we have two tables in the drawing "NAMED" in the first row "TABLE 1" and "TABLE 2".

The procedure search for all objects in the drawing and, if the object type it will be a Table, get for each table the "NAME" we put in the first row of the table.

The output in this case is the immediate window of VBA development tools usually used for debugging the code.

Once you have your "SELECTED TABLE" MyTable object you can do everything possible to do for example modify a cell content

 

Sub Table_List()
Dim MyObject As AcadObject
Dim MyTable As AcadTable
Dim MyTitle As Variant

For Each MyObject In ThisDrawing.ModelSpace

    If TypeOf MyObject Is AcadTable Then
        Set MyTable = MyObject
        MyTitle = MyTable.GetCellValue(0, 0)
         If MyTitle = "TABLE 2" Then
            Call MyTable.SetCellValue(1, 1, "YYYY")
         End If
        Debug.Print MyTitle
    End if
Next
End Sub

 

In the above case you are writing the "YYYY" value in the TABLE 2 cell at ROW 1 and COLUMN 1.

 

But again all above it will be possible with using VBA or some other development tools.

Bye

0 Likes
Message 7 of 9

Ganesh_Shahane
Observer
Observer

Dear Grobnik, Thank you for help. Actually, I am working on inventor software.  I have drawing in dwg format. I have opened it in inventor. Now there are some tables which have embedded excel spreadsheet. E.g. Refer attached screenshots. In the first screenshot you will see that I see those objects under 3rd party. In the 2nd screenshot you can see that table. When I double click on that table an excel file linked to it will open and I can modify the values in excel. After I save and close the excel spreadsheet, the values will be updated in table on drawing sheet. I want to automate this task. 

I tried above code, but it does not work. If you want, I will upload CAD file and drawing file created in inventor. Please let me know.

0 Likes
Message 8 of 9

grobnik
Collaborator
Collaborator

Hi @Ganesh_Shahane I'm not inventor expert.

I guess the code doesn't work because it's based on Autocad VBA, not for inventor, or Excel VBA.

The code above is working with Autocad drawing (see attached for your info)

in any case, you are not talking about a Table object inside the drawing, but an embedded object copied from Excel and paste into the drawing, it's not the same as I understood by your explanation.

Usually with Autocad you could have a TABLE object and a link to an excel worksheet, once you update the worksheet and in the same time your drawing is opened the data inside the table will be updated as the worksheet. As per my past experience this method it's not so quick on special way if you have a lot of data to be updated. I don't know if Inventor has the same feature. Of course you can have several link to several worksheet or part of a single worksheet (amount of cells) spreaded into several tables in the drawing.

I guess you should ask to inventor forum group I guess there can help you more.

Bye

0 Likes
Message 9 of 9

Ed__Jobe
Mentor
Mentor

When listing the tables, are you working in Inventor or AutoCAD? If using Inventor, you should be using iLogic, not VBA. Try the iLogic forum.

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