If you're still using COM, so try this code,
not tested seriously, just the same logic
as I'm posted before,
change file name you'll want to open
Public Shared Sub ReadTableFromDwg()
Dim acadver As String = "18" '//AutoCAD Version
' set your file name to read data here
Dim fname As String = "C:\Test\mtext.dwg"
' define csv file name variable
Dim csvname As String = String.Empty
Dim listData As New List(Of Object)
' get current culture
Dim oldCult As System.Globalization.CultureInfo = System.Threading.Thread.CurrentThread.CurrentCulture
' create new culture
Dim thisCult As System.Globalization.CultureInfo = New System.Globalization.CultureInfo("en-US")
' set current culture to newly created
System.Threading.Thread.CurrentThread.CurrentCulture = thisCult
MsgBox("Wait...")
Dim appProgID As String = "Autocad.Application" + "." + acadver
' get reference on interface IDispatch
Dim AcadType As Type = Type.GetTypeFromProgID(appProgID)
' launch AutoCAD
Dim AcadApp As Object = Activator.CreateInstance(AcadType)
Dim visargs() As Object = New Object(0) {}
' set visibility mode to false
visargs(0) = True
' Make application visible
AcadApp.GetType().InvokeMember("Visible", BindingFlags.SetProperty, Nothing, AcadApp, visargs, Nothing)
' Maximize window
''AcadApp.GetType().InvokeMember("WindowState", BindingFlags.SetProperty, Nothing, AcadApp, New Object() {1}, Nothing)
Dim AcadDocs As Object = AcadApp.GetType().InvokeMember("Documents", BindingFlags.GetProperty, Nothing, AcadApp, Nothing)
' define arguments to open file
Dim args() As Object = New Object(1) {}
args(0) = fname
' set read only mode to false
args(1) = False
' try open document
Dim AcDoc As Object = AcadDocs.GetType().InvokeMember("Open", BindingFlags.InvokeMethod, Nothing, AcadDocs, args, Nothing)
Dim AcUtil As Object = New Object
Try
' get reference on active document
AcDoc = AcadApp.GetType().InvokeMember("ActiveDocument", BindingFlags.GetProperty, Nothing, AcadApp, Nothing, Nothing)
' get reference on ModelSpace
Dim AcSpace As Object = AcDoc.GetType().InvokeMember("ModelSpace", BindingFlags.GetProperty, Nothing, AcDoc, Nothing)
' get reference of AcadUtility
AcUtil = AcDoc.GetType().InvokeMember("Utility", BindingFlags.GetProperty Or BindingFlags.Public, Nothing, AcDoc, Nothing)
Dim acTable As New Object
' '_____________________________________________
' ' based on example written by Tony Tanzillo
' '_____________________________________________
' The array of arguments that will be passed
' to the AcadUtil.GetEntity() method:
Dim entargs As Object() = New Object(2) {}
' In order to get VT_BYREF arguments to be
' filled in by the callee, we must use ParameterModifier
' to retrieve the arguments:
' The callee will replace the ParameterModifier
' in the args[] argument array with the results.
entargs(0) = New Object
entargs(1) = New Object
entargs(2) = vbLf + "! Select Table !"
' We also need to tell the marshaler that
' both parameters in the COM method call are
' 'out' or 'byref' parameters:
' 2 = the total number of arguments passed:
Dim pm As New ParameterModifier(3)
pm(0) = True
' first argument is ByRef
pm(1) = True
' second argument is ByRef
pm(1) = False
' third argument is ByVal
Dim modifiers As ParameterModifier() = New ParameterModifier() {pm}
' Invoke the method, passing the arguments, and
' the parameter modifers
AcUtil.GetType().InvokeMember("GetEntity", BindingFlags.InvokeMethod, Nothing, AcUtil, entargs, modifiers, Nothing, Nothing)
' The results are now in the entargs[] array:
Dim entity As Object = DirectCast(entargs(0), Object)
Dim pickpt As Object = DirectCast(entargs(1), Object)
Dim objName As String = entity.GetType.InvokeMember("ObjectName", BindingFlags.GetProperty Or BindingFlags.IgnoreCase, Nothing, entity, Nothing).ToString()
' make sure the table object selected
If objName <> "AcDbTable" Then
MessageBox.Show("Selectet is not type of table, exit program ...")
Return
End If
acTable = entity
Dim tableRows As Object = acTable.GetType().InvokeMember("Rows", BindingFlags.GetProperty Or BindingFlags.Public, Nothing, acTable, Nothing)
Dim tableColumns As Object = acTable.GetType().InvokeMember("Columns", BindingFlags.GetProperty Or BindingFlags.Public, Nothing, acTable, Nothing)
Dim tableContent As New List(Of List(Of String))
For i As Integer = 0 To tableRows - 1
Dim tableLine As New List(Of String)
For j As Integer = 0 To tableColumns - 1
Dim cellText As Object = acTable.GetType().InvokeMember("GetText", BindingFlags.InvokeMethod Or BindingFlags.Public, Nothing, acTable, New Object() {i, j})
tableLine.Add(cellText.ToString())
Next
tableContent.Add(tableLine)
Next
''-------------------------------------------------''
'' for debug only:
'Dim sb As New StringBuilder
'For Each line As List(Of String) In tableContent
' Dim textline As String = ""
' For Each st As String In line
' textline = textline + st + vbTab
' Next
' sb.AppendLine(textline.TrimEnd(vbTab))
'Next
'MessageBox.Show(sb.ToString())
''-------------------------------------------------''
Dim initDir As String = AcDoc.GetType.InvokeMember("Path", BindingFlags.GetProperty, Nothing, AcDoc, Nothing, Nothing).ToString()
Dim saveFileDialog As New System.Windows.Forms.SaveFileDialog
saveFileDialog.Title = "Enter a file name to write data: "
saveFileDialog.InitialDirectory = initDir
saveFileDialog.RestoreDirectory = True
saveFileDialog.Filter = "CSV files | *.csv"
saveFileDialog.FileName = "File name without extension"
Dim result As System.Windows.Forms.DialogResult = saveFileDialog.ShowDialog()
If result <> DialogResult.OK Then
csvname = "Not saved"
Return
End If
csvname = saveFileDialog.FileName
' Write data to CSV
Using sw As New StreamWriter(csvname, True, Encoding.ASCII)
For Each line As List(Of String) In tableContent
Dim textline As String = ""
For Each st As String In line
textline = textline + st + vbTab
Next
sw.WriteLine(textline.TrimEnd(vbTab))
Next
End Using
''----------------------------------------------------------------------------------------------------''
'save document
Dim closeargs() As Object = New Object(1) {}
closeargs(0) = True
' with the same name
closeargs(1) = fname
' Try close document
' simplified syntax
AcDoc.GetType().InvokeMember("Close", BindingFlags.InvokeMethod, Nothing, AcDoc, closeargs)
Catch ex As System.Exception
MessageBox.Show("Error: " & ex.Message & vbLf & "Trace: " & ex.StackTrace)
Finally
' Try quit application
AcadApp.GetType().InvokeMember("Quit", BindingFlags.InvokeMethod, Nothing, AcadApp, Nothing)
' clean up the memory
'--------------------'
' release Document.
releaseObject(AcDoc)
' release Documents.
releaseObject(AcadDocs)
' release Application.
releaseObject(AcadApp)
' call garbage cleaner immediatelly
GC.WaitForPendingFinalizers()
GC.GetTotalMemory(True)
GC.WaitForPendingFinalizers()
GC.GetTotalMemory(True)
' restore current culture
System.Threading.Thread.CurrentThread.CurrentUICulture = oldCult
'Display result
MessageBox.Show("Csv file saved as:" + vbLf + csvname)
End Try
End Sub
~'J'~
_____________________________________
C6309D9E0751D165D0934D0621DFF27919