How to add an automatic comment during Check-In using IEdmFile8 / UnlockFile2

CheckOut.swp (45,5 KB)

Hello everyone,

I have a VBA macro running inside SolidWorks that handles Check-Out and Check-In operations through the SOLIDWORKS PDM (EPDM) API.

The macro works correctly for both Check-Out and basic Check-In. I would now like to automatically add a comment during the Check-In phase, with the following fixed text (plus the current date and time generated at runtime):

“File insertion / update for CNC Machine Programs creation on dd/mm/yyyy at hh:mm:ss”

To do this, I tried casting IEdmFile5 to IEdmFile8 and calling UnlockFile2, which should accept a comment string as a fifth parameter.

Here is the relevant portion of the modified checkInFile sub:

Private Sub checkInFile(ByVal pdmVault, ByVal docType As Integer)
    Dim swModel   As SldWorks.ModelDoc2
    Dim pdmFile   As IEdmFile5
    Dim pdmFile8  As IEdmFile8
    Dim pdmFolder As IEdmFolder5
    Dim ret       As Integer
    Dim lFlags    As Long
    Dim sComment  As String

    Set swModel = swApp.ActiveDoc
    Set pdmFile = pdmVault.GetFileFromPath(swModel.GetPathName, pdmFolder)

    If Not pdmFile.IsLocked Then
        MsgBox "The file is not checked out.", vbInformation
        Exit Sub
    End If

    sComment = "Inserimento / Aggiornamento file per creazione " & _
               "Programmi Macchina utensile in data " & _
               Format(Now(), "dd/mm/yyyy") & " ore " & Format(Now(), "hh:mm:ss")

    If Me.chkMantienChkOut.Value = True Then
        lFlags = EdmUnlock_OverwriteLatestVersion Or EdmUnlock_KeepLocked
    Else
        lFlags = EdmUnlock_OverwriteLatestVersion
    End If

    If swModel.ForceReleaseLocks Then
        Set pdmFile8 = pdmFile
        pdmFile8.UnlockFile2 pdmFolder.ID, handle, lFlags, sComment
    End If

    If docType = swDocDRAWING Then
        ret = swApp.CloseAndReopen(swModel, swCloseReopenOption_DiscardChanges, swModel)
    Else
        ret = swModel.ReloadOrReplace(False, swModel.GetPathName, True)
    End If

    If Me.chkMantienChkOut.Value = True Then
        MsgBox "Check-In completed. File kept checked out.", vbInformation
    Else
        MsgBox "Check-In completed.", vbInformation
    End If
End Sub

My questions are:

  1. Is using IEdmFile8 / UnlockFile2 the correct approach to pass a comment to the PDM vault on Check-In?
  2. Do I need to add or update any Type Library reference in the VBA project for IEdmFile8 to be available?
  3. Is there a simpler or more compatible alternative method?

Any help is greatly appreciated. Thank you!

I never heard of UnlockFile2, but for individual files I always use IEdmFile5 (or what ever) => UnlockFile. Have you tried that?

Private Sub checkInFile(ByVal pdmVault As IEdmVault5, ByVal docType As Integer)
Const EdmUnlock_KeepLocked As Long = 1
Const EdmUnlock_IgnoreRefsOutsideVault As Long = 8
Const EdmUnlock_IgnoreRefsNotLockedByCaller As Long = 32
Const EdmUnlock_OverwriteLatestVersion As Long = 64
Const EdmUnlock_IgnoreReferences As Long = 128

Dim swModel As SldWorks.ModelDoc2
Dim pdmFile As IEdmFile5
Dim pdmFolder As IEdmFolder5
Dim sPath As String
Dim lSaveErr As Long, lSaveWarn As Long
Dim localPath As String
Dim flags As Long
Dim sComment As String

' === Documento attivo ===
Set swModel = swApp.ActiveDoc
If swModel Is Nothing Then Exit Sub

sPath = swModel.GetPathName
If Len(sPath) = 0 Then Exit Sub

' === Oggetti PDM ===
Set pdmFile = pdmVault.GetFileFromPath(sPath, pdmFolder)
If pdmFile Is Nothing Then Exit Sub
If Not pdmFile.IsLocked Then Exit Sub

' === Assicura copia locale ===
localPath = pdmFile.GetLocalPath(pdmFolder.ID)
If Len(localPath) = 0 Or Len(Dir$(localPath)) = 0 Then
    pdmFile.GetFileCopy 0, Empty, pdmFolder.ID, 0, vbNullString
End If

' === Commento check-in (come tua versione originale) ===
sComment = "File insertion / update for CNC Machine Programs creation on " & _
           Format(Now(), "dd/mm/yyyy") & " at " & Format(Now(), "hh:mm:ss")

' === Salva silenzioso in SW ===
swModel.Save3 swSaveAsOptions_Silent, lSaveErr, lSaveWarn
If lSaveErr <> 0 Then Exit Sub

' === Rilascia lock SW ===
swModel.ForceReleaseLocks

' === Prepara i flag ===
flags = EdmUnlock_IgnoreReferences Or _
        EdmUnlock_IgnoreRefsOutsideVault Or _
        EdmUnlock_IgnoreRefsNotLockedByCaller Or _
        EdmUnlock_OverwriteLatestVersion   ' <-- Sovrascrive versione precedente

' Aggiunge Mantieni il Check-Out se selezionato
If Me.chkMantienChkOut.Value = True Then
    flags = flags Or EdmUnlock_KeepLocked
End If

' === Check-in con commento ===
pdmFile.UnlockFile 0, sComment, flags, Nothing

' === Reload del documento ===
If docType = swDocDRAWING Then
    swApp.CloseAndReopen swModel, swCloseReopenOption_DiscardChanges, swModel
Else
    swModel.ReloadOrReplace False, sPath, True
End If

' === Messaggio finale ===
If Me.chkMantienChkOut.Value Then
    MsgBox "Check-in completato (file rimasto in check-out).", vbInformation
Else
    MsgBox "Check-in completato.", vbInformation
End If

End Sub