Replace Published Document with the updated version of Working document within M-Files -

Hi there,

We are keeping two versions of our policy documents:
1. Policy Document (DOCX format) – The first one is working document and this document is continuously get updated, with access restricted to specific users who make modifications.
2. Policy Document (PDF format) – This is a published document, and this is a read-only version available to all users.

Both documents have different Object ID, belonging to the same class but having different workflow states. The goal is to ensure that whenever a designated user updates the Working Policy Document, they modify the workflow state to "Republished" after completing the changes. This action should trigger the replacement of the Published Policy Document with the updated version, ensuring only the most recent document is available.

Currently, I am using the Object ID to identify the Published Policy Document that needs replacement. However, my VBScript is not allowing me to overwrite the published file with the updated working document. Could someone provide assistance or share insights on how to achieve this properly? Thanks!

ERROR:

CoActiveScriptSite.cpp, 914, Not found. (0x8004000B)
CoActiveScriptSite.cpp, 744, Not found. (0x8004000B)
Republished, StateAction, 35, Not found. (0x8004000B)
MNullPropagatingDispatchImpl.h, 127, Not found. (0x8004000B)
MNullPropagatingDispatchImpl.h, 174, Not found. (0x8004000B)
CoVaultObjectFileOperations.cpp, 878, Not found. (0x8004000B)
ServerVaultObjectFileOperationsHelper.cpp, 515, Not found. (0x8004000B)
ServerVaultObjectFileOperationsHelper.cpp, 1109, Not found. (0x8004000B)
RPCObjectOperationsHelper.cpp, 251, Not found. (0x8004000B)
CoRPCOverCOM.cpp, 7963, Not found. (0x8004000B)
RPCDocumentOperationsFileIO.cpp, 886, Not found. (0x8004000B)
RPCDocumentOperationsHelper.cpp, 22472, Not found. (0x8004000B)
ObjectFileHelper.cpp, 2205, Not found. (0x8004000B)
(M-Files 25.3.14681.8 2025-04-07T23:38:22.019Z)

Option Explicit

Dim objFSO: Set objFSO = CreateObject("Scripting.FileSystemObject")

' Retrieve the Object ID stored in the Test ID metadata.
Dim objTestID: Set objTestID = PropertyValues.SearchForProperty(Vault.PropertyDefOperations.GetPropertyDefIDByAlias("p.Test"))

If objTestID Is Nothing Or objTestID.TypedValue.IsEmpty Then
    Err.Raise 1001, "Workflow", "No Object ID found in Test ID field."
End If

Dim intPublishedObjectID: intPublishedObjectID = CLng(objTestID.TypedValue.Value)

' Create ObjID reference for the published document.
Dim objPublishedObjID: Set objPublishedObjID = CreateObject("MFilesAPI.ObjID")
objPublishedObjID.SetIDs MFBuiltInObjectTypeDocument, intPublishedObjectID

' Check out the target published document.
Dim objCheckedOutVersion: Set objCheckedOutVersion = Vault.ObjectOperations.CheckOut(objPublishedObjID)

' Retrieve the source files from the working version.
Dim objNewSourceFiles: Set objNewSourceFiles = GetNewObjectSourceFiles(ObjVer)

' Loop through and replace the target file with the source file.
Dim objSourceFile
For Each objSourceFile In objNewSourceFiles
    Dim szFilePath: szFilePath = objSourceFile.SourceFilePath

    ' Ensure the file exists before replacing.
    If Not objFSO.FileExists(szFilePath) Then
        Err.Raise 1003, "Workflow", "File does not exist: " & szFilePath
    End If

    ' Replace the published document with the new source file.
    Vault.ObjectFileOperations.UploadFile objCheckedOutVersion.ObjVer.ID, objCheckedOutVersion.ObjVer.Version, szFilePath
Next

' Check in the updated document.
Vault.ObjectOperations.CheckIn(objCheckedOutVersion.ObjVer)

' Clean up temporary files.
ClearTemporaryFiles objNewSourceFiles

''' HELPER FUNCTIONS

Function GetNewObjectSourceFiles(oSourceObjVer)
    Dim strTempFolderPath: strTempFolderPath = "D:\Create Object"
    Dim objFiles: Set objFiles = Vault.ObjectFileOperations.GetFiles(oSourceObjVer)
    Dim objSourceFiles: Set objSourceFiles = CreateObject("MFilesAPI.SourceObjectFiles")
    Dim objFile, objObjectSourceFile, strTemporaryFilePath

    For Each objFile In objFiles
        strTemporaryFilePath = objFSO.BuildPath(strTempFolderPath, objFSO.GetTempName()) & "." & objFile.Extension
        Vault.ObjectFileOperations.DownloadFile objFile.ID, objFile.Version, strTemporaryFilePath
        Set objObjectSourceFile = CreateObject("MFilesAPI.SourceObjectFile")
        objObjectSourceFile.SourceFilePath = strTemporaryFilePath
        objObjectSourceFile.Title = objFile.Title
        objObjectSourceFile.Extension = objFile.Extension
        objSourceFiles.Add -1, objObjectSourceFile
    Next

    Set GetNewObjectSourceFiles = objSourceFiles
End Function

Sub ClearTemporaryFiles(objObjectSourceFiles)
    Dim objSourceFile
    For Each objSourceFile In objObjectSourceFiles
        If objFSO.FileExists(objSourceFile.SourceFilePath) Then
            objFSO.DeleteFile objSourceFile.SourceFilePath, True
        End If
    Next
End Sub

Parents Reply Children
No Data