I'm looking for a macro to create PDF and STP files based on the active drawing when the macro is run.

I'm looking for a macro to create PDF and STP files based on the active drawing when the macro is run. I would like the file to be saved to the following directory C:\\Users\\{Current User}\\SW FDO\\{Folder named after active document}. I also would like to append “_REV{Revision from custom properties}” to the saved file name. There are many threads similar to this out here and I feel like I have looked at them all without final a one size fits all solution, so I’m hoping someone can help me out. Any help would be greatly appreciated! Running SW2017 if that matters.

SolidworksApi/macros
Answers 15
FR
FR 2021-04-09 Best Answer

Here you go:

<p>Option Explicit</p><p>Dim swApp As SldWorks.SldWorks</p><p>Dim swModel As SldWorks.ModelDoc2</p><p>Dim swDrawModel As SldWorks.ModelDoc2</p><p>Dim swDraw As SldWorks.DrawingDoc</p><p>Dim swView As SldWorks.View</p><p>Dim swExportPDFData As SldWorks.ExportPdfData</p><p>Dim Revision As String</p><p>Dim FilePath As String</p><p>Dim FileName As String</p><p>Sub Main()</p><p>Set swApp = Application.SldWorks</p><p>Set swDrawModel = swApp.ActiveDoc</p><p>If swDrawModel Is Nothing Then</p><p>    MsgBox "There is no active document"</p><p>    Exit Sub</p><p>End If</p><p>If swDrawModel.GetType <> swDocumentTypes_e.swDocDRAWING Then</p><p>    MsgBox "There is no active drawing document"</p><p>    Exit Sub</p><p>End If</p><p>Set swDraw = swDrawModel</p><p>Set swView = swDraw.GetFirstView</p><p>Set swView = swView.GetNextView</p><p>If swView Is Nothing Then</p><p>    MsgBox "There is no drawing view"</p><p>    Exit Sub</p><p>End If</p><p>Set swModel = swView.ReferencedDocument</p><p>'Get Model Properties</p><p>Revision = <strong>swDrawModel</strong>.GetCustomInfoValue("", "Revision")</p><p>If Revision = "" Then Revision = "0"</p><p>'Get file name</p><p>FileName = swDraw.GetPathName</p><p>FileName = Right(FileName, Len(FileName) - InStrRev(FileName, "\"))</p><p>FileName = Left(FileName, InStrRev(FileName, ".") - 1)</p><p>'create folder</p><p>FilePath = Environ("USERPROFILE") & "\SW FDO\"</p><p>If Dir(FilePath, vbDirectory) = vbNullString Then MkDir FilePath</p><p>FilePath = FilePath & FileName & "\"</p><p>If Dir(FilePath, vbDirectory) = vbNullString Then MkDir FilePath</p><p>'Set save path</p><p>FilePath = FilePath & FileName & "_Rev" & Revision</p><p>Debug.Print FilePath</p><p>'Save as STEP</p><p>swModel.Extension.SaveAs FilePath & ".STEP", swSaveAsVersion_e.swSaveAsCurrentVersion, swSaveAsOptions_e.swSaveAsOptions_Silent, Nothing, Empty, Empty</p><p>'Save as PDF</p><p>Set swExportPDFData = swApp.GetExportFileData(1)</p><p>swExportPDFData.SetSheets swExportDataSheetsToExport_e.swExportData_ExportAllSheets, Empty</p><p>swExportPDFData.ViewPdfAfterSaving = False</p><p>swDrawModel.Extension.SaveAs FilePath & ".PDF", swSaveAsVersion_e.swSaveAsCurrentVersion, swSaveAsOptions_e.swSaveAsOptions_Silent, swExportPDFData, 0, 0</p><p><strong>MsgBox "Files saved:" & vbCr & FilePath & ".STEP" & vbCr & FilePath & ".PDF"</strong></p><p>End Sub</p>
RB
RB 2021-04-09

It isn't easy peasy....well I Will try to wait a little bit for your solution buddy...

DG
DG 2021-04-09

Thanks for mentioning ​. I recall to have made a macro to do both task from same macro but have not been able to find

DG
DG 2021-04-09
<p>Fifi Riri wrote:</p><p></p><p>I tested it on SW2018. It works. Shouldn't have problem on 2017</p><p style="min-height: 8pt; padding: 0px;"></p>

I've been away from machine from several days so not able to test your original codes though I doubt if the created STEP is empty. Also if you check API help file for SaveAs, check the remarks an there it says to have model open (at least for some extension you need to have the model active).

GS
GS 2021-04-09

Thanks for all of the help everyone! Fifi's latest code works great and has performed as expected when tested by other users.

GS
GS 2021-04-09

Fifi -

Worked great as posted. I've made a few tweaks (and need a few others).

Here are the additional things I would like to tweak:

  • I would like it to pull the Revision of the drawing, not the model (still trying to correct years of bad file management where they don't necessarily match)
  • I added a message box at the end, to alert the user that the action has completed and as a double check what was saved and where, but it is only returning the STEP file. How can I tweak this to return the path of both/all saved files?

Here is the code I have:

  1. Option Explicit
  2. Dim swApp As SldWorks.SldWorks
  3. Dim swModel As SldWorks.ModelDoc2
  4. Dim swDrawModel As SldWorks.ModelDoc2
  5. Dim swDraw As SldWorks.DrawingDoc
  6. Dim swView As SldWorks.View
  7. Dim swExportPDFData As SldWorks.ExportPdfData
  8. Dim Revision As String
  9. Dim FilePath As String
  10. Dim FileName As String
  11. Sub Main()
  12. Set swApp = Application.SldWorks
  13. Set swDrawModel = swApp.ActiveDoc
  14. If swDrawModel Is Nothing Then
  15. MsgBox "There is no active document"
  16. Exit Sub
  17. End If
  18. If swDrawModel.GetType <> swDocumentTypes_e.swDocDRAWING Then
  19. MsgBox "There is no active drawing document"
  20. Exit Sub
  21. End If
  22. Set swDraw = swDrawModel
  23. Set swView = swDraw.GetFirstView
  24. Set swView = swView.GetNextView
  25. If swView Is Nothing Then
  26. MsgBox "There is no drawing view"
  27. Exit Sub
  28. End If
  29. Set swModel = swView.ReferencedDocument
  30. 'Get Model Properties
  31. Revision = swModel.GetCustomInfoValue("", "Revision")
  32. If Revision = "" Then Revision = "0"
  33. 'Get file name
  34. FileName = swDraw.GetPathName
  35. FileName = Right(FileName, Len(FileName) - InStrRev(FileName, "\"))
  36. FileName = Left(FileName, InStrRev(FileName, ".") - 1)
  37. 'create folder
  38. FilePath = Environ("USERPROFILE") & "\SW FDO\"
  39. If Dir(FilePath, vbDirectory) = vbNullString Then MkDir FilePath
  40. FilePath = FilePath & FileName & "\"
  41. If Dir(FilePath, vbDirectory) = vbNullString Then MkDir FilePath
  42. 'Set save path
  43. FilePath = FilePath & FileName & "_REV" & Revision
  44. Debug.Print FilePath
  45. 'Save as STEP
  46. swModel.Extension.SaveAs FilePath & ".STEP", swSaveAsVersion_e.swSaveAsCurrentVersion, swSaveAsOptions_e.swSaveAsOptions_Silent, Nothing, Empty, Empty
  47. 'Save as PDF
  48. Set swExportPDFData = swApp.GetExportFileData(1)
  49. swExportPDFData.SetSheets swExportDataSheetsToExport_e.swExportData_ExportAllSheets, Empty
  50. swExportPDFData.ViewPdfAfterSaving = False
  51. swDrawModel.Extension.SaveAs FilePath & ".PDF", swSaveAsVersion_e.swSaveAsCurrentVersion, swSaveAsOptions_e.swSaveAsOptions_Silent, swExportPDFData, 0, 0
  52. MsgBox "SAVED " & FilePath
  53. End Sub
RB
RB 2021-04-09

​ has one solution on that case on 2 codes...but both in Only one I don't know....

FR
FR 2021-04-09

I tested it on SW2018. It works. Shouldn't have problem on 2017

Add this on line 30 if necessary:

swApp.ActivateDoc3 Mid(swModel.GetPathName, InStrRev(swModel.GetPathName, "\") + 1), False, swRebuildOnActivation_e.swRebuildActiveDoc, Empty

and I guess this on line 47:

swApp.CloseDoc Mid(swModel.GetPathName, InStrRev(swModel.GetPathName, "\") + 1)

swApp.ActivateDoc3 Mid(swDraw.GetPathName, InStrRev(swDraw.GetPathName, "\") + 1), False, swRebuildOnActivation_e.swRebuildActiveDoc, Empty

DG
DG 2021-04-09

You need to make the model active file in order to save as STEP from model

FR
FR 2021-04-09

Hello, try this:

<p>Option Explicit</p><p>Dim swApp As SldWorks.SldWorks</p><p>Dim swModel As SldWorks.ModelDoc2</p><p>Dim swDrawModel As SldWorks.ModelDoc2</p><p>Dim swDraw As SldWorks.DrawingDoc</p><p>Dim swView As SldWorks.View</p><p>Dim swExportPDFData As SldWorks.ExportPdfData</p><p>Dim Revision As String</p><p>Dim FilePath As String</p><p>Dim FileName As String</p><p>Sub Main()</p><p>Set swApp = Application.SldWorks</p><p>Set swDrawModel = swApp.ActiveDoc</p><p>If swDrawModel Is Nothing Then</p><p>    MsgBox "There is no active document"</p><p>    Exit Sub</p><p>End If</p><p>If swDrawModel.GetType <> swDocumentTypes_e.swDocDRAWING Then</p><p>    MsgBox "There is no active drawing document"</p><p>    Exit Sub</p><p>End If</p><p>Set swDraw = swDrawModel</p><p>Set swView = swDraw.GetFirstView</p><p>Set swView = swView.GetNextView</p><p>If swView Is Nothing Then</p><p>    MsgBox "There is no drawing view"</p><p>    Exit Sub</p><p>End If</p><p>Set swModel = swView.ReferencedDocument</p><p>'Get Model Properties</p><p>Revision = swModel.GetCustomInfoValue("", "_Rev")</p><p>If Revision = "" Then Revision = "0"</p><p>'Get file name</p><p>FileName = swDraw.GetPathName</p><p>FileName = Right(FileName, Len(FileName) - InStrRev(FileName, "\"))</p><p>FileName = Left(FileName, InStrRev(FileName, ".") - 1)</p><p>'create folder</p><p>FilePath = Environ("USERPROFILE") & "\SW FDO\"</p><p>If Dir(FilePath, vbDirectory) = vbNullString Then MkDir FilePath</p><p>FilePath = FilePath & FileName & "\"</p><p>If Dir(FilePath, vbDirectory) = vbNullString Then MkDir FilePath</p><p>'Set save path</p><p>FilePath = FilePath & FileName & "-Rev" & Revision</p><p>Debug.Print FilePath</p><p>'Save as STEP</p><p>swModel.Extension.SaveAs FilePath & ".STEP", swSaveAsVersion_e.swSaveAsCurrentVersion, swSaveAsOptions_e.swSaveAsOptions_Silent, Nothing, Empty, Empty</p><p>'Save as PDF</p><p>Set swExportPDFData = swApp.GetExportFileData(1)</p><p>swExportPDFData.SetSheets swExportDataSheetsToExport_e.swExportData_ExportAllSheets, Empty</p><p>swExportPDFData.ViewPdfAfterSaving = False</p><p>swDrawModel.Extension.SaveAs FilePath & ".PDF", swSaveAsVersion_e.swSaveAsCurrentVersion, swSaveAsOptions_e.swSaveAsOptions_Silent, swExportPDFData, 0, 0</p><p>End Sub</p>
MK
MK 2021-04-09

#TASK is the right tool.

If you have PDM Pro make use of PDM tasks.

As I understand you need to export reference model files to STEP, right?

GS
GS 2021-04-09

Thank you for the suggestion. Due to the large number of users, and vast difference in experience, I would like to be able to push this to our entire internal user group via a macro, with 1 click operation.

SC
SC 2021-04-09

#TASK

AC
AC 2021-11-19

This code is working great and it creates the folder as expected. However, I'm trying to piece together this code with another that would put the output files in the same directory as the drawing that is open but in a folder titled PDFs. I'd love for it to make the folder if it doesn't exist just like this macro does. 

GP
GP 2023-01-03

Hello, try this:


Sub main()

Set swApp = Application.SldWorks
Set Part = swApp.ActiveDoc

If Part Is Nothing Then

    MsgBox "There is no active document"

    Exit Sub

End If

Dim FilePath As String
Dim PathSize As Long
Dim PathNoExtention As String
Dim NewFilePath As String

FilePath = Part.GetPathName
PathSize = Strings.Len(FilePath)
PathNoExtention = Strings.Left(FilePath, PathSize - 6)
NewFilePath = PathNoExtention & "pdf"


Part.SaveAs2 NewFilePath, 0, True, False

End Sub