TheSwamp
Code Red => VB(A) => Topic started by: davidtru on June 23, 2005, 10:36:06 AM
-
Hi! I've only been using VBA for a little over a week, so I'm quite new at this. I'm working on exporting a table from Excel to AutoCAD, and vice-versa. I am able to get the table from Excel to AutoCAD with cell and coordinate mapping (Cell x,x to x,y,z value) and using loops to speed up the process (there are over 1000 text strings.) I would like to be able to use this process in the reverse way in order to get the table back to Excel from AutoCAD.
What I would like to be able to do is have values for the x, y, and z coordinates set in my code, get the text string (if there is one) from those coordinates, and move the text string into a specific cell in an Excel spreadsheet.
The only thing that is giving me issues is actually getting the string from the specific AutoCAD coordinates.
I'm not having any issues opening up the Excel document, or anything along that line, I just can't figure out how to read text from coordinates.
Any suggestions on making this work are GREATLY appreciated. Thank you in advance.
-
Post what you have and we can go from there
-
Thanks for the quick reply!
This is the code I use to open up the Excel file, and the loop I use to extract the first column of useful data from Excel, and into Auto CAD.
Dim excelapp As Excel.Application
Dim wbkobj As Workbook
Dim shtobj As Worksheet
Dim strtest As String
Dim Row As Double
Dim X as Double
Dim Y as Double
Dim talgn As AcAlignment
On Error Resume Next
frmImportCableSched.Hide
Err.Clear
Set excelapp = GetObject(, "Excel.Application")
If Err <> 0 Then
Err.Clear
Set excelapp = CreateObject("Excel.Application")
If Err <> 0 Then
MsgBox "couldn't start excel", vbExclamation
End
End If
End If
excelapp.Visible = False
'Open workbook from predetermined string sPath
Set wbkobj = Workbooks.Open(sPath)
Set shtobj = excelapp.Worksheets(1)
'Import First column with data into Auto CAD
Row = 16
Do While Row < 101
strtest = shtobj.Cells(Row, 2)
X = 51.86
Y = 520.5 - ((Row - 16) * 5)
talgn = acAlignmentMiddleCenter
modDwgTools.WriteKey aDwg, strtest, talgn, X, Y
Row = Row + 1
Loop
Here is the code accessed when the modDwgTools.WriteKey is called:
Public Function WriteKey(aDwg As AcadDocument, strtest As String, algn As AcAlignment, X As Double, Y As Double, Optional dblHeight As Double) As Boolean
Dim aSpace As AcadModelSpace
Dim tBase(2) As Double
Dim tDest(2) As Double
Dim aText As AcadText
Set aSpace = aDwg.ModelSpace
If dblHeight = 0 Then
dblHeight = 2.5
End If
Set aText = aSpace.AddText(strtest, tBase, dblHeight)
aText.Layer = "TEXT"
aText.Alignment = algn
tDest(0) = X
tDest(1) = Y
tDest(2) = 0
aText.Move tBase, tDest
End Function
So, this code allows me to get all the necessary data from Excel to Auto CAD. I was just wondering how I would go about doing a reversal of this process (I have some spreadsheets I need to get into acad, and some drawings I need to get into Excel.) If possible, I would like to continue to use VBA in Auto CAD. Thanks.