How to detach a excel data linked table using VBA?

How to detach a excel data linked table using VBA?

shuaib_cad
Advocate Advocate
1,480 Views
4 Replies
Message 1 of 5

How to detach a excel data linked table using VBA?

shuaib_cad
Advocate
Advocate

I want to detach an excel data linked table from the AutoCAD drawing using VBA.

Can anyone help me with the code?

 

 

Regards,

Mohammed Shuaib

0 Likes
Accepted solutions (1)
1,481 Views
4 Replies
Replies (4)
Message 2 of 5

norman.yuan
Mentor
Mentor

DataLink and Table in AutoCAD are 2 things that work together to fiil data in table. So, your question is a bit vague: do you want to remove the data link from a table (i.e. the table remains, while the data filled from the data link would be gone), or you want to remove the table itself, which has data from data link? And by either way, do you want to keep the data link in the drawing at the end, even no table uses the data link?

 

It is unfortunate that AutoCAD COM API/VBA does not support DataLink handling directly (creating/updating/removing a data link, which is saved in drawing's NamedDictionary "ACAD_DATALINK"), but you can somehow manipulate via AcadDictionary object in VBA in some degree (such as check if particular data link exists by name, or delete one by name). Also, there is no COM API to add/attach and remove/detach data link to AcadTable. So, I guess, if you want to remove Data link from a table (but keep the table), you may have to save the table information (row/column/style/data),  delete the table and then recreate the table with the saved row/column/style/data.

 

On the other hand, if you can do AutoCAD .NET API, things would be a lot easier, because .NET API provides directway to handle DataLink and datalink in table.

 

Norman Yuan

Drive CAD With Code

EESignature

0 Likes
Message 3 of 5

shuaib_cad
Advocate
Advocate

 Hi Norman!

 

 

 

0 Likes
Message 4 of 5

norman.yuan
Mentor
Mentor
Accepted solution

OK, as I said in previous reply, there is no direct way to detach DataLink from a Table with COM API/VBA. The alternative is to directly delete the DataLink from drawing's NamedDictionary, if you do know the DataLink name and are sure the DataLink is only used by the table in interest.

 

Following code deletes a DataLink names as "TestTableData" from a drawing:

 

Public Sub Test()
    Dim dic As AcadDictionary
    
    On Error Resume Next
    Set dic = ThisDrawing.Dictionaries("ACAD_DATALINK")
    If Err.Number <> 0 Then Exit Sub
    
    dic.Remove "TestTableData"
    If Err.Number <> 0 Then
        MsgBox "Cannot find item ""TestTableData"" in DataLink dictionary!"
    Else
        MsgBox "Datalink ""TestTableData"" has been removed successfully"
    End If
    
End Sub

 

Hwever, after the DataLink is deleted from drawing's NamedDictionary, the table that uses this DataLink would still be data linked UNTIL you save the drawing, close it from AutoCAD. Once you reopen it in AutoCAD, you would see the table is no longer data linked, but the data from the previous DataLink remains.

 

Again, with AutoCAD .NET API, you can easily detach the datalink from table without having to deleting the DataLink itself, and you do not need to close the drawing for the detaching to take effect.

 

HTH

 

Norman Yuan

Drive CAD With Code

EESignature

0 Likes
Message 5 of 5

robertodethVV26R
Observer
Observer
Thanks norman.yuan for your help.
For delete every link use this code:
Public Sub DeleteallLinks()
Dim dic As AcadDictionary

On Error Resume Next
Set dic = ThisDrawing.Dictionaries("ACAD_DATALINK")
If Err.Number <> 0 Then Exit Sub

LINKS = ThisDrawing.Dictionaries("ACAD_DATALINK").Count
For i = LINKS - 1 To 0 Step -1
dic.Item(i).Delete
Next

If Err.Number <> 0 Then
MsgBox "Cannot find item ""TestTableData"" in DataLink dictionary!"
Else
MsgBox "Datalink ""TestTableData"" has been removed successfully"
End If

End Sub
0 Likes