明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 965|回复: 6

求教大神利用VBA在CAD中打开CAD相应文件夹下的excel

[复制链接]
发表于 2017-8-18 17:05:47 | 显示全部楼层 |阅读模式
Set xlbook = xlapp.workbooks.Open("C:\Users\Administrator\Desktop\块属性导入EXCEL实验\2017年模板(农网物资含税).xls") '打开的EXCEL路径

这句话是打开固定位置的excel,如何改为打开CAD文件夹下的excel呢,CAD位置不固定
发表于 2017-9-7 21:57:13 来自手机 | 显示全部楼层
thisdrawing.path
回复 支持 1 反对 0

使用道具 举报

发表于 2017-10-7 19:23:23 | 显示全部楼层
回帖是一种美德!感谢楼主的无私分享 谢谢
回复 支持 1 反对 0

使用道具 举报

发表于 2017-8-20 16:44:24 | 显示全部楼层
cad指的是cad的安装目录还是你的cad图纸所在文件夹?
发表于 2017-8-25 22:01:55 | 显示全部楼层
加上浏览对话框就行吧
发表于 2017-8-26 09:29:38 | 显示全部楼层
Private Type BROWSEINFO
   hwndOwner As Long
   pidlRoot As Long
   pszDisplayName As Long
   lpszTitle As Long
   ulFlags As Long
   lpfnCallback As Long
   lParam As Long
   iImage As Long
End Type
Private Declare Function SHBrowseForFolder Lib "shell32" Alias "SHBrowseForFolderA" (lpBi As BROWSEINFO) As Long
Private Declare Sub CoTaskMemFree Lib "ole32.dll" (ByVal hMem As Long)
Private Declare Function SHGetPathFromIDList Lib "shell32.dll" Alias "SHGetPathFromIDListA" (ByVal pidl As Long, ByVal pszPath As String) As Long
Private Declare Function lstrcat Lib "kernel32" Alias "lstrcatA" (ByVal lpString1 As String, ByVal lpString2 As String) As Long
Private Sub Command1_Click()
Dim j As Long, strXlsFileName As String
Dim strPath As String, FileName As String, ExtName As String
ExtName = ".xls" '指定要查找的文件扩展名
strPath = App.Path & "\"   ' 指定路径为当前脚本目录。
strPath = BrowseForFolder '‘ "E:\资料\"
If Len(strPath) = 0 Then Exit Sub
FileName = Dir(strPath, vbNormal) ' 找寻第一项。
Do While FileName <> "" ' 开始循环。

    If InStr(LCase(FileName), ExtName) Then '如果MyName中的扩展名是XLS则打开表格文件
        j = j + 1
        strXlsFileName = strXlsFileName & vbCrLf & FileName
    End If
    FileName = Dir ' 查找下一个
Loop
MsgBox "找到xls文件" & j & "个" & vbCrLf & strXlsFileName
End Sub
'选择文件夹对话框
'函数:BrowseForFolder
Public Function BrowseForFolder(Optional sPrompt As String = "") As String
   
  '定义变量
   Dim iNull As Integer
   Dim lpIDList As Long
   Dim lResult As Long
   Dim sPath As String
   Dim udtBi As BROWSEINFO
  '初始化.....
   With udtBi
    .hwndOwner = 0
    .lpszTitle = lstrcat(sPrompt, "")
    .ulFlags = 1
   End With

  '调用API
   lpIDList = SHBrowseForFolder(udtBi)
  '得到返回结果
  If lpIDList Then
    sPath = String$(MAX_PATH, 0)
    lResult = SHGetPathFromIDList(lpIDList, sPath)
    Call CoTaskMemFree(lpIDList)
    iNull = InStr(sPath, vbNullChar)
    If iNull Then sPath = Left$(sPath, iNull - 1)
   End If
   BrowseForFolder = sPath

End Function
 楼主| 发表于 2018-5-16 07:53:24 | 显示全部楼层
好久没逛帖子了,谢谢各位
您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|CAD论坛|CAD教程|CAD下载|联系我们|关于明经|明经通道 ( 粤ICP备05003914号 )  
©2000-2023 明经通道 版权所有 本站代码,在未取得本站及作者授权的情况下,不得用于商业用途

GMT+8, 2024-11-25 06:45 , Processed in 0.142453 second(s), 24 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表