Hi Everyone,
I am attempting to use a macro to batch save all of the drawings in a folder as PDFs. The goal is to save the PDF using a file name built on configuration specific custom properties. Right now I'm having trouble getting the file names to build properly in all cases.
I started with code taken from this post, () and slightly modified for my needs. In Matt Jones' words, the macro is meant to
"
-Open a Folder Selection Box (where the user selects a folder)
-Open all the drawing files in the selected folder (one by one, one after the other)
-Check to see if there is a folder called "PDF" in the directory, if not then create one
-Save the open drawing file as a pdf, building the save as name from custom properties in the referenced model
-Close the drawing
-Move on to next one
"
My file name string is supposed to look like this, where italics are custom properties, sometimes configuration specific custom properties.
"Number - Description Rev-Revision.pdf"
What ends up happening, is that for some of the files the custom properties are returned as blank, so I end up saving over one PDF named " - Rev-.PDF".
Some of my files work though, and it ends up saving them properly like "3534-001 - MOTOR BRACKET BOTTOM PLATE REV-A.PDF".
And even still some others save as "3534-001 - MOTOR BRACKET BOTTOM PLATE REV-.PDF" which works except for the empty revision.
I think the problem is tied to files with or without custom configurations. It seems that the ones with custom configurations work rather well except for the revision, but those that are weldments or without custom configurations don't work at all.
Here is my code, and the macro file is also attached if that's useful
<p>Option Explicit</p><p></p><p></p><p>Dim swApp As SldWorks.SldWorks</p><p>Dim swModel As SldWorks.ModelDoc2</p><p>Dim sFileName As String</p><p>Dim vFileName As String</p><p>Dim Path As String</p><p>Dim nPath As String</p><p>Dim nErrors As Long</p><p>Dim nWarnings As Long</p><p></p><p></p><p>Dim swDraw As SldWorks.DrawingDoc</p><p>Dim swCustProp As CustomPropertyManager</p><p>Dim swView As SldWorks.View</p><p></p><p>Dim ConfigName As String</p><p>Dim i As Long</p><p></p><p></p><p>Dim valOut1 As String</p><p>Dim valOut2 As String</p><p>Dim valOut3 As String</p><p>Dim resolvedValOut1 As String</p><p>Dim resolvedValOut2 As String</p><p>Dim resolvedValOut3 As String</p><p></p><p></p><p></p><p></p><p>Dim PartNo As String</p><p>Dim nFileName As String</p><p>Dim swDocs As Variant</p><p>Dim PDFpath As String</p><p>Dim currpath As String</p><p>Dim PartNoDes As String</p><p></p><p></p><p></p><p>Sub main()</p><p></p><p></p><p> Set swApp = Application.SldWorks</p><p> </p><p> 'added by Deepak's suggestion</p><p> Dim swExportPDFData As SldWorks.ExportPdfData</p><p> Set swExportPDFData = swApp.GetExportFileData(1)</p><p> swExportPDFData.ViewPdfAfterSaving = False</p><p> </p><p> Path = BrowseFolder() '"Select a Path/Folder"</p><p> </p><p> Path = Path + "\\"</p><p> </p><p> 'moved from below</p><p> PDFpath = Path & "PDF"</p><p> If Dir(PDFpath, vbDirectory) = "" Then MkDir PDFpath</p><p> </p><p> sFileName = Dir(Path & "*.slddrw")</p><p> </p><p> </p><p> Do Until sFileName = ""</p><p></p><p></p><p> Set swModel = swApp.OpenDoc6(Path + sFileName, swDocDRAWING, swOpenDocOptions_Silent, "", nErrors, nWarnings)</p><p></p><p></p><p> Set swModel = swApp.ActiveDoc</p><p> Set swDraw = swApp.ActiveDoc</p><p> Set swView = swDraw.GetFirstView</p><p> Set swView = swView.GetNextView</p><p> Set swModel = swView.ReferencedDocument</p><p> </p><p> </p><p> </p><p> If swModel.GetType = swDocPART Then</p><p>' PartNoDes = Mid(swDraw.GetPathName, InStrRev(swDraw.GetPathName, "\\") + 1)</p><p>' PartNoDes = Right(PartNoDes, Len(PartNoDes) - 14)</p><p>' PartNoDes = Left(PartNoDes, Len(PartNoDes) - 7)</p><p>' PartNo = Mid(swModel.GetPathName, InStrRev(swModel.GetPathName, "\\") + 1)</p><p>' PartNo = Left(PartNo, Len(PartNo) - 7)</p><p></p><p></p><p> If ((swView.ReferencedConfiguration <> "Default") Or (swView.ReferencedConfiguration <> "Default<As Machined>") _</p><p> Or (swView.ReferencedConfiguration <> "Default<As Welded>")) Then</p><p> Set swCustProp = swModel.Extension.CustomPropertyManager(swView.ReferencedConfiguration)</p><p> Else</p><p> Set swCustProp = swModel.Extension.CustomPropertyManager("")</p><p> End If</p><p> </p><p> ConfigName = swView.ReferencedConfiguration</p><p> </p><p> swCustProp.Get2 "Number", valOut1, resolvedValOut1</p><p> swCustProp.Get2 "Description", valOut2, resolvedValOut2</p><p> swCustProp.Get2 "Revision", valOut3, resolvedValOut3</p><p> nFileName = PDFpath & "\\" & resolvedValOut1 & " - " & resolvedValOut2 & " REV-" & resolvedValOut3</p><p></p><p></p><p></p><p></p><p> swDraw.SaveAs3 nFileName & ".PDF", 0, 0</p><p> </p><p> ElseIf swModel.GetType = swDocASSEMBLY Then</p><p>' PartNoDes = Mid(swDraw.GetPathName, InStrRev(swDraw.GetPathName, "\\") + 1)</p><p>' PartNoDes = Right(PartNoDes, Len(PartNoDes) - 11)</p><p>' PartNoDes = Left(PartNoDes, Len(PartNoDes) - 7)</p><p>' PartNo = Mid(swModel.GetPathName, InStrRev(swModel.GetPathName, "\\") + 1)</p><p>' PartNo = Left(PartNo, Len(PartNo) - 7)</p><p> If (swView.ReferencedConfiguration <> "Default") Then</p><p> Set swCustProp = swModel.Extension.CustomPropertyManager(swView.ReferencedConfiguration)</p><p> Else</p><p> Set swCustProp = swModel.Extension.CustomPropertyManager("")</p><p> End If</p><p> ConfigName = swView.ReferencedConfiguration</p><p>' swCustProp.Get2 "Number", valOut1, resolvedValOut1</p><p>' swCustProp.Get2 "Description", valOut2, resolvedValOut2</p><p>' swCustProp.Get2 "Revision", valOut3, resolvedValOut3</p><p>' nFileName = PDFpath & "\\" & resolvedValOut1 & " - " & resolvedValOut2 & " REV-" & resolvedValOut3</p><p></p><p></p><p></p><p></p><p> swDraw.SaveAs3 nFileName & ".PDF", 0, 0</p><p> </p><p> End If</p><p> </p><p> swApp.QuitDoc swDraw.GetPathName</p><p> </p><p> Set swDraw = Nothing</p><p> </p><p> Set swModel = Nothing</p><p> </p><p> sFileName = Dir</p><p> </p><p> Loop</p><p></p><p></p><p>MsgBox "All Done"</p><p></p><p></p><p>End Sub</p><p></p><p></p><p></p><p></p><p>Function BrowseFolder(Optional Title As String, Optional TopFolder _</p><p> As String) As String</p><p> Dim objShell As New Shell32.Shell</p><p> Dim objFolder As Shell32.Folder</p><p></p><p></p><p>'If you use 16384 instead of 1 on the next line,</p><p>'files are also displayed</p><p> Set objFolder = objShell.BrowseforFolder(0, Title, 1, TopFolder)</p><p> </p><p> If Not objFolder Is Nothing Then</p><p> BrowseFolder = objFolder.Items.Item.Path</p><p> End If</p><p> </p><p></p><p></p><p>End Function</p>Message was edited by: Quentin TorgersonTried to make code appear as VBA syntax, having trouble
SolidworksApi macros