Help automating a Macro

Hello all, new here!

Hoping to get some assistance. As per the code exerpt below, I cobbled together a Macro that when initiated will force the user to save their drawing and will then proceed to export a PDF and DWG, or DXF. (Comes up with a selection menu).

However rather than having the user click on a Macro button in Solidworks, I wanted to automate this further so that every time as user clicks the default save button in Solidworks, this Macro is initiated.

Granted, when clicking the save button in Solidworks would already take care of the user saving his work before the Exports happen, so you could likely lose the

swModel.Extension.RunCommand swCommands_SaveAs,

I found a resource here which looked to solve my problem : Run VBA macro automatically on document save using SOLIDWORKS API

However I cannot seem to get it to work, I am evidently misunderstanding how to implement this correctly, I am a novice at Macro and VB.

Could anyone help me with this ?. If required I am happy to donate towards getting this working.

Thanks in advance.

'Export Open Drawings as PDF_DWG (SW2019).swp ------------- 01/30/19

'Description: Macro to export all open drawings as PDF and DWG.

'Pre-Condition: An open drawing which has been saved and has one model view. The model should have the
' PartNumber, Revision and Description custom properties with values added.

'Post-Condition: Macro save active drawing as PDF and DXF in same location as the drawing file with
' PartNumber, Revision and Description custom properties values from referenced model as the file name.

' The macro would process all open drawings and would export only those drawings which has been saved and contains minimum one model view.

' Please back up your data before use and USE AT OWN RISK
' Created by Deepak Gupta (Boxer's SOLIDWORKS Blog, India)
' Disclaimer:
' This macro is provided as is. No claims, support, refund, safety net, or
' warranties are expressed or implied. By using this macro and/or its code in
' any way whatsoever, the user and any entities which the user represents,
' agree to hold the authors free of any and all liability.
' Free distribution and use of this code in other free works is welcome.
' You may redistribute it and/or modify it on the condition that this header is retained.
' All other forms of distribution (i.e., not free, fee for delivery, etc) are prohibited
' without the expressed written consent by the authors.
' Use at your own risk!
' ------------------------------------------------------------------------------

Option Explicit

Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swDrawModel As SldWorks.ModelDoc2
Dim swDraw As SldWorks.DrawingDoc
Dim swView As SldWorks.View
Dim nErrors As Long
Dim nWarnings As Long
Dim Revision As String
Dim Description As String
Dim PartNumber As String
Dim nFileName As String
Dim sFileName As String
Dim nResponse As Integer
Dim FileSave As Boolean
Dim sDrawingCol As New Collection

Sub Main()

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

swModel.Extension.RunCommand swCommands_SaveAs, Empty
' Check to see if a drawing is loaded.
If swDrawModel Is Nothing Then
MsgBox "There is no active drawing document"
Exit Sub
End If

Do While Not swDrawModel Is Nothing
If swDrawModel.GetType = swDocDRAWING Then
sDrawingCol.Add swDrawModel.GetPathName
Debug.Print swDrawModel.GetPathName
End If
Set swDrawModel = swDrawModel.GetNext

If sDrawingCol.Count > 0 Then

'Set file export type
nResponse = MsgBox("Select YES (PDF & DWG) OR NO (DXF) OR CANCEL (Exit/End)?", vbYesNoCancel)
If nResponse = vbYes Then
FileSave = True
ElseIf nResponse = vbNo Then
FileSave = False
ElseIf nResponse = vbCancel Then
Exit Sub
End If

MsgBox "There is no active drawing document"
Exit Sub
End If

Set swDrawModel = swApp.GetFirstDocument
Do While Not swDrawModel Is Nothing
If swDrawModel.GetType = swDocDRAWING Then
Set swDraw = swDrawModel
If swDraw.GetPathName <> "" Then
sFileName = Mid(swDraw.GetPathName, InStrRev(swDraw.GetPathName, "\") + 1)
swApp.ActivateDoc3 sFileName, False, swDontRebuildActiveDoc, nErrors
MsgBox "This drawing: " & UCase(swDraw.GetTitle) & " has not been saved, " & vbCrLf & _
"jumping to next drawing (if available) else ending the macro"
GoTo Jump
End If
Set swView = swDraw.GetFirstView
Set swView = swView.GetNextView

If Not swView Is Nothing Then
Set swModel = swView.ReferencedDocument
MsgBox "No model view found in this drawing: " & UCase(swDraw.GetTitle) & ", " & vbCrLf & _
"jumping to next drawing (if available) else ending the macro"
GoTo Jump
End If

'Get Model Properties
PartNumber = swModel.GetCustomInfoValue("", "Part Number")
Revision = swModel.GetCustomInfoValue("", "Revision")
Description = swModel.GetCustomInfoValue("", "Description")
If Revision = "" Then
Revision = ""
End If

'nFileName = Left(swDraw.GetPathName, InStrRev(swDraw.GetPathName, "\")) & PartNumber & "-" & Revision & " " & Description
' Comment out above line and remove comment from following line in case you want to simply save the draing file with its own name.
nFileName = Left(swDraw.GetPathName, InStrRev(swDraw.GetPathName, "\")) & sFileName
If FileSave = True Then
'Save as DWG (replace DWG with DXF in following line in case you want to save as DXF)
'Note that DWG will export to one or multiple sheets depending on your solidworks DWG export settings
swDraw.SaveAs3 nFileName & ".DWG", 0, 0
'Save as PDF`
swDraw.SaveAs3 nFileName & ".PDF", 0, 0
ElseIf FileSave = False Then
'Save as DWG (replace DWG with DXF in following line in case you want to save as DXF)
swDraw.SaveAs3 nFileName & ".DXF", 0, 0
End If
End If

Set swDrawModel = swDrawModel.GetNext
Set sDrawingCol = Nothing

End Sub

Hi @panzerscope , I see you’re still looking for help with this question that you’ve posted on another CAD forum as well. You received a suggestion from Deepak regarding the use of a macro that is already written and posted on codestack.

If this does not function as you would like, could you please elaborate on what it’s failing to do and perhaps we can assist with some tweaks to the macro code to fit your desired workflow. The more information you can provide, the better.

Hey there,

Unfortunately on the other forum I am unable to get logged in right now, so I wanted to pop my head in over here :slight_smile: So far as that solution, I mentioned in my post that I have already tried that but it is clear that either I am misunderstanding something regarding the instructions or that in someway the Macro codes does not play well with that method.

I am unsure which on it is. The Macro on its own works just fine. However when I implement it using that resource and I then do a test run, nothing happens. No errors, just seems as if it does not run at all :frowning:

So at this point a little stuck. I spent some time on variations but still could not implement it using that method so that the Macro runs on a Solidworks Save event.

Edit. Using the above method, I created the Macro again as linked below. Perhaps you can see what I have done incorrectly ?

If you read the comments carefully on CodeStack website, it says that the macro still needs to be run once when you start SolidWorks each time. Once you run the macro once and then click on save button, the macro will work fine.

In your case, ideal solution would be to convert this macro into an addin if you would like to fully automate it. But if you are a novice, I would not recommend doing it yourself. Either contact @AmenJlili or my self personally and I am sure one of us would be able to help you out. You will probably have to pay to the service though.

I would also not recommend exporting DXF, PDF and/ or DWG every time user saves the drawing as it will be annoying and time consuming. If you really like to make sure that you always have the latest copy, you can change it to export files just before the drawing is closed. I would also recommend removing the selection every time the export happens and instead do this selection by other means e.g. settings or configuration file or something else.

On the side note, if you have PDM Professional, it has out of box functionality to export files. Please ignore this if you are already aware of it.

Nilesh Patel


@panzerscope Welcome to cadoverflow and thank you for taking the time to post here!

Are you trying to make SOLIDWORKS save the drawing and export it to dxf, dwg or dxf when the user clicks on the save button?

This can be done with VBA through one of the FileSave event handlers. However, this introduces an additional level of complexity because you need to manage those event handlers through the lifecycle of the drawings (open and close events).

I agree with @nilesh that this piece of automation is better handled as an add-in.

If you are interested in services, hit me up through or through my email


Try Save Document Trigger of Toolbar+. You can use this macro if you want a flexible naming for your PDF


@artem, that’s a pretty cool tool. I haven’t had a chance to look into it much before now. It looks extremely useful, good work!


Thanks everyone for your suggestions @artem, that looks like an intriguing solution and I will have to check that one out for sure.

1 Like

Hello all,

So I have had some success with the CAD+ Toolbar add-in.

At the moment I have it configured so that my export macro is run upon a Save trigger. The only issue is that my macro is run before Solidworks has actually saved the drawing. My Macro can only export upon a saved drawing.

So in summary, I need it so that Solidworks first saves my new drawing and then runs the Macro. Is there a way to manipulate this ?

Or do I alter the Macro code so that the pre conditions are that the drawing does not already have to be saved in order for the exports to happen ? That being said, I am unsure which part of the code I would have to alter to change that behaviour.


There should be a post save event. I’m not too familiar with Artem’s code so have to ask him.

Check this example Selective Open Post-Notify Event Example (VBA) - 2017 - SOLIDWORKS API Help

It is likely my misunderstanding, but how would that help me to alter the behaviour so that a Solidworks Save event happens prior to the Macro being run ?

If you are using CAD+ Toolbar, I can’t really tell how to do that because I’m not sure how Artem’s code work. Sorry if I sound like a proper record but this sounds like a custom add-in waiting to be written.

To re-state your problem, you need an add-in that triggers your macro logic after/post the save event. The event handler that you should targeting is DAssemblyDocEvents_FileSavePostNotifyEventHandler Delegate (SolidWorks.Interop.sldworks) - 2017 - SOLIDWORKS API Help.

Feel free to reach to me at for a quote.