Hi all
I’m having problems creating a vba macro to assign code and save new models created with SW. The macro uses the SaveAs3 method and allows you to select various save options: Save as, Save as copy and continue and Save as copy and open.
The problems are as follows:
Once saved, the PDM datacard does not open automatically. This is a negligible problem except that I believe it is connected to the following more serious ones.
When the user fills in the datacard of a file saved in this way and then check it in, he loses all the data saved in the datacard. Checking out the file, the lost data often (but not always!) returns (they are still somewhere in local?), and checking it in again keeps the datacard correctly filled out.
Rarely, this doesn't happen when you choose the Save as Copy and continue option, as if not opening the file as soon as it's saved will avoid these problems.
I have made several tests. For example, saving the file with the Save as copy and open option but “locking” the macro with a MsgBox between the SaveAs3 and the OpenDoc methods to wait for the file to be added to the vault does not resolve the problem.
Below a cleaned version of my code that i’m using for tests:
Option Explicit
Const newModelName As String = "C:\VaultName\FolderName\ModelName.SLDPRT"
Const tipo As Long = swDocPART 'swDocASSEMBLY
'This option changes depending on which test I want to run.
Const opzioni As Long = swSaveAsOptions_Copy
'Const opzioni As Long = swSaveAsOptions_CopyAndOpen
Sub main()
UserForm1.Show
End Sub
Sub SalvaNuovoCodice()
Dim swApp As SldWorks.SldWorks
Dim swModel As ModelDoc2
Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc
If swModel.SaveAs3(newModelName, swSaveAsCurrentVersion, opzioni) = 0 Then
' 'This is the message box to lock the macro between the SaveAs and the OpenDoc methods.
' MsgBox "Wait."
' 'Open the file if the "Save as copy and open" option is selected.
' Call swApp.OpenDoc(newModelName, tipo)
Unload UserForm1
Else
MsgBox "Il salvataggio non è andato a buon fine."
End If
End Sub
i have also tried to check if the file is Locked, but it did not lead me to any solution.
Sub CheckFile()
Dim swPDMVault As IEdmVault5 'EdmVault5
Dim swPDMFile As IEdmFile5 'Object
Set swPDMVault = New EdmVault5
swPDMVault.LoginAuto "VaultName", 0
If Not swPDMVault Is Nothing Then
If swPDMVault.IsLoggedIn Then
Set swPDMFile = swPDMVault.GetFileFromPath(newModelName)
If Not swPDMFile Is Nothing Then
If swPDMFile.IsLocked Then
MsgBox swPDMFile.LockedByUser.name
' swPDMFile.UnlockFile 0, ""
Else
MsgBox "Unlocked"
End If
Else
MsgBox "File non trovato."
End If
Else
MsgBox "Login non riuscito."
End If
Else
MsgBox "Vault non trovato."
End If
End Sub
Can you help me?