Hi
I am realy bad at macro since i dont know VB.
I have a code that save PDF into folder in PDM (if folder is created)
I want to tweak this code a bit. To get the 8 first letters\numbers of the file name it looks into.
If fil name is named AB123456_01.assy i want to get 8 first didigts AB123456 as the folder name to look for, and the PDF name to be AB123455_01
The code we use is like this
Annyone who can edit the code for me ? :-)
Dim swApp As Object
Dim Part As Object
Dim boolstatus As Boolean
Dim longstatus As Long, longwarnings As Long
Sub main()
Set swApp = Application.SldWorks
Set Part = swApp.ActiveDoc
Dim swModel As SldWorks.ModelDoc2
Dim sFileName As String
Set swModel = swApp.ActiveDoc
' Zoom To Fit
Part.ViewZoomtofit2
' Save As
sFileName = Mid(swModel.GetPathName, InStrRev(swModel.GetPathName, "\") + 1)
sFileName = Left(sFileName, InStrRev(sFileName, ".") - 1)
'sFileName = swModel.GetTitle
Debug.Print sFileName
longstatus = Part.SaveAs3("C:\Nordic Steel PDM\Varenummer\" + sFileName & "\" + sFileName + ".pdf", 0, 0)
End Sub
Option Explicit
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swExportPDFData As SldWorks.ExportPdfData
Dim sFolderName As String
Dim sFileName As String
Dim nErrors As Long
Dim nWarnings As Long
Const PDMPath As String = "C:\Nordic Steel PDM\Varenummer\"
Sub Main()
Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc
If swModel Is Nothing Then
MsgBox "No active document.", vbExclamation
Exit Sub
End If
sFolderName = Mid(swModel.GetPathName, InStrRev(swModel.GetPathName, "\") + 1)
sFolderName = Left(sFolderName, InStrRev(sFolderName, ".") - 1)
sFolderName = Left(sFolderName, 8)
sFileName = Mid(swModel.GetPathName, InStrRev(swModel.GetPathName, "\") + 1)
sFileName = Left(sFileName, InStrRev(sFileName, ".") - 1)
sFileName = PDMPath + sFolderName & "\" + sFileName + ".PDF"
swModel.ViewZoomtofit2
Set swExportPDFData = swApp.GetExportFileData(1)
swExportPDFData.SetSheets swExportData_ExportAllSheets, ""
swExportPDFData.ViewPdfAfterSaving = False
swModel.Extension.SaveAs3 sFileName, swSaveAsCurrentVersion, swSaveAsOptions_Silent, swExportPDFData, Nothing, nErrors, nWarnings
End Sub
Re: Save PDF to PDM
Posted: Wed Jan 17, 2024 9:05 am
by arild80
Love it Gupta. Thank you so much :-D
Re: Save PDF to PDM
Posted: Thu Jan 18, 2024 4:10 pm
by SPerman
As another option, here is how I parse the filename and filepath.
Dim bRetval As Boolean
bRetval = model.Extension.SaveAs(OutputfileName, 0, 0, swExportPDFData, 0, 0)
Dim vault As New EdmVault5
vault.LoginAuto("VaultName", 0)
If vault.IsLoggedIn Then
vault.RefreshFolder(outputfolderPath)
Dim afile As IEdmFile5
afile = vault.GetFileFromPath(OutputfileName)
afile.UnlockFile(0, "", 0)
End If
The OutputfileName is the PDF file full name inside the vault folder. Also the PDF does not show up unless vault is refreshed manually. Using RefreshFolder doesn't work either. The aFile is always nothing,.
Re: Save PDF to PDM
Posted: Thu Feb 22, 2024 12:11 pm
by sloworks
Re: Save PDF to PDM
Posted: Thu Feb 22, 2024 2:10 pm
by gupta9665
Thank you for sharing the video, I have already checked and in the video the codes works OK because file is already there in the vault and visible. In my case, it show up only after refreshing manually. And I believe this is the reason that file is always nothing.
Re: Save PDF to PDM
Posted: Thu Feb 22, 2024 2:55 pm
by sloworks
Yuo have to add it to the vault, otherwise it stays as local file
When we save a PDF into a PDM folder it automatically adds to the vault based on our settings in PDM for adding file types. We only have PDM Standard so can't do PDM macros, but our save as PDF SolidWorks macro, puts the PDF in the right folder. The setting adds it. We then just have to check it in.
Re: Save PDF to PDM
Posted: Tue Feb 27, 2024 12:57 am
by gupta9665
CarrieIves wrote: ↑Mon Feb 26, 2024 2:52 pm
When we save a PDF into a PDM folder it automatically adds to the vault based on our settings in PDM for adding file types. We only have PDM Standard so can't do PDM macros, but our save as PDF SolidWorks macro, puts the PDF in the right folder. The setting adds it. We then just have to check it in.
Even for PDM professional, the add-in is putting the file in right location but PDF does not show until vault folder is refreshed. And adding the file to vault via API is not working.
Thanks for all the insights shared here! I've been following the discussion closely, and I'm trying to implement a similar solution for saving PDFs and STEP files to the PDM.
Thanks to Gupta's code and suggestions, I've managed to adapt it to my needs, especially with the folder naming based on the first 8 characters of the filename.
However, I'm running into a small issue when trying to check-in the exported files into the PDM. Like sloworks mentioned, I'm struggling with making the files visible in the vault without having to manually refresh. Even after using the vault.RefreshFolder method, the files still don’t show up until I manually refresh the folder, and the file object (aFile) is always returning as nothing.
Has anyone experienced this issue before or found a workaround? I saw the links provided by sloworks, and I'm about to try those methods, but any additional tips would be greatly appreciated!
Thanks in advance for your help!
Re: Save PDF to PDM
Posted: Mon Aug 19, 2024 10:51 am
by bnemec
Welcome to CADForum.net @hugozanga2024
If you haven't already, please stop by the forum rules section to read through them, matt is our admin and as you can see in the "couple of rules added" matt is human.
Maybe stop by the welcome forum viewforum.php?f=28 and say hi, introduce yourself a just a little.
Onto my point. The thread you responded to is a few months cold and more importantly is checked as solved. It's probably best to start a new thread for your question. To help us out link back to the threads that you have searched and found helpful then maybe call out the exception of why that didn't quite solve your problem or make clear.