Advanced Save as PDF help

Programming and macros
lazarus990
Posts: 2
Joined: Mon Jul 26, 2021 9:15 am
Answers: 0

Advanced Save as PDF help

Unread post by lazarus990 »

Years ago, I used a macro that would save an open file as a pdf in another parent directory. This basically replaced my Save button with what is essentially "Save file, and if it's a drawing, save as PDF as well". I'd love to recreate it for my new environment, but I'm quite new to coding and I'm completely in over my head. Here's my understanding of how the macro used to work:

Is active file .sldprt or .sldasm? Yes->run 'Save', No->run 'Save' and 'Save as PDF macro'

'Save as PDF macro'

Take current file location (ex: X:\Design\Test\5884.SLDDRW). Test if new location exists (ex: X:\Production\PDF\Test\5884.SLDDRW). If No->Create Folder, If Yes->Use new folder location. (This would be nice, but not necessary)

Run 'Save as PDF', Add "-REV" to PDF name (where "REV" = Revision Level from Drawing Custom Property), all pages saved into one document, overwrite if existing. It would be nice to have previous revisions moved into an Archive folder stored within the directory (ex: X:\Production\PDF\Test\ARCHIVE\5884-A.pdf). This archive would need to be created if not existing already.

I'd love to learn how to do this, but the sample code I've found on the SolidWorks forums isn't intuitive to me. If there's any way the code could be notated to describe what is going on so that I can make changes in the future, I would be eternally grateful. Thanks.

Below is the code I'm working with right now. It works, but it saves all file types, and it puts them in the same location.

Code: Select all

Option Explicit

Dim swApp As Object

Dim Part As Object

Dim FilePath As String

Dim PathSize As Long

Dim PathNoExtention As String

Dim NewFilePath As String

Dim Rev As String

Sub main()

Set swApp = Application.SldWorks

Set Part = swApp.ActiveDoc

FilePath = Part.GetPathName

PathSize = Strings.Len(FilePath)

Rev = Part.CustomInfo("Revision") ' Change Custom Property Here

PathNoExtention = Strings.Left(FilePath, PathSize - 7)

NewFilePath = PathNoExtention & "-" & Rev

Part.SaveAs2 NewFilePath & ".PDF", 0, True, False

End Sub
User avatar
zwei
Posts: 701
Joined: Mon Mar 15, 2021 9:17 pm
Answers: 18
Location: Malaysia
x 185
x 600

Re: Advanced Save as PDF help

Unread post by zwei »

It works, but it saves all file types, and it puts them in the same location.
I only took a quick scan of your code ...

1. You should check for the folder and create it if it is not available...
You will need to enable the filesystemobject library (sorry i cant remember the exact name) to do this
Example code:

Code: Select all

If FSO.FolderExists(PDFPath) = False Then                               'Create folder with PathName if not exist in Save Path
            MkDir PDFPath
End If

2. You should check the file type before you save (using and IF statement)
Example code:

Code: Select all

'Do XXXXX if file is drawing
If Not swModel.GetType() = swDocDRAWING Then
	'Do something
        
End If

3. You will need to remove the original path from the file name so that you can concatenate the new folder path to save it to the required folder
Example code:

Code: Select all

			
SaveFileName = swModel.GetPathName									'Get file path
SaveFileName = Mid(SaveFileName, InStrRev(SaveFileName, "\") + 1)			'Remove file path
SaveFileName = Left(SaveFileName, InStrRev(SaveFileName, ".") - 1)			'Remove file extension
SaveFileName = PDFPath & SaveFileName & "-" & Rev & ".pdf"					'Form new file path
swModel.Extension.SaveAs SaveFileName, 0, 0, Nothing, 0, 0


JFYI, it is always better to put code in Quote or Code block as it allow easier viewing
image.png

PS: The example code is taken from my code "template/library" so it is not optimized for your code.
Sorry but i had to drop off real quick (it was almost midnight here :P), will take a look if needed tomorrow.
Far too many items in the world are designed, constructed and foisted upon us with no understanding-or even care-for how we will use them.
lazarus990
Posts: 2
Joined: Mon Jul 26, 2021 9:15 am
Answers: 0

Re: Advanced Save as PDF help

Unread post by lazarus990 »

Thank you Zhen-Wei Tee. I will need to study this to try and understand what exactly is going on. Thanks for the tip on how to show as code as well. I've edited my post above in hoes that it is easier to read for others.
Post Reply