Save As PDF Macro with Custom Properties
Save As PDF Macro with Custom Properties
(OP)
Hello,
I'm looking for a Save As PDF Macro that's a little different from the ones I've found.
Here's what I'm looking for:
- Ability to save multiple open drawings as PDF files
- Save in original folder as drawing files
- Automatically name pdf file the name of drawing file AND name it the current revision (ex: saving "Connecting_Bar.slddrw" which happens to be REV2 in the custom part properties to "Connecting_Bar-Rev2.PDF"
I'm using solidworks 08
Below is the macro code I currently use:
Dim swApp As SldWorks.SldWorks
Dim Part As SldWorks.ModelDoc2
Sub main()
Set swApp = Application.SldWorks
Set Part = swApp.ActiveDoc
Dim FilePath As String
Dim PathSize As Long
Dim PathNoExtension As String
Dim NewFilePath As String
FilePath = Part.GetPathName
PathSize = Strings.Len(FilePath)
PathNoExtension = Strings.Left(FilePath, PathSize - 6)
NewFilePath = PathNoExtension & "pdf"
Part.SaveAs2 NewFilePath, 0, True, fale
End Sub
Anyone have a macro like this?
I'm looking for a Save As PDF Macro that's a little different from the ones I've found.
Here's what I'm looking for:
- Ability to save multiple open drawings as PDF files
- Save in original folder as drawing files
- Automatically name pdf file the name of drawing file AND name it the current revision (ex: saving "Connecting_Bar.slddrw" which happens to be REV2 in the custom part properties to "Connecting_Bar-Rev2.PDF"
I'm using solidworks 08
Below is the macro code I currently use:
Dim swApp As SldWorks.SldWorks
Dim Part As SldWorks.ModelDoc2
Sub main()
Set swApp = Application.SldWorks
Set Part = swApp.ActiveDoc
Dim FilePath As String
Dim PathSize As Long
Dim PathNoExtension As String
Dim NewFilePath As String
FilePath = Part.GetPathName
PathSize = Strings.Len(FilePath)
PathNoExtension = Strings.Left(FilePath, PathSize - 6)
NewFilePath = PathNoExtension & "pdf"
Part.SaveAs2 NewFilePath, 0, True, fale
End Sub
Anyone have a macro like this?






RE: Save As PDF Macro with Custom Properties
Be sure to post your finished code because the ability to saveas pdf "all open drawings" is something that I have wanted to do... but haven't had the time.
CODE
' Save Drawing as PDF and DXF
' SaveDwgAsPDF.swp - Original functional code by Lee Bell on 02/10/02.
' Modified for SolidWorks 2006 by JB on 3/31/06.
' Modified by Matthew Lorono (http://sw.fcsuper.com) on 4/24/06.
' Copyright 2006, 2007
' Modified by Dustin Biber on 11/11/2008
' Based on publically available information found at:
' http://www.eng-tips.com/viewthread.cfm?qid=151087
' Contact: fcsuper@aol.com (Matthew Lorono)
' 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. If any portion of
' this code is used in other works, credit to the authors must be placed in
' that work within a user viewable location (e.g., macro header). 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!
' ------------------------------------------------------------------------------
' Description:
' Saves the active drawing or current view of a model or assembly as a PDF and DXF to a specified directory.
' ------------------------------------------------------------------------------
' Version - By Matthew Lorono, Copyright 2006
' 1.00 * Created Macro from various sources at the above
' website.
' * Added error handling for no docs loaded.
' * Modified error handling for failed save.
' * Added/modified comments.
' * Cleaned up user interface.
' 1.10 * Add nondrawing support
' * Add error handling for missing directory
' 1.20 * Add error handling for missing document path
' * Add user input/confirmation of save folder
' 1.21 * Expand header to prohibit money based distribution
' (such as for-profit or fee based) of this macro.
' 1.22 * Add save status to lower left status bar pane; some
' clean up; add detail to description.
' 1.23 * Added DXF functionality.
' ------------------------------------------------------------------------------
Option Explicit
Dim SwApp As SldWorks.SldWorks
Dim Model As SldWorks.ModelDoc2
Dim swModelDocExt As SldWorks.ModelDocExtension
Dim swExportPDFData As SldWorks.ExportPdfData
Dim MyPathPDF, MyPathDWG, ModName, NewNamePDF, NewNameDWG As String
Dim Rev As String
Dim dPathName As String
'Dim MyPathConfPDF, MyPathConfDWG As String
Dim fso As Object
Dim MBpdf, MBdwg As Boolean
Dim Errs As Long
Dim Warnings As Long
Dim swFrame As SldWorks.Frame
Dim Sheet1, FlatPatternSheet As String
Dim strSheetName(4) As String
Dim varSheetName As Variant
Dim i As Long
Sub main()
Set SwApp = Application.SldWorks
SwApp.Visible = True
Set Model = SwApp.ActiveDoc
Set swFrame = SwApp.Frame
Set swModelDocExt = Model.Extension
Set swExportPDFData = SwApp.GetExportFileData(1)
' Names of the sheets
'strSheetName(0) = "Sheet1"
'strSheetName(1) = "Sheet2"
' strSheetName(2) = "Sheet3"
' strSheetName(3) = "Sheet4"
'varSheetName = strSheetName
' Error handler for no document loaded
If Model Is Nothing Then MsgBox "No document loaded!", vbCritical: End
' Use one of the three following options for PDF save location
' Comment out the options with are not used.
' Option 1: Use the current directory
' MyPath = CurDir
' Option 2: Specify the directory you want to use
'MyPathPDF = "D:\_DesignFiles\SolidWorks\Temp"
'MyPathDWG = "D:\_DesignFiles\SolidWorks\Temp"
'MyPathPDF = "N:\Engineering\Public\Drawings"
'MyPathDWG = "N:\Engineering\Public\CAM"
' Option 3: Use the drawing folder
MyPathPDF = Left(Model.GetPathName, InStrRev(Model.GetPathName, "\") - 1)
'MyPathDWG = Left(Model.GetPathName, InStrRev(Model.GetPathName, "\") - 1)
' Call correct sub
If Model.GetType <> 3 Then Call notdrawing
Call ifdrawing
End Sub
Sub notdrawing()
' Get documnet save path
dPathName = Model.GetPathName()
' Error handler if no save path
If ("" = dPathName) Then MsgBox ("This document has not been saved yet"), vbCritical: End
' Set PDF file name
ModName = Left(Model.GetTitle, InStrRev(Model.GetTitle, ".") - 1)
Call alldoc
End Sub
Sub ifdrawing()
' Set PDF file name
Rev = Model.CustomInfo2("", "Revision")
Sheet1 = Model.ActivateSheet("Sheet1")
ModName = Left(Model.GetTitle, InStrRev(Model.GetTitle, " Sheet") - 3) + "_Rev" + Rev
Call alldoc
End Sub
Sub alldoc()
' See PDF and DXF file name with extention .pdf or .dwg
NewNamePDF = ModName & ".pdf"
' Get path and user confirmation
'MyPathConfPDF = InputBox("By Matthew Lorono" & Chr(13) & Chr(13) & "No notification will occur for " & Chr(13) & "successful PDF creation." & Chr(13) & Chr(13) & "Save " & NewNamePDF & " to:", "Confirm PDF Save Path", MyPathPDF)
'MyPathConfPDF = MyPathPDF & "\" & NewNamePDF
'If MyPathConfPDF = "" Then MsgBox "Save As PDF cancelled by user.", vbInformation: End
' Get path and user confirmation
'MyPathConfDWG = InputBox("By Matthew Lorono" & Chr(13) & Chr(13) & "No notification will occur for " & Chr(13) & "successful DXF creation." & Chr(13) & Chr(13) & "Save " & NewNameDXF & " to:", "Confirm DXF Save Path", MyPathDXF)
'MyPathConfDWG = MyPathDWG & "\" & NewNameDWG
'If MyPathConfDWG = "" Then MsgBox "Save As DWG cancelled by user.", vbInformation: End
' Determine if directory exists
Set fso = CreateObject("Scripting.FileSystemObject")
If (Not fso.FolderExists(MyPathPDF)) Then MsgBox (MyPathPDF + " does not exist!"), vbCritical: End
'If (Not fso.FolderExists(MyPathDWG)) Then MsgBox (MyPathDWG + " does not exist!"), vbCritical: End
' PDF Creation
swFrame.SetStatusBarText "Saving files"
MBpdf = swExportPDFData.SetSheets(swExportData_ExportAllSheets, varSheetName)
MBpdf = swModelDocExt.SaveAs(MyPathPDF & "\" & NewNamePDF, 0, 0, swExportPDFData, Errs, Warnings)
'MBpdf = Model.SaveAs4(MyPathConfPDF & "\" & NewNamePDF, swSaveAsCurrentVersion, swSaveAsOptions_Silent, Errs, Warnings)
' DWG Creation
'varSheetName = Model.GetSheetNames
'For i = 0 To UBound(varSheetName)
'MBdwg = Model.ActivateSheet(varSheetName(i))
'NewNameDWG = ModName & "_" & varSheetName(i) & ".dwg"
'MBdwg = Model.SaveAs4(MyPathDWG & "\" & NewNameDWG, swSaveAsCurrentVersion, swSaveAsOptions_Silent, Errs, Warnings)
'Debug.Assert MBdwg
'Next i
' Switch back to first sheet
'MBdwg = Model.ActivateSheet(varSheetName(0))
' Warnings to user on Error
' MsgBox "Errors: " & Errs & vbCrLf & "Warnings: " & Warnings
If Warnings <> 0 Then
swFrame.SetStatusBarText "Warnings during save"
MsgBox "There were warnings. File creation may have failed. Verify" & Chr(13) & "results and check possible causes.", vbExclamation
Else
End If
If MBpdf = False Then
swFrame.SetStatusBarText "Save failure"
MsgBox "PDF creation has failed! Check Add-ins (if S/W 2005 or older)," & Chr(13) & "available disk space or other possible causes.", vbCritical
Else
End If
'If MBdwg = False Then
'swFrame.SetStatusBarText "Save failure"
'MsgBox "DWG creation has failed!" & Chr(13) & "Check available disk space or other possible causes.", vbCritical
'Else
'End If
Call last
End Sub
Sub last()
swFrame.SetStatusBarText "Done"
' Clear immediate values
Set Model = Nothing
Set MyPathPDF = Nothing
'Set MyPathDWG = Nothing
Set swFrame = Nothing
Set fso = Nothing
End
End Sub
This macro needs an icon associated to it, so you can start it in the .main module.
-Dustin
Professional Engineer
Pretty good with SolidWorks
RE: Save As PDF Macro with Custom Properties
Matt Lorono
Lorono's SolidWorks Resources & SolidWorks Legion
&
RE: Save As PDF Macro with Custom Properties
BTW... I do like your folder selection macro. Very helpful.
-Dustin
Professional Engineer
Pretty good with SolidWorks
RE: Save As PDF Macro with Custom Properties
-Dustin
Professional Engineer
Pretty good with SolidWorks
RE: Save As PDF Macro with Custom Properties
But I assume what I have to come up with goes something like:
1. Dim Rev As String
2. Setting "Rev" variable to equal the Revision custom property
3. Including the "Rev" variable in the FilePath = Part.GetPathName line of code?
Whats the specific line of code to grab the revision custom property from a part file?
RE: Save As PDF Macro with Custom Properties
CODE
grabs the revision value and assigns it to the Rev variable. The variable is then used later to define the filename.
Use the code provided as a starting point... don't re-invent the wheel... Also walk through the code to try to discern the logic within it.
-Dustin
Professional Engineer
Pretty good with SolidWorks
RE: Save As PDF Macro with Custom Properties
Matt Lorono
Lorono's SolidWorks Resources & SolidWorks Legion
&
RE: Save As PDF Macro with Custom Properties
-Dustin
Professional Engineer
Pretty good with SolidWorks
RE: Save As PDF Macro with Custom Properties
ShaggyPE: Your code mainly does what I'm looking for but I get an error in the line:
swFrame.SetStatusBarText "Done"
RE: Save As PDF Macro with Custom Properties
-Dustin
Professional Engineer
Pretty good with SolidWorks
RE: Save As PDF Macro with Custom Properties
-Dustin
Professional Engineer
Pretty good with SolidWorks
RE: Save As PDF Macro with Custom Properties
I'm using 2008
RE: Save As PDF Macro with Custom Properties
This is a FAQ I wrote a few years ago that can help get macros up and running when these types of errors happen:
http:/
Matt Lorono
Lorono's SolidWorks Resources & SolidWorks Legion
&
RE: Save As PDF Macro with Custom Properties
That is definitely at least part of the problem.
1. Put the macro file someplace safe
2. Download one of the .bmp files attached in this thread... put it in the same place
3. With a file open in SolidWorks, go to tools> customize> commands> macro> and drag the "new macro button" to where you want the button. I put mine next to the save disk button.
4. Select "Choose Image" and Browse for your .bmp file
5. Browse for your macro file (.swp or .swb)
6. Hit the drop down in method and select the one that has .main at the end.
Now hit the button.
One issue you may have is the save location. The macro is currently hard coded to save in a particular location. There are instructions within the code to change that to the current working directory.
-Dustin
Professional Engineer
Pretty good with SolidWorks
RE: Save As PDF Macro with Custom Properties
Matt Lorono
Lorono's SolidWorks Resources & SolidWorks Legion
&
RE: Save As PDF Macro with Custom Properties
Jterry85,
I just confirmed that the current code above actually saves the pdf in the same folder as the original drawing, there is no need to update the code for that functionality.
-Dustin
Professional Engineer
Pretty good with SolidWorks
RE: Save As PDF Macro with Custom Properties
Thanks all!
Now ONE little thing I'd like to change if possible.... The file name saves as "revx", is it possible to save it as "RevX"??
RE: Save As PDF Macro with Custom Properties
Is it possible to make this change?
RE: Save As PDF Macro with Custom Properties
-Dustin
Professional Engineer
Pretty good with SolidWorks
RE: Save As PDF Macro with Custom Properties
Thank you all for your efforts.
RE: Save As PDF Macro with Custom Properties
-Dustin
Professional Engineer
Pretty good with SolidWorks
RE: Save As PDF Macro with Custom Properties
The Rev will pull from the drawing only. If you are using the part or assembly rev on the drawing, there's no way to get the macro to pick that up by referring to the custom property directly. You'll need to traverse the drawing to find the desired part or assy, then pull the values directly from that selected file.
Matt Lorono
Lorono's SolidWorks Resources & SolidWorks Legion
&