MACRO LIBRARY

Save out sheets as individual drawings

Free public access. No account or subscription required.

Instructions

Preconditions
Drawing is open.
Result
Drawing sheets are saved to specified path.

Files

Save out sheets as individual drawings

Code

Module: mMain

'Save out drawings sheets as individual drawings
    
'Preconditions: drawing is open
'Results: drawings are saved to path specified in input box with this _
    naming format: <original drawing title>-<sheet name>.SLDDRW
    
'Notes:
'Drawing files with same name as new drawing files will be overwritten.
    
'Written by Keith Rice
'CADSharp LLC
'www.cadsharp.com
    
Option Explicit
Option Compare Text
    
Sub main()
    
    Dim swApp As SldWorks.SldWorks
    Dim swModel As SldWorks.ModelDoc2
    Dim swDraw As SldWorks.DrawingDoc
    Dim strFilePath As String
    Dim strNewFilePath As String
    Dim strExportFolder As String
    Dim strDrwTitle As String
    Dim vSheetNames As Variant
    Dim lngErr As Long, lngWarn As Long
    Dim i As Integer, j As Integer
    Dim fso As New FileSystemObject
    
    Set swApp = Application.SldWorks
    Set swModel = swApp.ActiveDoc
    
    If swModel Is Nothing Then Exit Sub
    If swModel.GetType <> swDocDRAWING Then Exit Sub
    
    strExportFolder = InputBox("Please enter a folder path to export drawing files to.")
    If strExportFolder = Empty Then Exit Sub
    If Right(strExportFolder, 1) <> "\" Then strExportFolder = strExportFolder & "\"
    
    If fso.FolderExists(strExportFolder) = False Then
        swApp.SendMsgToUser "Folder does not exist: " & vbCrLf & strExportFolder
        Exit Sub
    End If
    
    Set swDraw = swModel
    
    strFilePath = swModel.GetPathName
    
    vSheetNames = swDraw.GetSheetNames
    
    swApp.CloseDoc strFilePath
    
    For i = 0 To UBound(vSheetNames)
        
        strDrwTitle = Right(strFilePath, Len(strFilePath) - InStrRev(strFilePath, "\"))
        strDrwTitle = Replace(strDrwTitle, ".SLDDRW", Empty)
        strNewFilePath = strExportFolder & strDrwTitle & "-" & vSheetNames(i) & ".SLDDRW"
        
        fso.CopyFile strFilePath, strNewFilePath, True
        If fso.FileExists(strNewFilePath) = False Then
            swApp.SendMsgToUser "Failed to copy this file to destination folder:" & vbCrLf & strFilePath
            Exit Sub
        End If
        
        Set swModel = swApp.OpenDoc6(strNewFilePath, swDocDRAWING, _
            swOpenDocOptions_Silent, "", lngErr, lngWarn)
            
        If lngErr <> 0 Then
            swApp.SendMsgToUser "Failed to save drawing with error code " & _
                lngErr & vbCrLf & strNewFilePath
            Exit Sub
        End If
        
        Set swDraw = swModel
        
        For j = 0 To UBound(vSheetNames)
            If vSheetNames(j) <> vSheetNames(i) Then
                swDraw.ActivateSheet vSheetNames(i)
                swModel.Extension.SelectByID2 vSheetNames(j), "SHEET", 0, 0, 0, False, 0, Nothing, 0
                swModel.DeleteSelection True
            End If
        Next j
        
        swModel.Save3 swSaveAsOptions_Silent + swSaveAsOptions_Copy, lngErr, lngWarn
        
        If lngErr <> 0 Then
            swApp.SendMsgToUser "Failed to save drawing with error code " & _
                lngErr & vbCrLf & strNewFilePath
            Exit Sub
        End If
        
        swApp.CloseDoc strNewFilePath
        
    Next i
    
    swApp.SendMsgToUser "Finished processing drawing."
    
    Exit Sub
    
ErrHandler:
    swApp.SendMsgToUser "Unhandled exception while processing drawings. Exiting."
End Sub