Files
Code
Module: mMain
'Utility for saving a document with a new extension in a current or parent _
directory, or saving a document with a new extension in another directory _
that exists within the current or parent directory.
'Preconditions:
'1. Appropriate constant values are specified below.
'2. Reference to "Microsoft Scripting Runtime" is added.
'Written by Keith Rice
'CADSharp LLC
'www.cadsharp.com
'Specifies the new file extension. Do not include period.
Const NEW_EXTENSION As String = "PDF"
'Used in main only. Specifies the folder to find in the current or parent _
directory. If more than one folder with this name exists in the current _
or parent directory, the one closest to the root directory is used.
Const FOLDER_NAME As String = "04 PDFs"
'Used in main2 only. Specifies the number of directories above the current _
one to save the file to. E.g., 0 will save it to the current directory. _
1 to the directory immediately above the current directory, and so on.
Const DIR_ABOVE As Integer = 1
Sub main()
If FOLDER_NAME = Empty Then Exit Sub
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc
' Create new file name
Dim strModelPath As String
Dim strSavePath As String
Dim strSaveFolder As String
Dim strFileName As String
Dim vSplit As Variant
strModelPath = swModel.GetPathName
If swModel.GetPathName = Empty Then
swApp.SendMsgToUser "Please save model."
Exit Sub
End If
strFileName = Right(strModelPath, _
Len(strModelPath) - InStrRev(strModelPath, "\"))
vSplit = Split(strFileName, ".")
strFileName = vSplit(0) & "." & NEW_EXTENSION
' Create new directory
vSplit = Split(strModelPath, "\")
Dim intDirAbove As Integer
Dim i As Integer
Dim bFound As Boolean
bFound = False
For i = 0 To UBound(vSplit) - intDirAbove
strSaveFolder = strSaveFolder & vSplit(i) & "\"
If Dir(strSaveFolder & FOLDER_NAME, vbDirectory) <> Empty Then
bFound = True
Exit For
End If
Next i
If bFound = False Then
swApp.SendMsgToUser "Failed to find correct folder."
Exit Sub
End If
strSavePath = strSaveFolder & FOLDER_NAME & "\" & strFileName
Debug.Print "Original path: " & strModelPath
Debug.Print "New save path: " & strSavePath
' Save
bRet = swModel.Extension.SaveAs(strSavePath, swSaveAsCurrentVersion, _
swSaveAsOptions_Silent, Nothing, Empty, Empty)
If bRet = False Then swApp.SendMsgToUser "Problems saving file."
End Sub
Sub main2()
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc
' Create new file name
Dim strModelPath As String
Dim strSavePath As String
Dim strFileName As String
Dim vSplit As Variant
strModelPath = swModel.GetPathName
strFileName = Right(strModelPath, Len(strModelPath) - InStrRev(strModelPath, "\"))
vSplit = Split(strFileName, ".")
strFileName = vSplit(0) & "." & NEW_EXTENSION
' Create new directory
vSplit = Split(strModelPath, "\")
Dim i As Integer
For i = 0 To UBound(vSplit) - DIR_ABOVE - 1
strSavePath = strSavePath & vSplit(i) & "\"
Next i
strSavePath = strSavePath & strFileName
Debug.Print "Original path: " & strModelPath
Debug.Print "New save path: " & strSavePath
' Save
bRet = swModel.Extension.SaveAs(strSavePath, swSaveAsCurrentVersion, _
swSaveAsOptions_Silent, Nothing, Empty, Empty)
If bRet = False Then swApp.SendMsgToUser "Problems saving file."
End Sub