How to connect AutoCAD (user specified version) using VB.Net via COM Library?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Dear Team,
In my Vb.Net Project, connecting AutoCAD 2020 using the COM type Library by selecting the Reference (AutoCAD Version 2020) manually & Internally code modified the version 2020. Its working fine.
(Cannot change to Net Library, built lot of Standard functions based on this COM)
Suppose if user want to connect AutoCAD 2024, once again i have to modify the COM Reference (AutoCAD Version - 2024) then will modify the connection portion inside the code based on version.
For Each user we can't built Project, not good Automation.
Query : Based on User Input (Version2024), connect AutoCAD 2024 via COM Library, along with reference base version 2020? (Is it possible Deselect 2020 reference and Select 2024 Reference inside code?)
I tried this code, not working - results in Error.
Please give Suggestions to achieve this, Thanks by advance.
Code Snippet:
Need to open AutoCAD 2024:
' -Manually added AutoCAD 2020 reference (below picture) - for built up the project
Public Shared Sub connect_cad()
'CAD_Version -- user input
'Dim CAD_Version As String = "AutoCAD.Application.23.1" 'Autocad 2020 user input (working fine)
Dim CAD_version As String = "AutoCAD.Application.24.3" ' AutoCAD 2024 user input (getting error)
Try
Cadapp = CType(System.Runtime.InteropServices.Marshal.GetActiveObject(CAD_version), AcadApplication) '' GetObject(, "AutoCAD.Application")
If Cadapp.Documents.Count = 0 Then Cadapp.Documents.Add()
Caddoc = Cadapp.ActiveDocument
Cadapp.WindowState = AcWindowState.acMax
Cadapp.Visible = True
Catch Ex As Exception
If Err.Number <> 0 Then
CADOPEN = False
Err.Clear()
Cadapp = CType(CreateObject(CAD_version), AcadApplication) 'Error coming here
If Err.Number <> 0 Then
CADOPEN = False
MsgBox(Err.Description)
Exit Sub
End If
Cadapp.WindowState = AcWindowState.acMax
Cadapp.Visible = True
Caddoc = Cadapp.ActiveDocument
Else
If Cadapp.Documents.Count = 0 Then Cadapp.Documents.Add()
Caddoc = Cadapp.ActiveDocument
conect_cad_option = True
Cadapp.WindowState = AcWindowState.acMax
Cadapp.Visible = True
End If
Finally
End Try
End Sub
Error Msg:
System.InvalidCastException: 'Unable to cast COM object of type 'System.__ComObject' to interface type 'AutoCAD.AcadApplication'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{6323190E-C6ED-434C-A368-E9314A8D7597}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).'
This Picture - Manually added Reference for AutoCAD Version 2020