Example VB script for downloading file by ID

Hello,

I would like an example script to download a (single)file already present in M-Files to a temporary location on the server.

The script runs in a workflow not connected to the file. Selection of the file by ID number seems to me the best option.

I'm pretty experienced in writing scripts within M-Files, but I can't get this to work.

Greetings, Arno.

  • What is your primary challenge here? Identifying the document object that holds the file or downloading the file once you identified it?

  • My challenge is downloading the file by giving a fixed ID number. I don't think the ID number of the document will change.

  • After some more trial-and-error I came up with a solution:

    Option Explicit
    
    'Confirmation template ID  
    Dim strDocID: strDocID = 5457442
      
    'Initialize Target ObjVer
    Dim oObjID: Set oObjID = CreateObject("MFilesAPI.ObjVer")
    Call oObjID.SetIDs(0, strDocID, -1)
      
    'Get the latest object version
    Dim objFiles: Set objFiles = Vault.ObjectOperations.GetLatestObjVer(oObjID.ObjID, False, False)
    Dim objFile: Set objFile = Vault.ObjectFileOperations.GetFiles(objFiles).Item(1)
    
    'Set temp path
    Dim objFSO: Set objFSO = CreateObject("Scripting.FileSystemObject")
    Dim szTempPath: szTempPath = objFSO.GetSpecialFolder(2) & "\" & "CONF.docx"
    
    'Download order confirmation template
    Vault.ObjectFileOperations.DownloadFile objFile.ID, objFile.Version, szTempPath
    
    'Add order confirmation
    Vault.ObjectFileOperations.GetFilesForModificationInEventHandler(ObjVer)
    Vault.ObjectFileOperations.AddFile ObjVer, PropertyValues.SearchForProperty( 1095 ).TypedValue.DisplayValue & " Confirmation", "docx", szTempPath
    Call objFSO.DeleteFile(szTempPath, true)
    Vault.ObjectFileOperations.UpdateMetadataInFile ObjVer, -1, False

    I was mixing up ObjectID with fileID. Adding '.Item(1)' to the getfiles line made it work.

    Arno.