Sub OpenSwFiles() '打开文件Dim SwFname As String, k As Integer, Posn As Long, SwErr As LongDim SwApp As SldWorks.SldWorks, SwFileType As Byte, SwModel As ModelDoc2On Error Resume Next Set SwApp = GetObject(, "SldWorks.Application") If SwApp Is Nothing Then MsgBox "请先打开SolidWorks!", vbExclamation, "不正经的机械仙人" Exit Sub End If With ThisWorkbook.ActiveSheet Application.StatusBar = "正在打开:" If myselcondi Then For k = 1 To Selnumber ProcessBarUpdater k, Selnumber, "正在打开:", "共" & Selnumber & "个,第" & k & "个 ..." Posn = Selarray(k) SwFname = .Cells(Posn, 2) & "\" & .Cells(Posn, 3) & .Cells(Posn, 4) If Dir(SwFname) = "" Then .Cells(Posn, 1) = "文件不存在!" Else If UCase(.Cells(Posn, 4)) = ".SLDPRT" Then SwFileType = swDocPART Else SwFileType = swDocASSEMBLY End If Set SwModel = SwApp.ActivateDoc2(SwFname, False, SwErr) If SwModel Is Nothing Then Set SwModel = SwApp.OpenDoc(SwFname, SwFileType) If SwModel Is Nothing Then .Cells(Posn, 1) = "重名或高版本,打开失败!" Else .Cells(Posn, 1) = "打开成功!" End If Set SwModel = Nothing End If Next Else MsgBox "未选择正确行,程序结束!", vbExclamation, "不正经的机械仙人" End If End With Application.StatusBar = FalseEnd SubSub StartSwApp()'启动SW程序Dim SwApp As SldWorks.SldWorks, i As ByteDim SwRevision As String, SwOpenPath As StringOn Error Resume Next Set SwApp = GetObject(, "SldWorks.Application") If SwApp Is Nothing Then If ThisWorkbook.Sheets("参数设置").Range("C2").Value = "" Then Set SwApp = CreateObject("SldWorks.Application") ThisWorkbook.Sheets("参数设置").Range("C2").Value = SwApp.RevisionNumber SwApp.ExitApp End If SwRevision = Left(ThisWorkbook.Sheets("参数设置").Range("C2").Value, 2) SwOpenPath = myregedit("HKEY_LOCAL_MACHINE\SOFTWARE\Classes\SldWorks.Application." & SwRevision & "\shell\open\command\", "", "", "R") SwOpenPath = Left(SwOpenPath, InStrRev(SwOpenPath, " ") - 1) Shell SwOpenPath, vbNormalFocus Do Until Not SwApp Is Nothing '延迟等待 Set SwApp = GetObject(, "SldWorks.Application") Sleep 300 i = i + 1 If i > 100 Then Exit Do Loop If SwApp Is Nothing Then MsgBox "SolidWorks程序启动失败!", vbCritical, "不正经的机械仙人" Else MsgBox "SolidWorks程序已运行!", vbExclamation, "不正经的机械仙人" End If Set SwApp = NothingEnd Sub