fcsuper
Mechanical
- Apr 20, 2006
- 2,204
From several posts on this board and comp.cad.solidworks message boards, I've put together the following example of a macro that will save the current solidworks doc (part/assy/dwg) as a PDF. Since I recently updated it, I figured I might as well share (since I got it from here anyway). I've left remarks in to offer alternatives for several functions. Changes from previous posted versions of this macro include nondrawing support and more error handling. Of course, suggestions for improvement are always welcome. 
Dim SwApp As SldWorks.SldWorks
Dim Model As SldWorks.ModelDoc2
Dim MyPath, ModName, NewName As String
Dim fso As Object
Dim MB As Boolean
Dim Errs As Long
Dim Warnings As Long
Sub main()
Set SwApp = Application.SldWorks
Set Model = SwApp.ActiveDoc
' Error handler for no document loaded
If Model Is Nothing Then MsgBox "No drawing 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
MyPath = "C:\PDF"
' Option 3: Use the document's folder
' MyPath = Left(Model.GetPathName, InStrRev(Model.GetPathName, "\") - 1)
' Determine if directory exists
Set fso = CreateObject("Scripting.FileSystemObject")
If (Not fso.FolderExists(MyPath)) Then MsgBox (MyPath + " does not exist!"), vbCritical: End
' Call correct sub
If Model.GetType <> 3 Then Call notdrawing
Call ifdrawing
End Sub
Sub notdrawing()
ModName = Left(Model.GetTitle, InStrRev(Model.GetTitle, ".") - 1)
Call alldoc
End Sub
Sub ifdrawing()
' Status
ModName = Left(Model.GetTitle, InStrRev(Model.GetTitle, " Sheet") - 3)
Call alldoc
End Sub
Sub alldoc()
NewName = ModName & ".pdf"
MsgBox "Save " & NewName & " to" & Chr(13) & MyPath & Chr(13) & Chr(13) & "(No notification will occur " & Chr(13) & "for success PDF creation.)"
' PDF Creation
MB = Model.SaveAs4(MyPath & "\" & NewName, swSaveAsCurrentVersion, swSaveAsOptions_Silent, Errs, Warnings)
' Warnings to user on Error
If Warnings <> 0 Then
MsgBox "There were warnings. PDF creation may have failed. Verify" & Chr(13) & "results and check possible causes.", vbExclamation
Else
End If
If MB = False Then
MsgBox "PDF creation has failed! Check save location, available" & Chr(13) & "disk space or other possible causes.", vbCritical
Else
End If
Call last
End Sub
Sub last()
' Clear immediate values
Set Model = Nothing
Set MyPath = Nothing
End
End Sub
Please note that this code assumes Save as PDF function is available. In S/W versions of 2005 or under, you will need to goto Add-In's to active the "Save As PDF" Add-in. 2006 and above do not have this issue.
Dim SwApp As SldWorks.SldWorks
Dim Model As SldWorks.ModelDoc2
Dim MyPath, ModName, NewName As String
Dim fso As Object
Dim MB As Boolean
Dim Errs As Long
Dim Warnings As Long
Sub main()
Set SwApp = Application.SldWorks
Set Model = SwApp.ActiveDoc
' Error handler for no document loaded
If Model Is Nothing Then MsgBox "No drawing 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
MyPath = "C:\PDF"
' Option 3: Use the document's folder
' MyPath = Left(Model.GetPathName, InStrRev(Model.GetPathName, "\") - 1)
' Determine if directory exists
Set fso = CreateObject("Scripting.FileSystemObject")
If (Not fso.FolderExists(MyPath)) Then MsgBox (MyPath + " does not exist!"), vbCritical: End
' Call correct sub
If Model.GetType <> 3 Then Call notdrawing
Call ifdrawing
End Sub
Sub notdrawing()
ModName = Left(Model.GetTitle, InStrRev(Model.GetTitle, ".") - 1)
Call alldoc
End Sub
Sub ifdrawing()
' Status
ModName = Left(Model.GetTitle, InStrRev(Model.GetTitle, " Sheet") - 3)
Call alldoc
End Sub
Sub alldoc()
NewName = ModName & ".pdf"
MsgBox "Save " & NewName & " to" & Chr(13) & MyPath & Chr(13) & Chr(13) & "(No notification will occur " & Chr(13) & "for success PDF creation.)"
' PDF Creation
MB = Model.SaveAs4(MyPath & "\" & NewName, swSaveAsCurrentVersion, swSaveAsOptions_Silent, Errs, Warnings)
' Warnings to user on Error
If Warnings <> 0 Then
MsgBox "There were warnings. PDF creation may have failed. Verify" & Chr(13) & "results and check possible causes.", vbExclamation
Else
End If
If MB = False Then
MsgBox "PDF creation has failed! Check save location, available" & Chr(13) & "disk space or other possible causes.", vbCritical
Else
End If
Call last
End Sub
Sub last()
' Clear immediate values
Set Model = Nothing
Set MyPath = Nothing
End
End Sub
Please note that this code assumes Save as PDF function is available. In S/W versions of 2005 or under, you will need to goto Add-In's to active the "Save As PDF" Add-in. 2006 and above do not have this issue.