兰州人 发表于 2007-2-18 13:51:00

[原创]Autocad调用excel涉及到的版本引用问题

<strong><p>在AutoCad中要调用Excel程序,必须引用MicroSoft Excell X.0 Library</p><p>&nbsp;excel 2000&nbsp; MicroSoft Excell 9.0 Library</p><p>&nbsp;excel 2003&nbsp; MicroSoft Excell 11.0 Library</p><p>在不同机器上使用,会出现问题</p><p>解决方法,Dim xlApp As Excel.Application</p><p>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;改为:Dim xlApp As Object</p><p>&nbsp;&nbsp;&nbsp;&nbsp; 以下语句在不需引用MicroSoft Excell X.0 Library,测试通过OK<br/></p><p>Sub AcadToExcel()<br/>&nbsp;&nbsp;&nbsp; Dim iPt(0 To 2) As Double<br/>&nbsp;&nbsp;&nbsp; Dim xlApp As Excel.Application<br/>&nbsp;&nbsp;&nbsp; Dim xlSheet As Object<br/>&nbsp;&nbsp;&nbsp; <br/>&nbsp;&nbsp;&nbsp; <br/>&nbsp;&nbsp;&nbsp; ' 发生错误时跳到下一个语句继续执行<br/>&nbsp;&nbsp;&nbsp; On Error Resume Next<br/>&nbsp;&nbsp;&nbsp; ' 连接Excel应用程序<br/>&nbsp;&nbsp;&nbsp; Set xlApp = GetObject(, "Excel.Application")<br/>&nbsp;&nbsp;&nbsp; <br/>&nbsp;&nbsp;&nbsp; If Err.Number &lt;&gt; 0 Then<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Set xlApp = CreateObject("Excel.Application")<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; xlApp.Visible = True<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; xlApp.Workbooks.Add<br/>&nbsp;&nbsp;&nbsp; End If</p><p>&nbsp;&nbsp;&nbsp; ' 返回当前活动的工作表<br/>&nbsp;&nbsp;&nbsp; Set xlSheet = xlApp.ActiveSheet<br/>&nbsp;&nbsp;&nbsp; <br/>&nbsp;&nbsp;&nbsp; <br/>&nbsp;&nbsp;&nbsp; ' 释放对象<br/>&nbsp;&nbsp;&nbsp; Set xlSheet = Nothing<br/>&nbsp;&nbsp;&nbsp; Set xlApp = Nothing</p><p>End Sub<br/></p><p>引用&nbsp;excel 2000&nbsp; MicroSoft Excell X.0 Library</p><p>Dim xlApp As Excel.Application</p><p>本地窗口</p><p>表达式&nbsp;&nbsp; &nbsp;值&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 类型</p><p>xlApp&nbsp;&nbsp;&nbsp; Nothing&nbsp;&nbsp;&nbsp; <font color="#f73809">Application</font></p><p>Dim xlApp As Object</p><p>本地窗口</p><p>表达式&nbsp;&nbsp; &nbsp;值&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 类型</p><p>xlApp&nbsp;&nbsp;&nbsp; Nothing&nbsp;&nbsp;&nbsp; <font color="#ff0000">Object</font></p></strong>

兰州人 发表于 2007-2-18 15:52:00

<p>做成功能函数的方法</p><p>Function xlSheet() As Object</p><p>&nbsp;&nbsp;&nbsp; Dim xlApp As Object<br/>&nbsp;&nbsp;&nbsp; 'Dim xlsheet As Object&nbsp;&nbsp;&nbsp;&nbsp;<br/>&nbsp;&nbsp;&nbsp;&nbsp;<br/>&nbsp;&nbsp;&nbsp; ' 发生错误时跳到下一个语句继续执行<br/>&nbsp;&nbsp;&nbsp; On Error Resume Next<br/>&nbsp;&nbsp;&nbsp; ' 连接Excel应用程序<br/>&nbsp;&nbsp;&nbsp; Set xlApp = GetObject(, "Excel.Application")<br/>&nbsp;&nbsp;&nbsp; <br/>&nbsp;&nbsp;&nbsp; If Err.Number &lt;&gt; 0 Then<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Set xlApp = CreateObject("Excel.Application")<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; xlApp.Visible = True<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; xlApp.Workbooks.Add<br/>&nbsp;&nbsp;&nbsp; End If</p><p>&nbsp;&nbsp;&nbsp; ' 返回当前活动的工作表<br/>&nbsp;&nbsp;&nbsp; Set xlSheet = xlApp.ActiveSheet&nbsp;&nbsp;&nbsp; <br/>End Function<br/></p><p>调用</p><p>Sub lls()<br/>&nbsp; xlSheet.cells(1, 5).Value = "aa1"<br/>End Sub<br/></p><p>在程序中调用相当简单,只要一条指令搞定,Ok</p>

天龙八部 发表于 2007-3-2 11:11:00

不错,是CAD与EXCEL连接的一个方法
页: [1]
查看完整版本: [原创]Autocad调用excel涉及到的版本引用问题