创建文件夹并保存PDF

时间:2015-09-01 06:02:36

标签: vba solidworks

我有一个应该执行以下操作的宏;

- 打开文件夹选择框(用户选择文件夹)

- 打开所选文件夹中的所有图形文件(逐个,一个接一个)

- 查看目录中是否有名为“PDF”的文件夹,如果没有则创建一个

- 将打开的图形文件保存为pdf,从引用模型中的自定义属性构建另存为名称

- 关闭图纸

- 转到下一个

现在我的代码宏将完成一个绘图,关闭绘图并显示msgbox,如果该“PDF”文件夹存在,如果该文件夹不存在,它将创建文件夹,保存打开的绘图,关闭绘图并失败“sFileName = Dir”

如果我注释掉“If Dir(PDFpath,vbDirectory)=”“那么MkDir PDFpath”并制作“pdfpath = currpath”它会完美运行并将图纸全部保存在所选目录中。

如何创建该文件夹并将PDF保存到其中?

Option Explicit

Dim swApp           As SldWorks.SldWorks
Dim swModel         As SldWorks.ModelDoc
Dim swDraw          As SldWorks.DrawingDoc
Dim swCustProp      As CustomPropertyManager
Dim swView          As SldWorks.View
Dim sFileName       As String
Dim vFileName       As String
Dim Path            As String
Dim nPath           As String
Dim nErrors         As Long
Dim nWarnings       As Long
Dim ConfigName      As String
Dim i               As Long
Dim valOut1         As String
Dim valOut2         As String
Dim resolvedValOut1 As String
Dim resolvedValOut2 As String
Dim PartNo          As String
Dim nFileName       As String
Dim swDocs          As Variant
Dim PDFpath         As String
Dim currpath        As String
Dim PartNoDes       As String

Sub main()
    Set swApp = Application.SldWorks
    Path = BrowseFolder("Select a Path/Folder")
    Path = Path + "\"
    sFileName = Dir(Path & "*.slddrw")
    Do Until sFileName = ""
        Set swModel = swApp.OpenDoc6(Path + sFileName, swDocDRAWING, swOpenDocOptions_Silent, "", nErrors, nWarnings)
        Set swModel = swApp.ActiveDoc
        Set swDraw = swApp.ActiveDoc
        Set swView = swDraw.GetFirstView
        Set swView = swView.GetNextView
        Set swModel = swView.ReferencedDocument
        currpath = Left(swDraw.GetPathName, InStrRev(swDraw.GetPathName, "\"))
        PDFpath = currpath & "PDF"
        If Dir(PDFpath, vbDirectory) = "" Then MkDir PDFpath

        If swModel.GetType = swDocPART Then
            PartNoDes = Mid(swDraw.GetPathName, InStrRev(swDraw.GetPathName, "\") + 1)
            PartNoDes = Right(PartNoDes, Len(PartNoDes) - 14)
            PartNoDes = Left(PartNoDes, Len(PartNoDes) - 7)
            PartNo = Mid(swModel.GetPathName, InStrRev(swModel.GetPathName, "\") + 1)
            PartNo = Left(PartNo, Len(PartNo) - 7)
            Set swCustProp = swModel.Extension.CustomPropertyManager(swView.ReferencedConfiguration)
            ConfigName = swView.ReferencedConfiguration
            swCustProp.Get2 "Description", valOut1, resolvedValOut1
            swCustProp.Get2 "Revision", valOut2, resolvedValOut2
            nFileName = PDFpath & "\" & PartNo & "-" & ConfigName & "-" & resolvedValOut2 & " " & PartNoDes
            swDraw.SaveAs3 nFileName & ".PDF", 0, 0

        ElseIf swModel.GetType = swDocASSEMBLY Then
            PartNoDes = Mid(swDraw.GetPathName, InStrRev(swDraw.GetPathName, "\") + 1)
            PartNoDes = Right(PartNoDes, Len(PartNoDes) - 11)
            PartNoDes = Left(PartNoDes, Len(PartNoDes) - 7)
            PartNo = Mid(swModel.GetPathName, InStrRev(swModel.GetPathName, "\") + 1)
            PartNo = Left(PartNo, Len(PartNo) - 7)
            Set swCustProp = swModel.Extension.CustomPropertyManager("")
            swCustProp.Get2 "Description", valOut1, resolvedValOut1
            swCustProp.Get2 "Revision", valOut2, resolvedValOut2
            nFileName = PDFpath & "\" & PartNo & "-" & resolvedValOut2 & " " & PartNoDes
            swDraw.SaveAs3 nFileName & ".PDF", 0, 0

        End If
        swApp.QuitDoc swDraw.GetPathName
        Set swDraw = Nothing
        Set swModel = Nothing
        sFileName = Dir
    Loop
MsgBox "All Done"

End Sub

1 个答案:

答案 0 :(得分:0)

我已经通过使用filesystemobject解决了这个问题。

见下面的代码;

Option Explicit

Dim swApp           As SldWorks.SldWorks
Dim swModel         As SldWorks.ModelDoc
Dim swDraw          As SldWorks.DrawingDoc
Dim swCustProp      As CustomPropertyManager
Dim swView          As SldWorks.View
Dim sFileName       As String
Dim Path            As String
Dim nPath           As String
Dim nErrors         As Long
Dim nWarnings       As Long
Dim ConfigName      As String
Dim i               As Long
Dim valOut1         As String
Dim valOut2         As String
Dim resolvedValOut1 As String
Dim resolvedValOut2 As String
Dim PartNo          As String
Dim nFileName       As String
Dim swDocs          As Variant
Dim PDFpath         As String
Dim PartNoDes       As String
Dim FSO             As Object
Dim FolderPath      As String
Dim strquotes(110)  As String
Dim lngIndex        As Long

Sub main()
    Set swApp = Application.SldWorks
    Path = BrowseFolder("Select a Path/Folder")
    Path = Path + "\"
    PDFpath = Path & "PDF"

    Set FSO = CreateObject("scripting.filesystemobject")

    FolderPath = PDFpath
    If Right(FolderPath, 1) <> "\" Then
        FolderPath = FolderPath & "\"
    End If

    If FSO.FolderExists(FolderPath) = False Then
        MkDir (PDFpath)
    Else
        'MsgBox "Folder exist"
    End If

    sFileName = Dir(Path & "*.slddrw")
    Do Until sFileName = ""

        Set swModel = swApp.OpenDoc6(Path + sFileName, swDocDRAWING, swOpenDocOptions_Silent, "", nErrors, nWarnings)
        Set swModel = swApp.ActiveDoc
        Set swDraw = swApp.ActiveDoc
        Set swView = swDraw.GetFirstView
        Set swView = swView.GetNextView
        Set swModel = swView.ReferencedDocument

        If swModel.GetType = swDocPART Then
            PartNoDes = Mid(swDraw.GetPathName, InStrRev(swDraw.GetPathName, "\") + 1)
            PartNoDes = Right(PartNoDes, Len(PartNoDes) - 14)
            PartNoDes = Left(PartNoDes, Len(PartNoDes) - 7)
            PartNo = Mid(swModel.GetPathName, InStrRev(swModel.GetPathName, "\") + 1)
            PartNo = Left(PartNo, Len(PartNo) - 7)
            Set swCustProp = swModel.Extension.CustomPropertyManager(swView.ReferencedConfiguration)
            ConfigName = swView.ReferencedConfiguration
            swCustProp.Get2 "Description", valOut1, resolvedValOut1
            swCustProp.Get2 "Revision", valOut2, resolvedValOut2
            nFileName = PDFpath & "\" & PartNo & "-" & ConfigName & "-" & resolvedValOut2 & " " & PartNoDes
            swDraw.SaveAs3 nFileName & ".PDF", 0, 0

        ElseIf swModel.GetType = swDocASSEMBLY Then
            PartNoDes = Mid(swDraw.GetPathName, InStrRev(swDraw.GetPathName, "\") + 1)
            PartNoDes = Right(PartNoDes, Len(PartNoDes) - 11)
            PartNoDes = Left(PartNoDes, Len(PartNoDes) - 7)
            PartNo = Mid(swModel.GetPathName, InStrRev(swModel.GetPathName, "\") + 1)
            PartNo = Left(PartNo, Len(PartNo) - 7)
            Set swCustProp = swModel.Extension.CustomPropertyManager("")
            swCustProp.Get2 "Description", valOut1, resolvedValOut1
            swCustProp.Get2 "Revision", valOut2, resolvedValOut2
            nFileName = PDFpath & "\" & PartNo & "-" & resolvedValOut2 & " " & PartNoDes
            swDraw.SaveAs3 nFileName & ".PDF", 0, 0

        End If
        swApp.QuitDoc swDraw.GetPathName
        Set swDraw = Nothing
        Set swModel = Nothing
        sFileName = Dir
    Loop
MsgBox ("All drawings in " & Path & " saved as PDF!" & vbNewLine & vbNewLine & "Lormanism of the day :" & vbNewLine & strquotes(lngIndex))

End Sub