从AutoCAD调用excel的功能函数 Function xlSheet() As Object Dim xlApp As Object ' This Line ,Not set Excel , run Excel 'Dim xlsheet As Object ' 发生错误时跳到下一个语句继续执行 On Error Resume Next ' 连接Excel应用程序 Set xlApp = GetObject(, "Excel.Application") If Err.Number <> 0 Then Set xlApp = CreateObject("Excel.Application") xlApp.Visible = True xlApp.Workbooks.Add End If ' 返回当前活动的工作表 Set xlSheet = xlApp.ActiveSheet End Function 主程序, 获取Entity的ObjectID,传输到Excel Sub lls() ' Begin the selection Dim returnObj As AcadObject Dim basePnt As Variant On Error Resume Next ii = 1 ' The following example waits for a selection from the user RETRY: ThisDrawing.Utility.GetEntity returnObj, basePnt, "Select an object" If Err <> 0 Then Err.Clear MsgBox "Program ended.", , "GetEntity Example" Exit Sub Else returnObj.Update MsgBox "The object type is: " & returnObj.EntityName, , "GetEntity Example" returnObj.Update End If xlSheet.cells(ii, 5).Value = returnObj.ObjectID ii = ii + 1 GoTo RETRY End Sub 以下是一个辅助程序 Sub Example_GetEntity() ' This example creates several objects in model space. It then ' prompts the user to select an object. The example continues to ' have the user select objects until the user selects in empty space. ' Create a Ray object in model space Dim rayObj As AcadRay Dim basePoint(0 To 2) As Double Dim SecondPoint(0 To 2) As Double basePoint(0) = 3#: basePoint(1) = 3#: basePoint(2) = 0# SecondPoint(0) = 1#: SecondPoint(1) = 3#: SecondPoint(2) = 0# Set rayObj = ThisDrawing.ModelSpace.AddRay(basePoint, SecondPoint) ' Create a polyline object in model space Dim plineObj As AcadLWPolyline Dim points(0 To 5) As Double points(0) = 3: points(1) = 7 points(2) = 9: points(3) = 2 points(4) = 3: points(5) = 5 Set plineObj = ThisDrawing.ModelSpace.AddLightWeightPolyline(points) plineObj.Closed = True ' Create a line object in model space Dim lineObj As AcadLine Dim startPoint(0 To 2) As Double Dim endPoint(0 To 2) As Double startPoint(0) = 0: startPoint(1) = 0: startPoint(2) = 0 endPoint(0) = 2: endPoint(1) = 2: endPoint(2) = 0 Set lineObj = ThisDrawing.ModelSpace.AddLine(startPoint, endPoint) ' Create a circle object in model space Dim circObj As AcadCircle Dim centerPt(0 To 2) As Double Dim radius As Double centerPt(0) = 20: centerPt(1) = 30: centerPt(2) = 0 radius = 3 Set circObj = ThisDrawing.ModelSpace.AddCircle(centerPt, radius) ' Create an ellipse object in model space Dim ellObj As AcadEllipse Dim majAxis(0 To 2) As Double Dim center(0 To 2) As Double Dim radRatio As Double center(0) = 5#: center(1) = 5#: center(2) = 0# majAxis(0) = 10: majAxis(1) = 20#: majAxis(2) = 0# radRatio = 0.3 Set ellObj = ThisDrawing.ModelSpace.AddEllipse(center, majAxis, radRatio) ZoomExtents End Sub
|