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
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>It isn't easy peasy....well I Will try to wait a little bit for your solution buddy...
Thanks for mentioning . I recall to have made a macro to do both task from same macro but have not been able to find
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).
Thanks for all of the help everyone! Fifi's latest code works great and has performed as expected when tested by other users.
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:
Here is the code I have:
has one solution on that case on 2 codes...but both in Only one I don't know....
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
You need to make the model active file in order to save as STEP from model
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>#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?
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.
#TASK
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.
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