If the macro tries to check-out (IEdmFile5 :: LockFile) the document is opened in the Solidworks I get an error message that tells me the current file is in use by another application and does not check it out.
The lock and unlock file methods only works for non opened documents? If yes, is there another method to get it?
Here’s a code that it’s working for testing purposes:
Option Explicit
Dim swApp As SldWorks.SldWorks
Dim pdmVault As EdmVault5
Dim handle As Long
Const vaultName As String = "Your vault name"
Sub main()
Dim swFrame As SldWorks.Frame
Dim swModel As SldWorks.ModelDoc2
Set swApp = Application.SldWorks
Set swFrame = swApp.Frame
handle = swFrame.GetHWnd
Set pdmVault = New EdmVault5
pdmVault.LoginAuto vaultName, handle
Set swModel = swApp.ActiveDoc
Call checkOutFile(pdmVault, swModel.GetType)
Call checkInFile(pdmVault, swModel.GetType)
End Sub
Private Sub checkOutFile(ByVal pdmVault, ByVal docType As Integer)
Dim swModel As SldWorks.ModelDoc2
Dim pdmFile As IEdmFile5
Dim pdmFolder As IEdmFolder5
Dim ret As Integer
Set swModel = swApp.ActiveDoc
Set pdmFile = pdmVault.GetFileFromPath(swModel.GetPathName, pdmFolder)
If pdmFile.IsLocked Then Exit Sub
If swModel.ForceReleaseLocks Then
pdmFile.LockFile pdmFolder.ID, handle
End If
If docType = swDocDRAWING Then
ret = swApp.CloseAndReopen(swModel, swCloseReopenOption_DiscardChanges, swModel)
Else
ret = swModel.ReloadOrReplace(False, swModel.GetPathName, True)
End If
End Sub
Private Sub checkInFile(ByVal pdmVault, ByVal docType As Integer)
Dim swModel As SldWorks.ModelDoc2
Dim pdmFile As IEdmFile5
Dim pdmFolder As IEdmFolder5
Dim ret As Integer
Set swModel = swApp.ActiveDoc
Set pdmFile = pdmVault.GetFileFromPath(swModel.GetPathName, pdmFolder)
If Not pdmFile.IsLocked Then Exit Sub
If swModel.ForceReleaseLocks Then
pdmFile.UnlockFile pdmFolder.ID, handle, EdmUnlock_OverwriteLatestVersion
End If
If docType = swDocDRAWING Then
ret = swApp.CloseAndReopen(swModel, swCloseReopenOption_DiscardChanges, swModel)
Else
ret = swModel.ReloadOrReplace(False, swModel.GetPathName, True)
End If
End Sub