I have got a little problem with connecting autocad to MS excel. I try to
do this with VBA in excel.
As a civil-engineer i often work with surveyors who give us lists with
coordinates. Normally i would mark these coordinates in autocad using a
block called á-coord ´. Now i have seen the the possibilities of VBA, i am
trying to make excel mark the coordinates for me.
I took the following macro and tried if excel could insert the block called
´a-coord´ with the attribute ´pointnumber´(puntnummer).
Function lijn(ByRef RegelNr As Integer)
rij = Str(RegelNr)
rij = Right(rij, Len(rij) - 1)
With Worksheets("blad1")
lijn = "[_insert " & _
.Range("a" & rij).Cells.Value & " " & _
.Range("b" & rij).Cells.Value & "," & _
.Range("c" & rij).Cells.Value & " " & _
.Range("d" & rij).Cells.Value & " " & _
.Range("e" & rij).Cells.Value & " " & _
.Range("f" & rij).Cells.Value & " " & _
.Range("g" & rij).Cells.Value & " ]" _
End With
End Function
Sub Macro1()
I=1
kanaalnr = Application.DDEInitiate("autoCAD.r14.DDE", "system")
Do
Application.DDEExecute kanaalnr, lijn((i))
i = i + 1
Loop Until lijn((i)) = "[_insert , ]"
Application.DDETerminate kanaalnr
End Sub
I have put the following in excel:
CA name of the block
CB x-coordinate
CC y-coordinate
CD x-scale
CE y-scale 0=default
CF rotation angle 0=default
CG pointnumber
ca cb cc cd ce cf cg
a-coord 0 0 1 0 0 1
a-coord 10 10 1 0 0 2
a-coord 0 0 1 0 0 3
a-coord 50 50 1 0 0 4
Etc.
Autocads output is as follows:
Command: _insert Block name (or ?): a-coord Insertion point: 0,0 X scale
factor <1> / Corner / XYZ: 1 Y scale factor (default=X): Rotation angle
<0>:
Enter attribute values
puntnummer <puntnummer>: 1 _insert a-coord 10,10 1 2 _insert a-coord 0,0 1
3 _insert a-coord 50,50 1 4 _insert a-coord 100,50 1 5 _insert a-coord
150,50 1 6 _insert a-coord 200,50 1 7 _insert a-coord 250,50 1 8
_insert a-coord 300,50 1 9 _insert a-coord 35*Cancel*
Unterminated Complex Entity
The first actions of the program are as expected, but then the problems
begin:
I think that the spaces in the macro don´t act as enters, but as normal
spaces. I think i need a forced enter in my macro, but that is just a
guess.
Who can help me!!!!!!!!!!
' Declare necessary API routines:
Declare Function FindWindow Lib "user32" Alias _
"FindWindowA" (ByVal lpClassName As String, _
ByVal lpWindowName As Long) As Long
Declare Function SendMessage Lib "user32" Alias _
"SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, _
ByVal wParam As Long, _
ByVal lParam As Long) As Long
Public Fileid As String
Sub DetectExcel()
' Procedure dectects a running Excel and registers it.
Const WM_USER = 1024
Dim hwnd As Long
' If Excel is running this API call returns its handle.
hwnd = FindWindow("XLMAIN", 0)
If hwnd = 0 Then ' 0 means Excel not running.
Exit Sub
Else
' Excel is running so use the SendMessage API
' function to enter it in the Running Object Table.
SendMessage hwnd, WM_USER + 18, 0, 0
End If
End Sub
Public Sub GetApp()
Dim FileName As String
Dim Exla As Object
Dim ExcelWasNotRunning As Boolean ' Flag for final release.
On Error Resume Next ' Defer error trapping.
Set Exla = CreateObject("excel.application") ' You may have to set Visible
property to True If Err.Number <> 0 Then ExcelWasNotRunning = True
Err.Clear ' Clear Err object in case error occurred.
FileName = OriginalFile
DetectExcel
Set Exla = GetObject(FileName) '
Exla.Application.Visible = True
Exla.Parent.Windows(1).Visible = True
'===================================================================================================================
'===================================================================================================================
'Previous code works to open Excel and the file specifide add any other code
after this