我已经使用 VBA 为 Solidworks 编写了一个宏。宏将所有打开的工程图文件保存为 PDF(从工程图文件中的参考模型收集信息以构建文件名)到它在打开的工程图文件目录中创建的文件夹中,然后关闭工程图并继续下一个一。
我的问题是它将任何打开的绘图文件中的所有 PDF 保存到它从第一张绘图创建的同一个文件夹中。因此,如果我有多个项目,我正在处理所有 PDF 文件进入创建的第一个文件夹,直到我重新启动计算机才会出现这种情况。
我是否需要清除一些值或引用,以便它可以使用相同的宏执行多个项目?
请注意这里有一些加倍和不必要的东西,但这只是因为在我处理它的同时,我想在我忘记它们之前为未来保留一些选择!
Option Explicit
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swDraw As SldWorks.DrawingDoc
Dim swCustProp As CustomPropertyManager
Dim valOut1 As String
Dim valOut2 As String
Dim resolvedValOut1 As String
Dim resolvedValOut2 As String
Dim ConfigName As String
Dim PartNo As String
Dim FullName As String
Dim nFileName As String
Dim swView As SldWorks.View
Dim PDFpath As String
Dim Filename As String
Dim currpath As String
Sub main()
Dim swDocs As Variant
Dim i As Integer
Set swApp = Application.SldWorks
swDocs = swApp.GetDocuments
For i = 0 To UBound(swDocs)
Set swModel = swDocs(i)
If swModel.GetType = swDocDRAWING Then
currpath = Left(Filename, InStrRev(Filename, "\"))
Filename = Right(swModel.GetPathName, Len(swModel.GetPathName) - InStrRev(swModel.GetPathName, "\"))
PDFpath = currpath & "PDF"
Set swDraw = swModel
Set swView = swDraw.GetFirstView
Set swView = swView.GetNextView
Set swModel = swView.ReferencedDocument
currpath = Left(Filename, InStrRev(Filename, "\"))
Filename = Right(swModel.GetPathName, Len(swModel.GetPathName) - InStrRev(swModel.GetPathName, "\"))
PDFpath = currpath & "PDF"
If (swModel.GetType = swDocPART) Then
Set swModel = swView.ReferencedDocument
Set swView = swDraw.GetFirstView
Set swView = swView.GetNextView
ConfigName = swView.ReferencedConfiguration
FullName = swModel.GetTitle
PartNo = Left(FullName, Len(FullName) - 7)
Set swCustProp = swModel.Extension.CustomPropertyManager(ConfigName)
swCustProp.Get2 "Description", valOut1, resolvedValOut1
swCustProp.Get2 "Revision", valOut2, resolvedValOut2
If Dir(PDFpath, vbDirectory) = "" Then MkDir PDFpath
nFileName = PDFpath & "\" & PartNo & "-" & ConfigName & "-" & resolvedValOut2 & " " & resolvedValOut1
swDraw.SaveAs3 nFileName & ".PDF", 0, 0
'MsgBox nFileName & ".PDF" + " Saved as a PDF"
swApp.QuitDoc swDraw.GetPathName
ElseIf (swModel.GetType = swDocASSEMBLY) Then
Set swView = swDraw.GetFirstView
Set swView = swView.GetNextView
Set swModel = swView.ReferencedDocument
ConfigName = swView.ReferencedConfiguration
FullName = swModel.GetTitle
PartNo = Left(FullName, Len(FullName) - 7)
Set swCustProp = swModel.Extension.CustomPropertyManager("")
swCustProp.Get2 "Description", valOut1, resolvedValOut1
swCustProp.Get2 "Revision", valOut2, resolvedValOut2
If Dir(PDFpath, vbDirectory) = "" Then MkDir PDFpath
nFileName = PDFpath & "\" & PartNo & "-" & resolvedValOut2 & " " & resolvedValOut1
swDraw.SaveAs3 nFileName & ".PDF", 0, 0
swApp.QuitDoc swDraw.GetPathName
End If
End If
Next i
MsgBox ("All open drawings saved as PDF!" & vbNewLine & vbNewLine & "That was too fast and too furious.")
End Sub