Problem saving SW files inside vault using SW macro

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?

1 Like

Edit: I realize now that this may not exactly be answering the initial question. More information about the specific error would be helpful.

Okay, I’ve run into something similar a long time ago.

You’re saying that saving with the option to open the saved document swSaveAsOptions_CopyAndOpen option that it causes an error. This is somewhat expected since it’s similar to trying to check in a file using windows explorer while you have that file open currently in solidworks. It should pop up an error saying that the file is currently in use.

image

Now, if you’re saving a file that’s already in PDM to a location also in PDM then it’s likely your vault is adding the file to the vault automatically. If that’s not the case then you will have to call AddFile as others have suggested. Otherwise, the below method should work for you.

VBA Class Module (I named it SwAppWithEventsHandler)

Dim WithEvents swApp As SldWorks.SldWorks

Private Sub Class_Initialize()
    Set swApp = Application.SldWorks
End Sub

Private Function swApp_FileCloseNotify(ByVal FileName As String, ByVal reason As Long) As Long
    If reason = swFileCloseNotifyReason_e.swFileCloseNotifyReason_CloseForReload Then
        CheckInFile (FileName)  'Checks file into PDM while it's closed in SW
    End If
End Function


Public Function SaveSWModelAs(path As String) As Boolean
    Dim swModel As ModelDoc2
    Dim swModDocExt As ModelDocExtension
    
    Set swModel = swApp.ActiveDoc
    Set swModDocExt = swModel.Extension
    
    Dim errors As Long
    Dim warnings As Long
    SaveSWModelAs = swModDocExt.SaveAs3(path, swSaveAsVersion_e.swSaveAsCurrentVersion, swSaveAsOptions_e.swSaveAsOptions_CopyAndOpen, Nothing, Nothing, errors, warnings)
End Function

Public Function CloseAndReopenDrawing() As Boolean
    Dim reOpenedModel As ModelDoc2
    Dim closeAndReopenError As Integer
    closeAndReopenError = swApp.CloseAndReopen2(swApp.ActiveDoc, swCloseReopenOption_e.swCloseReopenOption_DiscardChanges, reOpenedModel)
End Function

VBA Module - Change the filePath variable to your full path to save your new file as with slddrw extension.

Option Explicit
Dim pdm_Vault As New EdmVault5
Dim filePath As String
Dim swAppWithEvents As swAppWithEventsHandler

Sub main()
    
    Set swAppWithEvents = New swAppWithEventsHandler
    
    If Not pdm_Vault.IsLoggedIn Then
        pdm_Vault.LoginAuto "Sandbox", 0
    End If
    
    filePath = "C:\SandboxVault\Folder\TestPDMFile.slddrw"
    
    swAppWithEvents.SaveSWModelAs filePath
    
    swAppWithEvents.CloseAndReopenDrawing
        
End Sub

Function CheckInFile(FileName As String)
    Dim pdm_Folder As IEdmFolder13
    Dim pdm_File As IEdmFile10
    
    Set pdm_File = pdm_Vault.GetFileFromPath(filePath, pdm_Folder)
    
    Debug.Assert Not pdm_File Is Nothing
    Debug.Print pdm_File.Name
    
    pdm_File.UnlockFile 0, "Test Check In using CloseAndReopen"
    
End Function

Now this is the preferred method for drawings. It will save as and open the new file in PDM within SW. Then it will close it very shortly while it checks it into PDM and then reopen right after.

If you’re doing the same for parts or assemblies then refer to the Remarks for the IModelDoc2::ReloadOrReplace Method.

Have you tried saving the file outside PDM and then adding it using IEdmFolder5::AddFile

I’ve tried saving it outside and moving it inside the vault using FileSystemObject.MoveFile. Monday I will try with AddFile, but still it will not allow me to use SaveAs3 with the CopyAndOpen option since it would be opened in SW and then unmoveable.

MoveFile is for files within the vault file system I think. Use AddFile

I too encountered a similar problem. I am using VB.net to generate a PDF from the active drawing and save it to the local vault folder. However, when attempting to use Addfile, the file consistently returns as nothing. The file doesn’t appear until I manually refresh the vault. Even RefreshFolder isn’t effective. Are there any other options or guidance available for checking in a PDF into the desired vault folder? Thank you for any assistance.

I can confirm, I have already tried RefreshFolder but without effects.

Ok, I’ve made several attempts using AddFile as Amen suggested. Right now I save the file as temporary in a local folder then use AddFile to copy it to the vault. It all seems to work, but I can’t use the swSaveAsOptions_CopyAndOpen option since it would open the temporary one and not the definitive. I’m not completely happy of this solution since it’ s a workaround with a lot of unwanted passage, but it’s still something.

If you have idea on how to simplify it it would be awesome!

Hi BigCrazyAI
Thankyou for your answer. If I understand correctly you use your code to check in drawing, am I right? I have a different need. What I’m trying to do is save a file with the swSaveAsOptions_CopyAndOpen option, which save it and automatically open and replace it in every opened assembly, simultaneously closing the original file, as if I had saved it using the dialog box and choosed the option you see below.
immagine
But since I need to save the file on a local folder and then add it to the vault using AddFile, using that option will open and replace the local temporary file instead of the added one.

EDIT: I’ve take a look to IModelDoc2.ReloadOrReplace Method and IModelDoc2.ForceReleaseLocks Method but i can’t understand well what they do… Can you give me some clarification?

Ciao @OldHarryMTX :wink:
I don’t know if I understand the problem correctly, but If your macro will always use swSaveAsOptions_CopyAndOpen, then I would try the SaveAs method. I know it is obsolete, but I found it quite reliable to save the open component as a new file and replace all its instances in the current SolidWorks session.

I hope it helps.