求教大神,在CAD VBA程序里面如何打开这个CAD文件路径下的一个excel文件
如题,CAD文件夹里面包含一个excel文件,如何利用CAD 里面的VBA程序打开这个excel文件呢(defun c:tt(/ CAD DIR DOC EXCEL WORKBOOKS X XLS)
(setq cad(vlax-get-acad-object))
(setq doc(vla-get-ActiveDocument cad))
(setq dir(vl-filename-directory(vlax-get doc 'FullName)))
(setq xls(vl-directory-files dir"*.xls*"1));xls文件列表
(setq excel(vlax-get-or-create-object "excel.application"))
(setq xls(mapcar'(lambda(x)(strcat dir "\\"x))xls))
(vla-put-visible excel 1)
(if excel
(progn
(setq Workbooks(vlax-get-property excel 'Workbooks))
(foreach x xls(vlax-invoke-method WorkBooks 'Open x))
)
(list(alert"未安装excel")(exit)))
(prompt"***大懒猪荣誉出品***")
(princ)
) vbcad 发表于 2017-8-15 19:23
Dim MyPath As String, MyName As String
Dim objExcel As Object, objBook As Object, objSheet As Objec ...
Public Sub 导材料实验()
Dim MyPath As String
Dim objExcel As Object, objBook As Object
Dim objSheet As Excel.Workbook
Set objExcel = CreateObject("Excel.Application") '建立EXCEL对象
MyPath = Dir(ThisDrawing.Path & "\*.xls") ' 指定路径为当前脚本目录。
Do While MyPath <> "" ' 开始循环。
Set objSheet = Workbooks.Open(ThisWorkbook.Path & "\" & MyPath)
Set objBook = objExcel.Workbooks.Open(FilePath) '打开表格文件
MyPath = Dir ' 查找下一个
Loop
End Sub
我用的你的程序,稍作修改做的,运行到Dim objSheet As Excel.Workbook
提示用户类型未定义 Sub LinkExcel(ExcelApp)
On Error Resume Next
Set ExcelApp = GetObject(, "Excel.application")
If Err Then
Err.Clear
Set ExcelApp = CreateObject("Excel.application")
If Err Then
MsgBox ("不能运行Excel,请检查是否安装了Excel")
Exit Sub
End If
ExcelApp.Application.Visible = True
ExcelApp.Workbooks.Add
End If
ExcelApp.StatusBar = "Ready"
End Sub
Public Sub 导材料实验()
Dim wb As Workbook, wk As Workbook, myfile$
myfile = Dir(ThisDrawing.Path & "\*.xls")
Set wb = Workbooks.Open(ThisDrawing.Path & "\" & myfile)
Set wk = GetObject(ThisDrawing.Path & "\" & myfile)
End Sub
这是我一开始编的程序,结果一运行就提示类型未定义 http://bbs.mjtd.com/thread-69284-1-1.html
http://bbs.mjtd.com/thread-67724-1-1.html
引用excel类库 workbooks是什么? Dim MyPath As String, MyName As String
Dim objExcel As Object, objBook As Object, objSheet As Object
Set objExcel = CreateObject("Excel.Application") '建立EXCEL对象
objExcel .Visible = True'可见
MyPath = App.Path& "\"' 指定路径为当前脚本目录。
MyName = Dir(MyPath, vbNormal) ' 找寻第一项。
Do While MyName <> "" ' 开始循环。
Debug.Print MyName ' 将其名称显示出来。
if instr(lcase(MyName),".xls") then '如果MyName中的扩展名是XLS则打开表格文件
Set objBook = objExcel.Workbooks.Open(FilePath) '打开表格文件
endif
MyName = Dir ' 查找下一个
Loop vbcad 发表于 2017-8-15 19:23
Dim MyPath As String, MyName As String
Dim objExcel As Object, objBook As Object, objSheet As Objec ...
C:\Users\Administrator\Desktop vbcad 发表于 2017-8-15 19:23
Dim MyPath As String, MyName As String
Dim objExcel As Object, objBook As Object, objSheet As Objec ...
objExcel .Visible = True '可见
程序运行到这一步的时候提示无效或不合格的引用 jpg102329 发表于 2017-8-16 10:13
这是lisp吧 906379829 发表于 2017-8-16 10:23
这是lisp吧
VBA同理
<"***大懒猪荣誉出品***">
页:
[1]
2