Save PDF to PDM

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 ? :slight_smile:


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

Try these codes.

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

Love it Gupta. Thank you so much :smiley:

As another option, here is how I parse the filename and filepath.

'this is the full name and path
sModelPath = swModel.GetPathName

'get filename from full path
CropPos = InStrRev(sModelPath, "\")
sNewName = Right(sModelPath, (Len(sModelPath) - CropPos))

'strip sldprt extension
sNewName = Left(sNewName, Len(sNewName) - 7)

'get path
sNewPath = Left(sModelPath, CropPos - 1)

Would it be easy to do the same with STEP file?

Yes.

Change

sFileName = PDMPath + sFolderName & "\" + sFileName + ".PDF"

to

sFileName = PDMPath + sFolderName & "\" + sFileName + ".STEP"

and change

swModel.Extension.SaveAs3 sFileName, swSaveAsCurrentVersion, swSaveAsOptions_Silent, swExportPDFData, Nothing, nErrors, nWarnings

to

swModel.Extension.SaveAs3 sFileName, swSaveAsCurrentVersion, swSaveAsOptions_Silent, Nothing, Nothing, nErrors, nWarnings

Just an idea…you could also do a check-in for the exported files…PDM has quite nice support for VBA

I’m trying to check-in an exported PDF file (which is saved locally into the vault) but it is not working. Can you please guide? Thank you.

Here are the codes I’m trying to use

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,.

https://www.youtube.com/watch?v=GAnPb9u9_nc

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.

Yuo have to add it to the vault, otherwise it stays as local file

https://help.solidworks.com/2021/english/api/epdmapi/EPDM.Interop.epdm~EPDM.Interop.epdm.IEdmFolder8~AddFile2.html

or

https://help.solidworks.com/2021/english/api/epdmapi/epdm.interop.epdm~epdm.interop.epdm.iedmbatchadd~addfilefrompathtopath.html

Thank you I’ll check these out.

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.

Any help:
https://help.solidworks.com/2023/English/api/epdmapi/Add_Files_to_Vault_Example_VBNET.htm

check also this:
https://www.cati.com/blog/automatically-adding-files-to-enterprise-pdm/

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!

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 https://www.cadforum.net/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.