SolidWorks*Excel+VBA-打个招呼
- 2026-09-23 20:19:46
SolidWorks*Excel+VBA-打个招呼VBA二开的尽头还是逃不掉Excel。 这次是认真的! 先搜索,把需要处理的SW文件扒到Excel里: 可选仅当前文件夹,或包括子文件夹一起,里里外外筛一通。 找到文件并检索文件时间,顺带查下有无工程图。 
已关注
关注
重播 分享 赞
Option ExplicitSub OpenFolder() '打开文件夹Dim Swfolder, fs As ObjectOn Error Resume NextSet fs = CreateObject("Scripting.FileSystemObject")If Selection.Rows.Count = 1 And Selection.Columns.Count = 1 ThenSet Swfolder = fs.GetFolder(Selection.Value)If Swfolder Is Nothing ThenMsgBox "文件夹不存在" & Chr(13) & _"请检查所选单元格内容!", vbExclamation, "不正经的机械仙人"ElseShell "explorer " & Swfolder.Path, vbNormalFocusEnd IfElseMsgBox "请选择单一单元格!", vbExclamation, "不正经的机械仙人"End IfSet Swfolder = NothingSet fs = NothingEnd SubSub SearchSwFiles() '搜索文件Dim Rng As Range, Usedrng As Range, fs As FileSystemObjectDim Swpath As String, SearchSubfolder As Boolean, opt As ByteDim DrwFile As Stringopt = MsgBox("是否搜索子文件夹?", vbQuestion + vbYesNoCancel, "不正经的机械仙人")If opt = vbYes ThenSearchSubfolder = TrueElseIf opt = vbNo ThenSearchSubfolder = FalseElseExit SubEnd IfWith Application.ActiveSheetIf .Cells(2, 2) = "" ThenMsgBox "未在[B2]单元格输入搜索文件夹!", vbExclamation, "不正经的机械仙人"Exit SubElseIf Right(.Cells(2, 2), 1) = "\" Then.Cells(2, 2) = Left(.Cells(2, 2), Len(.Cells(2, 2)) - 1) '统一格式End IfSwpath = .Cells(2, 2)Set fs = CreateObject("Scripting.FileSystemObject")If Not fs.FolderExists(Swpath) Then.Range("A2").Value = "文件夹不存在×"MsgBox "文件夹不存在!", vbExclamation, "不正经的机械仙人"Exit SubElse.Range("A2").Value = "√"End IfSet Usedrng = Intersect(.UsedRange, .Range("C:C"))For Each Rng In UsedrngIf Rng.Value <> "" And Rng.Row > 2 Then.Cells(Rng.Row, 1) = .Cells(Rng.Row, 2) & "\" & .Cells(Rng.Row, 3) & .Cells(Rng.Row, 4)End IfNextSearchSwFiles_rec fs.GetFolder(Swpath), SearchSubfolder, 3Set Usedrng = Intersect(.UsedRange, .Range("A:A")) '刷新其余未匹配到的单元格For Each Rng In UsedrngIf Rng.Value = .Cells(Rng.Row, 2) & "\" & .Cells(Rng.Row, 3) & .Cells(Rng.Row, 4) ThenIf Dir(Rng.Value) <> "" Then.Cells(Rng.Row, 5) = fs.GetFile(Rng.Value).DateLastModifiedRng.ClearContentsElseRng.Value = "文件不存在!"End IfDrwFile = .Cells(Rng.Row, 2) & "\" & .Cells(Rng.Row, 3) & ".SLDDRW"If Dir(DrwFile) <> "" Then.Cells(Rng.Row, 6) = fs.GetFile(DrwFile).DateLastModifiedElse.Cells(Rng.Row, 6) = "无工程图"End IfEnd IfNextMsgBox "文件已经搜索完毕!", vbInformation, "不正经的机械仙人"End WithSet fs = NothingApplication.StatusBar = FalseEnd SubPrivate Sub SearchSwFiles_rec(Swpath As Folder, SearchSubfolder As Boolean, Seqe As Long)On Error Resume NextDim Swfile As file, TmpSwpath As Folder, fs As New FileSystemObjectDim SwFtype As String, SwFname As String, DrwFile As StringDim Posn As Long, Posnb As Long, FullSwFname As StringWith Application.ActiveSheetErr.Clear: Posn = SeqeApplication.StatusBar = "正在搜索: " & Swpath.PathFor Each Swfile In Swpath.FilesSwFname = Swfile.NameSwFtype = Mid(SwFname, InStrRev(SwFname, "."))If (UCase(SwFtype) = ".SLDPRT" Or UCase(SwFtype) = ".SLDASM") And Left(Swfile.Name, 1) <> "~" ThenPosnb = 0: FullSwFname = Swfile.PathPosnb = WorksheetFunction.Match(FullSwFname, .Range("A:A"), 0)If Posnb = 0 Then '加入表中'Do Until .Cells(Posn, 3) = ""Posn = Posn + 1Loop.Cells(Posn, 1) = "新文件!".Cells(Posn, 2) = Swpath.Path.Cells(Posn, 3) = Left(SwFname, InStrRev(SwFname, ".") - 1).Cells(Posn, 4) = SwFtype.Cells(Posn, 5) = Swfile.DateLastModifiedDrwFile = .Cells(Posn, 2) & "\" & .Cells(Posn, 3) & ".SLDDRW"If Dir(DrwFile) <> "" Then.Cells(Posn, 6) = fs.GetFile(DrwFile).DateLastModifiedElse.Cells(Posn, 6) = "无工程图"End IfErr.ClearElse.Cells(Posnb, 1).ClearContents.Cells(Posnb, 5) = Swfile.DateLastModifiedDrwFile = .Cells(Posnb, 2) & "\" & .Cells(Posnb, 3) & ".SLDDRW"If Dir(DrwFile) <> "" Then.Cells(Posnb, 6) = fs.GetFile(DrwFile).DateLastModifiedElse.Cells(Posnb, 6) = "无工程图"End IfEnd IfEnd IfNextIf SearchSubfolder ThenFor Each TmpSwpath In Swpath.SubFoldersSearchSwFiles_rec TmpSwpath, True, PosnNextEnd IfEnd WithSet fs = NothingEnd Sub
前辈如是说。

本文来自网友投稿或网络内容,如有侵犯您的权益请联系我们删除,联系邮箱:wyl860211@qq.com 。