Can anybody tell me if it's possible to link visual basic from excel, to AutoCAD? How can I do that?
thanks
Bart
|
Can anybody tell me if it's possible to link visual basic from excel, to AutoCAD? How can I do that?
thanks
Bart
Hi
For starters take a look at the following threads and all! the links found within...
dbconnect
Using VBA to pull values from Excel and into attributes
Automatic BOM Generation
DATABASE
Have a good one, Mike
Last edited by Mike.Perry; 2006-04-01 at 01:11 AM. Reason: Correct formatting
Yes, yes you can. Which way to you want to communicate, AutoCAD controls Excel or Excel controls AutoCAD? And do you want data to come from an existing file or create a new one?
Here's a quicky example of creating a new instance of Excel from AutoCAD and then closing it down.
Sub ABC()
Dim oExcel As Object
Set oExcel = CreateObject("Excel.Application")
' Insert desired code here.
oExcel.Quit
End Sub
While it'll work, I wouldn't do it that way. That's the first mistake people make is referencing and using Excel itself. Talk about resource and time consuming! Also, what if the user doesn't have Excel? That sounds odd in today's work place but I run across it often enough.
The better approach is to use ADO. Most people don't realize that you can use ADO to talk to anything from Access to a simple ASCII text file without having the authoring program installed. Here's a quickie function for accomplishing it this way. Its simple, straight forward, and FAST.
There are other options for pulling the data besides the brute force grab all approach - just look up in a JET help file. Also, with this approach, you need to append any cell you read with a blanket & "" just in case there is no value in the cell. Otherwise, it'll pop a null error message on you. Or, you can move the recordset to either a collection or a dictionary and they will handle the null value for you.Private Function Read_Excel(ByVal sFile As String) As Recordset
'+--Read Excel File Using ADO
Dim oConn As ADODB.Connection
Set oConn = New ADODB.Connection
oConn = "DRIVER=Microsoft Excel Driver (*.xls);" & "DBQ="
Dim oRS As Recordset
Set oRS = New Recordset
On Error GoTo Exit_Here
With oRS
.CursorLocation = adUseClient
.CursorType = adOpenKeyset
.LockType = adLockBatchOptimistic
.Open "SELECT * FROM [sheet1$]", oConn & sFile
End With
'set return value
Set Read_Excel = oRS
Set oRS = Nothing
Exit_Here:
End Function
We want AutoCAD to control Excel. What we mean is: if we draw a rectangle in AutoCAD and we give it dimensions for example length 100, width 50. With a simple action we want those 2 dimensions to appear in excel.Originally Posted by bweir
Thanks Bart