US$0.00
0

SOLIDWORKS Macro to Save a Copy as an eDrawings File

Save the active SOLIDWORKS part, assembly or drawing as an eDrawings file without overwriting existing ones.
SOLIDWORKS Macro to Save a Copy as an eDrawings File
Picture of Amen Jlili

Amen Jlili

Amen Jlili is the founder and technical director of Blue Byte Systems Inc., a software company in Vancouver, Canada, specializing in automating SOLIDWORKS and PDM.

Description

  • Saves a copy of the active part, assembly or drawing as .eprt, .easm or .edrw.
  • The file is written next to the original.
  • If the file already exists, a number is added (Name_1, Name_2, …) so nothing is overwritten.

System Requirements

  • SOLIDWORKS Version: SOLIDWORKS 2014 or newer
  • Operating System: Windows 10 or later

Pre-Requisites

  • The active document must be saved.

Results

  • An eDrawings file next to the original document.
  • A message confirms the full path of the new file.

Steps to Set Up the Macro

  1. Open SOLIDWORKS and the document described in the pre-requisites.
  1. Create the macro
  • Go to Tools > Macro > New, give the .swp file a name and save it. The VBA editor opens.
  • Replace everything in the module with the code below.
  1. Run the macro
  • Press F5 in the VBA editor, or use Tools > Macro > Run.
  • For daily use, assign it to a toolbar button or keyboard shortcut via Tools > Customize.

VBA Macro Code

' Disclaimer:
' The code provided should be used at your own risk.
' Blue Byte Systems Inc. assumes no responsibility for any issues or damages that may arise from using or modifying this code.
' For more information, visit https://bluebyte.biz
' Reviewed and updated by Blue Byte Systems Inc.

Option Explicit

' Saves a copy of the active part, assembly or drawing as an eDrawings file
' (.eprt, .easm or .edrw) in the same folder as the original.
' If the file already exists, a number is added: Name_1.eprt, Name_2.eprt, ...

Sub main()
    Dim swApp As SldWorks.SldWorks
    Dim swModel As SldWorks.ModelDoc2
    Dim docPath As String
    Dim ext As String
    Dim basePath As String
    Dim outFile As String
    Dim counter As Long
    Dim errors As Long
    Dim warnings As Long

    Set swApp = Application.SldWorks
    Set swModel = swApp.ActiveDoc

    If swModel Is Nothing Then
        MsgBox "Open a document before running this macro.", vbExclamation
        Exit Sub
    End If

    Select Case swModel.GetType
        Case swDocPART:     ext = ".eprt"
        Case swDocASSEMBLY: ext = ".easm"
        Case swDocDRAWING:  ext = ".edrw"
        Case Else
            MsgBox "Unsupported document type.", vbExclamation
            Exit Sub
    End Select

    docPath = swModel.GetPathName
    If docPath = "" Then
        MsgBox "Save the document first.", vbExclamation
        Exit Sub
    End If

    ' Strip the extension, whatever its length
    basePath = Left(docPath, InStrRev(docPath, ".") - 1)

    ' Find a file name that does not exist yet
    outFile = basePath & ext
    Do While Dir(outFile) <> ""
        counter = counter + 1
        outFile = basePath & "_" & counter & ext
    Loop

    If swModel.Extension.SaveAs(outFile, swSaveAsCurrentVersion, _
        swSaveAsOptions_Silent + swSaveAsOptions_Copy, Nothing, errors, warnings) Then
        MsgBox "Saved: " & outFile, vbInformation
    Else
        MsgBox "Could not save the eDrawings file. Error code: " & errors, vbCritical
    End If
End Sub

Code Review Notes

We reviewed this macro before publishing it here. Changes from the original version:

  • The original only worked for parts and could loop forever when the file already existed, because the counter was never used in the file name. It has been rewritten.
  • A typo meant save errors were never reported. Errors now show a clear message.
  • Works for parts, assemblies and drawings, with any extension length.

Need to customize the macro?

Contact us and we will adapt this macro to your workflow, or build a complete SOLIDWORKS or PDM automation for your team.

Need this adapted to your workflow?

We build custom SOLIDWORKS macros, add-ins and PDM automation.

Picture of Amen Jlili

Amen Jlili

Amen Jlili is the founder and technical director of Blue Byte Systems Inc., a software company in Vancouver, Canada, specializing in automating SOLIDWORKS and PDM.

Leave a Comment

0
0
Your Cart
Your cart is emptyReturn to Shop