程序运行出现的问题
<P>程序如下:</P><P>Option Explicit</P>
<P>' 使用Excel数据表中的数据绘制一条直线<BR>Public Sub UseExcelData()<BR> Dim excelApp As Excel.Application<BR> Dim excelSheet As Excel.Worksheet<BR> <BR> <BR> <BR> ' 运行Excel应用程序<BR> Set excelApp = CreateObject("Excel.Application")<BR> excelApp.Visible = True<BR> <BR> ' 打开指定的Excel文件,获得指定的页<BR> excelApp.Workbooks.Open "d:\demo.xls"<BR> Set excelSheet = excelApp.ActiveWorkbook.Sheets("Sheet1")<BR> <BR> ' 使用指定页的数据绘图<BR> Dim lineObj As AcadLine<BR> Dim startPoint(0 To 2) As Double<BR> Dim endPoint(0 To 2) As Double<BR> <BR> startPoint(0) = excelSheet.Cells(1, 1).Value<BR> startPoint(1) = excelSheet.Cells(1, 2).Value<BR> startPoint(2) = excelSheet.Cells(1, 3).Value<BR> endPoint(0) = excelSheet.Cells(2, 1).Value<BR> endPoint(1) = excelSheet.Cells(2, 2).Value<BR> endPoint(2) = excelSheet.Cells(2, 3).Value<BR> <BR> Set lineObj = ThisDrawing.ModelSpace.AddLine(startPoint, endPoint)<BR> ZoomAll<BR> <BR> ' 退出Excel应用程序<BR> excelApp.Quit<BR>End Sub</P>
<P>程序一运行,就开始安装EXCEL,因为没有光盘,安装失败!可是实际上我的电脑上已经安装了EXCEL,不知道什么?请帮助解决,谢谢!</P> <P>放入安装盘,运行一次EXCEL后即可!</P> 还是不行,说是找不到安装所需文件SKU011.CAB。请指教!能否告知原因问什么会出现这种问题? <P>建议excel和CAD两个软件安装的时候选择全部安装,可以解决一些不可预知的问题,供参考。</P> 如果单独开启EXCEL或WORD也有此问题,则应该放入安装盘开启EXCEL更新一次即可。你的EXCEL是否不是在你当前用户下安装的!所以会出现此情况,如果切换到安装的路径,则不会这样! <P>我将EXCEL 重装了一次,问题解决了。但是,程序运行后,有时是成功的,但有时是失败的。而且,程序运行后每次重新打开那个EXCEL文件,都是只读形式的。程序如下:</P>
<P>Private Sub CommandButton1_Click()<BR> <BR> On Error Resume Next<BR> Set xlapp = GetObject(, "excel.application")<BR> If Err Then<BR> Err.Clear<BR> Set xlapp = CreateObject("excel.application")<BR> If Err Then<BR> Err.Clear<BR> MsgBox ("不能运行EXCEL,请检查是否安装了EXCEL")<BR> Exit Sub<BR> End If<BR> End If<BR> xlapp.workbooks.Open "D:\book3.xls"<BR> Set xlsheet = xlapp.activesheet<BR> xlsheet.range("a2").Value = "试验"<BR> activeworkbook.Save<BR> activeworkbook.Close<BR> <BR>End Sub</P>
<P>请问是什么原因?</P> <P> activeworkbook.Save<BR> activeworkbook.Close<BR>把这两句改成以下两句试试</P>
<P> xlapp.Quit '==========退出Excel<BR> Set xlapp = Nothing '===把控制权让给Excel</P> <P>经过高手的指导,上述问题已解决,谢谢!但是新的问题产生了:程序执行后,出现提示:是否保存对“BOOK3.XLS”的更改?,之后我又将activeworkbook.save加上去,可还是不行,请高手指点,该怎样实现对XLS文件的自动保存?总的程序如下:</P>
<P>Private Sub CommandButton1_Click()<BR> ' 安全创建选择集<BR> On Error Resume Next<BR> Dim SSet As AcadSelectionSet<BR> If Not IsNull(ThisDrawing.SelectionSets.Item("Example")) Then<BR> Set SSet = ThisDrawing.SelectionSets.Item("Example")<BR> SSet.Delete<BR> End If<BR> Set SSet = ThisDrawing.SelectionSets.Add("Example")<BR> <BR> Dim fType As Variant, fData As Variant ' 选择集过滤器<BR> Call CreateSSetFilter(fType, fData, 2, "title")<BR> SSet.Select acSelectionSetAll, , , fType, fData<BR> <BR> '获取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> Select Case varAttributes(Cnt).TagString<BR> Case "TITLE-CN-1"<BR> exltagname_1 = varAttributes(Cnt).TextString<BR> Case "TITLE-CN-2"<BR> exltagname_2 = varAttributes(Cnt).TextString<BR> Case "TITLE-CN-3"<BR> exltagname_3 = varAttributes(Cnt).TextString<BR> Case "TITLE-CN-4"<BR> exltagname_4 = varAttributes(Cnt).TextString<BR> End Select<BR> Next<BR> MsgBox exltagtag<BR> ' 删除选择集<BR> SSet.Delete<BR> On Error Resume Next<BR> Set xlapp = GetObject(, "excel.application")<BR> If Err Then<BR> Err.Clear<BR> Set xlapp = CreateObject("excel.application")<BR> If Err Then<BR> Err.Clear<BR> MsgBox ("不能运行EXCEL,请检查是否安装了EXCEL")<BR> Exit Sub<BR> End If<BR> End If</P>
<P> xlapp.workbooks.Open "D:\book3.xls"<BR> Set xlsheet = xlapp.activesheet<BR> xlsheet.range("A2").Value = exltagname_1<BR> activeworkbook.Save<BR> xlapp.Quit<BR> Set xlsheet = Nothing<BR> Set xlbook = Nothing<BR> Set xlapp = Nothing<BR> End Sub</P>
<P>另外一个问题,exltagname_1的值总是传不出来,故EXCEL文件中A2的值未更改。且用MSGBOX exltagname_1都显示不出他的值。但是exltagname_1对应的varAttributes(0).TextString的值却能够显示出来!请问为什么,该怎样解决?谢谢!<BR></P> Set xlapp = GetObject(, "excel.application")<BR> xlapp.Workbooks.Open FileName:="d:\book3.xls"<BR> xlapp.Worksheets("sheet1").Range("A2").Value = exltagname_1<BR> 经过高手的指导,第二个问题已解决,谢谢!但是第一个问题:程序执行后,出现提示:是否保存对“BOOK3.XLS”的更改?,之后我又将activeworkbook.save加上去,可还是不行,请高手指点,该怎样实现对XLS文件的自动保存?
页:
[1]
2