Hi Folks,
I am working on a master properties macro, and I am running into a limitation of the macro with parts/assemblies that were forgotten to be checked out (PDM Pro). If you forget to check out the part before you run the main macro, there are values that are not saved upon closing the macro and you will have to reenter the values after the part/assembly is check out.
So, my next addition to the macro is to add a check out button to this master properties macro. I have been able to check out the file from Solidworks, but I am running into the issue with the window in Solidworks still says "read only" after check out. My thought to get around this, is to use the multi user environment option and toggle the system property and some how then toggle the get write access command. And this is my hang up. I have been able to do everything but get the get write access command to work.
I have tired going down the path with SetReadOnly method, but I am hitting Error 438 and not sure how to fix this. Is this the better route over multi user enviorment?
Here is the Macro I have so far:
Const VAULT_NAME As String = "EBVault"
Dim swApp As SldWorks.SldWorks
Dim swPdmVault As IEdmVault5
Dim check As Boolean
Dim Value As Boolean
'Dim instance As swCommands_e
Sub main()
Set swApp = Application.SldWorks
Dim swModel As SldWorks.ModelDoc2
Set swModel = swApp.ActiveDoc
If Not swModel Is Nothing Then
Set swPdmVault = New EdmVault5
swPdmVault.LoginAuto VAULT_NAME, 0
If swPdmVault.IsLoggedIn Then
CheckOutModel swModel, swPdmVault
Else
MsgBox "Please login to vault"
End If
Else
MsgBox "Please open the model"
End If
End Sub
Sub CheckOutModel(model As SldWorks.ModelDoc2, vault As IEdmVault5)
Dim modelPath As String
modelPath = model.GetPathName()
Dim swPdmFile As IEdmFile5
Set swPdmFile = vault.GetFileFromPath(modelPath)
Set swApp = Application.SldWorks
If Not swPdmFile Is Nothing Then
On Error GoTo catch
Dim res As Boolean
Dim swPdmFolder As IEdmFolder5
Set swPdmFolder = vault.GetFolderFromPath(Left(modelPath, InStrRev(modelPath, "\\")))
try:
model.ForceReleaseLocks
swPdmFile.LockFile swPdmFolder.ID, 0
res = True
GoTo finally
catch:
Debug.Print Err.Number & ": "; Err.Description
res = False
GoTo finally
finally:
model.ReloadOrReplace Not res, modelPath, Not res
Else
Err.Raise vbError, "", "Specified model doesn't exist in the vault"
End If
Dim instance As IModelDoc2
Dim SetReadOnly As Boolean
'SetReadOnly = False
'Value = instance.SetReadOnlyState(SetReadOnly)
swApp.SetUserPreferenceToggle swUserPreferenceToggle_e.swCollabEnableMultiUser, True
swApp.SetUserPreferenceToggle swUserPreferenceToggle_e.swCollabAddShortcutMenuItems, True
'swCommands_Change_Write_Access
'swCommands_Change_Write_Access_Comp
End Sub
Thanks for any help!

Is the macro run by a user? I have had hit and miss success checking files out/in using VBA . IMO it may just be a lot simpler to check if the file is checked out and if not tell the user to check it out and rerun the macro.
Hi David, the user does run the macro on their computer. The check out part is no issue, as this is accomplished via IEdmVault5. The issue is you have to close the document and reopen it to remove the read only state or use the multi user environment get write access feature.
Its the read only I am trying to remove from Solidworks without having to close and reopen since it was successfully checked out.
Caveat: I don't use PDM, so I can't really test anything here. However, it looks to me like your "ReloadOrReplace" line has "False" for the "DiscardChanges" argument, which causes ReloadOrReplace to fail if the document has been modified in any way in the current SW session. Have you tried it with "True" for that argument? Of course, you will have to write your properties after calling ReloadOrReplace.
After realizing what a dummy I was on how I forgot to change instance out for swModel. I also realized that I did not need to do any of the multi user environment and go straight to SetReadOnlyState.
After that, I got it working, and I cleaned it up little bit. So here is the results for checking a file out in PDM Pro while a document is open if anyone has this need. Its mainly useful if you are embedding this into another macro, a properties macro in my case.
Const VAULT_NAME As String = "EBVault"
Dim swApp As SldWorks.SldWorks
Dim swPdmVault As IEdmVault5
Dim check As Boolean
Dim Value As Boolean
Dim swModel As SldWorks.ModelDoc2
Dim SetReadOnly As Boolean
Dim modelPath As String
Dim swPdmFile As IEdmFile5
Sub VaultCheck()
Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc
If Not swModel Is Nothing Then
Set swPdmVault = New EdmVault5
swPdmVault.LoginAuto VAULT_NAME, 0
If swPdmVault.IsLoggedIn Then
CheckOutModel swModel, swPdmVault
Else
MsgBox "Please login to vault"
End If
Else
MsgBox "Please open the model"
End If
End Sub
Sub CheckOutModel(model As SldWorks.ModelDoc2, vault As IEdmVault5)
modelPath = model.GetPathName()
Set swPdmFile = vault.GetFileFromPath(modelPath)
Set swApp = Application.SldWorks
If Not swPdmFile Is Nothing Then
On Error GoTo catch
Dim res As Boolean
Dim swPdmFolder As IEdmFolder5
Set swPdmFolder = vault.GetFolderFromPath(Left(modelPath, InStrRev(modelPath, "\")))
try:
model.ForceReleaseLocks
swPdmFile.LockFile swPdmFolder.ID, 0
res = True
GoTo finally
catch:
Debug.Print Err.Number & ": "; Err.Description
res = False
GoTo finally
finally:
model.ReloadOrReplace Not res, modelPath, Not res
Else
Err.Raise vbError, "", "Specified model doesn't exist in the vault"
End If
Value = swModel.SetReadOnlyState(SetReadOnly)
End Sub
Gald you got it working. Just some things to think about that may come up. Do you have a check for if the file is already open with write access? What if it is a new file that has not been added to the vault yet? Also, it may be good to check swPdmFile.IsLocked is false before trying to lock it and get an error.