906379829 发表于 2017-8-15 16:27:10

求教大神,在CAD VBA程序里面如何打开这个CAD文件路径下的一个excel文件

如题,CAD文件夹里面包含一个excel文件,如何利用CAD 里面的VBA程序打开这个excel文件呢

jpg102329 发表于 2017-8-16 10:13:57

(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)
)

906379829 发表于 2017-8-16 15:57:59

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
提示用户类型未定义

mikewolf2k 发表于 2017-8-16 12:54:21

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

906379829 发表于 2017-8-15 16:45:48

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

这是我一开始编的程序,结果一运行就提示类型未定义

zjy2999 发表于 2017-8-15 17:03:28

http://bbs.mjtd.com/thread-69284-1-1.html
http://bbs.mjtd.com/thread-67724-1-1.html
引用excel类库

mikewolf2k 发表于 2017-8-15 17:04:04

workbooks是什么?

vbcad 发表于 2017-8-15 19:23:16

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

906379829 发表于 2017-8-16 10:01:26

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

906379829 发表于 2017-8-16 10:13:25

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 '可见
程序运行到这一步的时候提示无效或不合格的引用

906379829 发表于 2017-8-16 10:23:01

jpg102329 发表于 2017-8-16 10:13


这是lisp吧

jpg102329 发表于 2017-8-16 10:28:41

906379829 发表于 2017-8-16 10:23
这是lisp吧

VBA同理
<"***大懒猪荣誉出品***">
页: [1] 2
查看完整版本: 求教大神,在CAD VBA程序里面如何打开这个CAD文件路径下的一个excel文件