weixin7944 发表于 2006-7-22 19:03:00

程序运行出现的问题

<P>程序如下:</P>
<P>Option Explicit</P>
<P>' 使用Excel数据表中的数据绘制一条直线<BR>Public Sub UseExcelData()<BR>&nbsp;&nbsp;&nbsp; Dim excelApp As Excel.Application<BR>&nbsp;&nbsp;&nbsp; Dim excelSheet As Excel.Worksheet<BR>&nbsp;&nbsp;&nbsp; <BR>&nbsp; <BR>&nbsp;&nbsp;&nbsp; <BR>&nbsp;&nbsp;&nbsp; ' 运行Excel应用程序<BR>&nbsp;&nbsp;&nbsp; Set excelApp = CreateObject("Excel.Application")<BR>&nbsp;&nbsp;&nbsp; excelApp.Visible = True<BR>&nbsp;&nbsp;&nbsp; <BR>&nbsp;&nbsp;&nbsp; ' 打开指定的Excel文件,获得指定的页<BR>&nbsp;&nbsp;&nbsp; excelApp.Workbooks.Open "d:\demo.xls"<BR>&nbsp;&nbsp;&nbsp; Set excelSheet = excelApp.ActiveWorkbook.Sheets("Sheet1")<BR>&nbsp;&nbsp;&nbsp; <BR>&nbsp;&nbsp;&nbsp; ' 使用指定页的数据绘图<BR>&nbsp;&nbsp;&nbsp; Dim lineObj As AcadLine<BR>&nbsp;&nbsp;&nbsp; Dim startPoint(0 To 2) As Double<BR>&nbsp;&nbsp;&nbsp; Dim endPoint(0 To 2) As Double<BR>&nbsp;&nbsp;&nbsp; <BR>&nbsp;&nbsp;&nbsp; startPoint(0) = excelSheet.Cells(1, 1).Value<BR>&nbsp;&nbsp;&nbsp; startPoint(1) = excelSheet.Cells(1, 2).Value<BR>&nbsp;&nbsp;&nbsp; startPoint(2) = excelSheet.Cells(1, 3).Value<BR>&nbsp;&nbsp;&nbsp; endPoint(0) = excelSheet.Cells(2, 1).Value<BR>&nbsp;&nbsp;&nbsp; endPoint(1) = excelSheet.Cells(2, 2).Value<BR>&nbsp;&nbsp;&nbsp; endPoint(2) = excelSheet.Cells(2, 3).Value<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <BR>&nbsp;&nbsp;&nbsp; Set lineObj = ThisDrawing.ModelSpace.AddLine(startPoint, endPoint)<BR>&nbsp;&nbsp;&nbsp; ZoomAll<BR>&nbsp;&nbsp;&nbsp; <BR>&nbsp;&nbsp;&nbsp; ' 退出Excel应用程序<BR>&nbsp;&nbsp;&nbsp; excelApp.Quit<BR>End Sub</P>
<P>程序一运行,就开始安装EXCEL,因为没有光盘,安装失败!可是实际上我的电脑上已经安装了EXCEL,不知道什么?请帮助解决,谢谢!</P>

jxh2003zfr 发表于 2006-7-22 22:34:00

<P>放入安装盘,运行一次EXCEL后即可!</P>

weixin7944 发表于 2006-7-23 14:16:00

还是不行,说是找不到安装所需文件SKU011.CAB。请指教!能否告知原因问什么会出现这种问题?

sdxylijian 发表于 2006-7-23 17:16:00

<P>建议excel和CAD两个软件安装的时候选择全部安装,可以解决一些不可预知的问题,供参考。</P>

jxh2003zfr 发表于 2006-7-24 09:13:00

如果单独开启EXCEL或WORD也有此问题,则应该放入安装盘开启EXCEL更新一次即可。你的EXCEL是否不是在你当前用户下安装的!所以会出现此情况,如果切换到安装的路径,则不会这样!

weixin7944 发表于 2006-8-4 09:12:00

<P>我将EXCEL 重装了一次,问题解决了。但是,程序运行后,有时是成功的,但有时是失败的。而且,程序运行后每次重新打开那个EXCEL文件,都是只读形式的。程序如下:</P>
<P>Private Sub CommandButton1_Click()<BR>&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp; On Error Resume Next<BR>&nbsp; Set xlapp = GetObject(, "excel.application")<BR>&nbsp; If Err Then<BR>&nbsp; Err.Clear<BR>&nbsp; Set xlapp = CreateObject("excel.application")<BR>&nbsp; If Err Then<BR>&nbsp; Err.Clear<BR>&nbsp; MsgBox ("不能运行EXCEL,请检查是否安装了EXCEL")<BR>&nbsp; Exit Sub<BR>&nbsp; End If<BR>&nbsp; End If<BR>&nbsp;&nbsp; xlapp.workbooks.Open "D:\book3.xls"<BR>&nbsp;&nbsp;&nbsp; Set xlsheet = xlapp.activesheet<BR>&nbsp; xlsheet.range("a2").Value =&nbsp;"试验"<BR>&nbsp; activeworkbook.Save<BR>&nbsp; activeworkbook.Close<BR>&nbsp;&nbsp;&nbsp; <BR>End Sub</P>
<P>请问是什么原因?</P>

mayuezxl 发表于 2006-8-4 10:11:00

<P>&nbsp;&nbsp; activeworkbook.Save<BR>&nbsp; activeworkbook.Close<BR>把这两句改成以下两句试试</P>
<P>&nbsp;xlapp.Quit '==========退出Excel<BR>&nbsp;&nbsp; Set xlapp = Nothing '===把控制权让给Excel</P>

weixin7944 发表于 2006-8-5 08:45:00

<P>经过高手的指导,上述问题已解决,谢谢!但是新的问题产生了:程序执行后,出现提示:是否保存对“BOOK3.XLS”的更改?,之后我又将activeworkbook.save加上去,可还是不行,请高手指点,该怎样实现对XLS文件的自动保存?总的程序如下:</P>
<P>Private Sub CommandButton1_Click()<BR>&nbsp; ' 安全创建选择集<BR>&nbsp;&nbsp;&nbsp; On Error Resume Next<BR>&nbsp;&nbsp;&nbsp; Dim SSet As AcadSelectionSet<BR>&nbsp;&nbsp;&nbsp; If Not IsNull(ThisDrawing.SelectionSets.Item("Example")) Then<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Set SSet = ThisDrawing.SelectionSets.Item("Example")<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; SSet.Delete<BR>&nbsp;&nbsp;&nbsp; End If<BR>&nbsp;&nbsp;&nbsp; Set SSet = ThisDrawing.SelectionSets.Add("Example")<BR>&nbsp;&nbsp;&nbsp; <BR>&nbsp;Dim fType As Variant, fData As Variant&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ' 选择集过滤器<BR>&nbsp;&nbsp;&nbsp; Call CreateSSetFilter(fType, fData, 2, "title")<BR>&nbsp;&nbsp;&nbsp; SSet.Select acSelectionSetAll, , , fType, fData<BR>&nbsp;&nbsp;&nbsp; <BR>&nbsp;&nbsp;&nbsp; '获取TITLE信息<BR>Dim Cnt As Integer<BR>Dim ssss As String<BR>ssss = ""<BR>Dim exltagname_1 As String<BR>Dim acadBlkTitleRef As AcadBlockReference<BR>Dim acadAttrTitle As AcadAttribute<BR>Dim varAttributes As Variant<BR>Set acadBlkTitleRef = SSet.Item(0)<BR>varAttributes = acadBlkTitleRef.GetAttributes<BR>For Cnt = LBound(varAttributes) To UBound(varAttributes)<BR>&nbsp;Select Case varAttributes(Cnt).TagString<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Case "TITLE-CN-1"<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; exltagname_1 = varAttributes(Cnt).TextString<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Case "TITLE-CN-2"<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; exltagname_2 = varAttributes(Cnt).TextString<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Case "TITLE-CN-3"<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; exltagname_3 = varAttributes(Cnt).TextString<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Case "TITLE-CN-4"<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; exltagname_4 = varAttributes(Cnt).TextString<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;End Select<BR>&nbsp;&nbsp;&nbsp; Next<BR>&nbsp;&nbsp; MsgBox exltagtag<BR>&nbsp;&nbsp;&nbsp; ' 删除选择集<BR>&nbsp;&nbsp;&nbsp; SSet.Delete<BR>&nbsp;&nbsp;&nbsp;&nbsp; On Error Resume Next<BR>&nbsp; Set xlapp = GetObject(, "excel.application")<BR>&nbsp; If Err Then<BR>&nbsp; Err.Clear<BR>&nbsp; Set xlapp = CreateObject("excel.application")<BR>&nbsp; If Err Then<BR>&nbsp; Err.Clear<BR>&nbsp; MsgBox ("不能运行EXCEL,请检查是否安装了EXCEL")<BR>&nbsp; Exit Sub<BR>&nbsp; End If<BR>&nbsp; End If</P>
<P>&nbsp;xlapp.workbooks.Open "D:\book3.xls"<BR>&nbsp;&nbsp;&nbsp; Set xlsheet = xlapp.activesheet<BR>&nbsp; xlsheet.range("A2").Value = exltagname_1<BR>&nbsp; activeworkbook.Save<BR>&nbsp;xlapp.Quit<BR>&nbsp; Set xlsheet = Nothing<BR>&nbsp; Set xlbook = Nothing<BR>&nbsp; Set xlapp = Nothing<BR>&nbsp; End Sub</P>
<P>另外一个问题,exltagname_1的值总是传不出来,故EXCEL文件中A2的值未更改。且用MSGBOX exltagname_1都显示不出他的值。但是exltagname_1对应的varAttributes(0).TextString的值却能够显示出来!请问为什么,该怎样解决?谢谢!<BR></P>

mayuezxl 发表于 2006-8-5 09:39:00

&nbsp;Set xlapp = GetObject(, "excel.application")<BR>&nbsp;&nbsp; xlapp.Workbooks.Open FileName:="d:\book3.xls"<BR>&nbsp;&nbsp; xlapp.Worksheets("sheet1").Range("A2").Value = exltagname_1<BR>

weixin7944 发表于 2006-8-6 10:09:00

经过高手的指导,第二个问题已解决,谢谢!但是第一个问题:程序执行后,出现提示:是否保存对“BOOK3.XLS”的更改?,之后我又将activeworkbook.save加上去,可还是不行,请高手指点,该怎样实现对XLS文件的自动保存?
页: [1] 2
查看完整版本: 程序运行出现的问题