[原创]Autocad调用excel涉及到的版本引用问题
<strong><p>在AutoCad中要调用Excel程序,必须引用MicroSoft Excell X.0 Library</p><p> excel 2000 MicroSoft Excell 9.0 Library</p><p> excel 2003 MicroSoft Excell 11.0 Library</p><p>在不同机器上使用,会出现问题</p><p>解决方法,Dim xlApp As Excel.Application</p><p> 改为:Dim xlApp As Object</p><p> 以下语句在不需引用MicroSoft Excell X.0 Library,测试通过OK<br/></p><p>Sub AcadToExcel()<br/> Dim iPt(0 To 2) As Double<br/> Dim xlApp As Excel.Application<br/> Dim xlSheet As Object<br/> <br/> <br/> ' 发生错误时跳到下一个语句继续执行<br/> On Error Resume Next<br/> ' 连接Excel应用程序<br/> Set xlApp = GetObject(, "Excel.Application")<br/> <br/> If Err.Number <> 0 Then<br/> Set xlApp = CreateObject("Excel.Application")<br/> xlApp.Visible = True<br/> xlApp.Workbooks.Add<br/> End If</p><p> ' 返回当前活动的工作表<br/> Set xlSheet = xlApp.ActiveSheet<br/> <br/> <br/> ' 释放对象<br/> Set xlSheet = Nothing<br/> Set xlApp = Nothing</p><p>End Sub<br/></p><p>引用 excel 2000 MicroSoft Excell X.0 Library</p><p>Dim xlApp As Excel.Application</p><p>本地窗口</p><p>表达式 值 类型</p><p>xlApp Nothing <font color="#f73809">Application</font></p><p>Dim xlApp As Object</p><p>本地窗口</p><p>表达式 值 类型</p><p>xlApp Nothing <font color="#ff0000">Object</font></p></strong> <p>做成功能函数的方法</p><p>Function xlSheet() As Object</p><p> Dim xlApp As Object<br/> 'Dim xlsheet As Object <br/> <br/> ' 发生错误时跳到下一个语句继续执行<br/> On Error Resume Next<br/> ' 连接Excel应用程序<br/> Set xlApp = GetObject(, "Excel.Application")<br/> <br/> If Err.Number <> 0 Then<br/> Set xlApp = CreateObject("Excel.Application")<br/> xlApp.Visible = True<br/> xlApp.Workbooks.Add<br/> End If</p><p> ' 返回当前活动的工作表<br/> Set xlSheet = xlApp.ActiveSheet <br/>End Function<br/></p><p>调用</p><p>Sub lls()<br/> xlSheet.cells(1, 5).Value = "aa1"<br/>End Sub<br/></p><p>在程序中调用相当简单,只要一条指令搞定,Ok</p> 不错,是CAD与EXCEL连接的一个方法
页:
[1]