This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

VB script code to search for a specific object with a specific metadata value

HI,

I am not a developper, but I tried to run a vb script code to search for a specific opbjet (type "produits finis") which has the (unique) metadata value ("matricule").

This metadata is deducted from the content of the file through regex which is OK.

The code is rising an error : "type incompatible : Expression.SetPropertyValueExpression". I can't find what is the right syntax I should use. (line 71)

Can someone help me?

Many thanks in advance

'Extract metadata from file content using regex.
'Suited for documents with a fixed content format and limited file size.
'2022.06.27 Karl Lausten
Option Explicit

Dim objID : set objID = ObjVer.ObjID
Dim objVersion : set objVersion = Vault.ObjectOperations.GetLatestObjectVersionAndProperties(objID, true)
Dim myFiles : Set myFiles = objVersion.VersionData.Files
Dim myFile
Dim fileVer
For Each myFile in myFiles
	set fileVer = myFile.FileVer
Next
'This script was made to run on single file objects. The script is not prepared to handle multiple files!
'Running it on documents with multiple files will create problems such as propertyvalues being overwritten in the proces below.


'on récupère le text intégral du fichier
Dim szFullText :szFullText = Vault.ObjectFileOperations.GetTextContentForFile(ObjVer, fileVer)

Dim oPattern : Set oPattern = New RegExp 'le pattern de la regex


'ici on récupère dans le texte le matricule
Dim oPropertyValue
Dim iMAT : iMAT = Vault.PropertyDefOperations.GetPropertyDefIDbyAlias("PD.Matricule") 'iMAT est l'ID de la propritété MAtricule
With oPattern
	'.Pattern = "(\d{2}\-\d{2}\-\d{4})(?=\s*Dato:)"
	.Pattern = "([A-Z]*[0-9]{3,4}[A-Z]{1,2}[0-9]{4,6})"
	.IgnoreCase = True
	.Global = True
End With
Dim szMatch : set szMatch = oPattern.Execute(szFullText)
'Err.Raise MFScriptCancel,szMatch.Item(0).SubMatches(0)
Set oPropertyValue = PropertyValues.SearchForProperty(iMAT)
oPropertyValue.TypedValue.SetValue MFDatatypeText, szMatch.Item(0).SubMatches(0)
Vault.ObjectPropertyOperations.SetProperty ObjVer,oPropertyValue




'ici on récupère dans l'objet du mail la couleur du toner
Dim NewPropertyValue
Dim iObjet_email : iObjet_email = Vault.PropertyDefOperations.GetPropertyDefIDbyAlias("PD.Objet")
Dim szObjet_email : szObjet_email=PropertyValues.SearchForProperty(iObjet_email).typedvalue.value
With oPattern
	'.Pattern = "(\d{2}\-\d{2}\-\d{4})(?=\s*Dato:)"
	.Pattern = "(.yan|.ellow|.aune|.oir|.agenta|.lack)"
	.IgnoreCase = True
	.Global = True
End With
set szMatch = oPattern.Execute(szObjet_email)
Set NewPropertyValue = PropertyValues.SearchForProperty(iObjet_email)
NewPropertyValue.TypedValue.SetValue MFDatatypeText, szMatch.Item(0).SubMatches(0)
Vault.ObjectPropertyOperations.SetProperty ObjVer,NewPropertyValue


''''''''''''''''' la suite ici permet d'aller cherche le copieur qui a ce matricule
Dim matriculecherche :matriculecherche = szMatch.Item(0).SubMatches(0) 
' Initialize the API and connect to a vault.

' initialise une searchcondition et une search condition multiple
    Dim FstSearchCondition : Set FstSearchCondition = CreateObject("MFilesAPI.SearchCondition")
    Dim AllSearchConditions : Set AllSearchConditions = CreateObject("MFilesAPI.SearchConditions") 
	

'FstSearchCondition.Expression.SetValueListItemExpression 2, 0, Nothing ' MFValueListItemPropertyDefName = 2, MFParentChildBehaviorNone = 0
 
''We want to search by property - in this case the built-in "name or title" property.
''Alternatively we could pass the ID of the property definition if it's not built-in.
FstSearchCondition.Expression.SetPropertyValueExpression 1308, 0
 
'' We want only items that equal the search string provided.
FstSearchCondition.ConditionType = MFConditionTypeEqual
 
'' Note that the type must both match the property definition type, and be applicable for the supplied value.
FstSearchCondition.TypedValue.SetValue MFDatatypeText, matriculecherche

AllSearchConditions.Add -1, oSearchCondition

'' Create the seconde condition.
    Dim SdSearchCondition : Set FstSearchCondition = CreateObject("MFilesAPI.SearchCondition")

'' Set the expression.
	SdSearchCondition.Expression.SetStatusValueExpression MFStatusTypeObjectTypeID
 
'' Set the condition.
	SdSearchCondition.ConditionType = MFConditionTypeEqual
 
'' Set the value.
	SdSearchCondition.TypedValue.SetValue MFDatatypeLookup, 194 'OBJET produit fini ID (194)
 
'' Add the condition to the collection.
AllSearchConditions.Add -1,  SdSearchCondition



' Invoke the search operation.
Dim oSearchResults : Set oSearchResults = Vault.ObjectSearchOperations.SearchForObjectsByConditionsEx (AllSearchConditions)

Call Console.WriteLine("Title of " + oObjType.NameSingular + ": " + oObjectVersion.Title)

' Simply process the search results.
'For Each oObjectVersion As MFilesAPI.ObjectVersion In oObjectVersions

    ' Resolve the object type.
    'Dim oObjType As MFilesAPI.ObjType
   ' oObjType = oVault.ObjectTypeOperations.GetObjectType(oObjectVersion.ObjVer.Type)

    ' Output the result.
   ' Call Console.WriteLine("Title of " + oObjType.NameSingular + ": " + oObjectVersion.Title)

'Next

Parents
  • According to the documentation in https://developer.m-files.com/APIs/COM-API/Reference/#MFilesAPI~Expression~SetPropertyValueExpression.html you need to include a value for ParentChild behavior as parameter. Your line 71 should probably look something like this:

    FstSearchCondition.Expression.SetPropertyValueExpression 1308, MFParentChildBehaviorNone, 0

  • Hi  . Thanks for your help which put me on the way.

    In VB, the correct syntax seems to be : FstSearchCondition.Expression.SetPropertyValueExpression 1308, MFParentChildBehavior.MFParentChildBehaviorNone, Nothing

    Anymway, I now have an error on line 94 which is (extract of the detailed error) :

    "

    2, StateAction, 94, Type incompatible: 'Vault.ObjectSearchOperations.SearchForObjectsByCondition' (0x80040008)
    MErrorHelper.cpp, 2350, Type incompatible: 'Vault.ObjectSearchOperations.SearchForObjectsByCondition' (0x80040008)"

    I can't find how to solve this. Can anyone may help?

    Thanks in advance,

    'Extract metadata from file content using regex.
    'Suited for documents with a fixed content format and limited file size.
    '2022.06.27 Karl Lausten
    Option Explicit
    
    Dim objID : set objID = ObjVer.ObjID
    Dim objVersion : set objVersion = Vault.ObjectOperations.GetLatestObjectVersionAndProperties(objID, true)
    Dim myFiles : Set myFiles = objVersion.VersionData.Files
    Dim myFile
    Dim fileVer
    For Each myFile in myFiles
    	set fileVer = myFile.FileVer
    Next
    'This script was made to run on single file objects. The script is not prepared to handle multiple files!
    'Running it on documents with multiple files will create problems such as propertyvalues being overwritten in the proces below.
    
    
    'on récupère le text intégral du fichier
    Dim szFullText :szFullText = Vault.ObjectFileOperations.GetTextContentForFile(ObjVer, fileVer)
    
    Dim oPattern : Set oPattern = New RegExp 'le pattern de la regex
    
    
    'ici on récupère dans le texte le matricule
    Dim oPropertyValue
    Dim iMAT : iMAT = Vault.PropertyDefOperations.GetPropertyDefIDbyAlias("PD.Matricule") 'iMAT est l'ID de la propritété MAtricule
    With oPattern
    	'.Pattern = "(\d{2}\-\d{2}\-\d{4})(?=\s*Dato:)"
    	.Pattern = "([A-Z]*[0-9]{3,4}[A-Z]{1,2}[0-9]{4,6})"
    	.IgnoreCase = True
    	.Global = True
    End With
    Dim szMatch : set szMatch = oPattern.Execute(szFullText)
    'Err.Raise MFScriptCancel,szMatch.Item(0).SubMatches(0)
    Set oPropertyValue = PropertyValues.SearchForProperty(iMAT)
    oPropertyValue.TypedValue.SetValue MFDatatypeText, szMatch.Item(0).SubMatches(0)
    Vault.ObjectPropertyOperations.SetProperty ObjVer,oPropertyValue
    
    
    
    
    'ici on récupère dans l'objet du mail la couleur du toner
    Dim NewPropertyValue
    Dim iObjet_email : iObjet_email = Vault.PropertyDefOperations.GetPropertyDefIDbyAlias("PD.Objet")
    Dim szObjet_email : szObjet_email=PropertyValues.SearchForProperty(iObjet_email).typedvalue.value
    With oPattern
    	'.Pattern = "(\d{2}\-\d{2}\-\d{4})(?=\s*Dato:)"
    	.Pattern = "(.yan|.ellow|.aune|.oir|.agenta|.lack)"
    	.IgnoreCase = True
    	.Global = True
    End With
    set szMatch = oPattern.Execute(szObjet_email)
    Set NewPropertyValue = PropertyValues.SearchForProperty(iObjet_email)
    NewPropertyValue.TypedValue.SetValue MFDatatypeText, szMatch.Item(0).SubMatches(0)
    Vault.ObjectPropertyOperations.SetProperty ObjVer,NewPropertyValue
    
    
    ''''''''''''''''' la suite ici permet d'aller cherche le copieur qui a ce matricule
    Dim szSearchText :szSearchText = szMatch.Item(0).SubMatches(0) 
    ' Initialize the API and connect to a vault.
    
    ' initialise une searchcondition et une search condition multiple
       Dim FstSearchCondition : Set FstSearchCondition = CreateObject("MFilesAPI.SearchCondition")
       Dim AllSearchConditions : Set AllSearchConditions = CreateObject("MFilesAPI.SearchConditions") 
    	
    
    FstSearchCondition.ConditionType = MFConditionType.MFConditionTypeEqual
    
    FstSearchCondition.Expression.SetPropertyValueExpression 1308, MFParentChildBehavior.MFParentChildBehaviorNone, Nothing
    FstSearchCondition.TypedValue.SetValue MFDataType.MFDatatypeText, szSearchText
    
     
    AllSearchConditions.Add -1, FstSearchCondition
    
    '' Create the seconde condition.
        Dim SdSearchCondition : Set SdSearchCondition = CreateObject("MFilesAPI.SearchCondition")
    
    '' Set the expression.
    	SdSearchCondition.Expression.SetStatusValueExpression MFStatusType.MFStatusTypeObjectID,Nothing
     
    '' Set the condition.
    	SdSearchCondition.ConditionType = MFConditionType.MFConditionTypeEqual
     
    '' Set the value.
    	SdSearchCondition.TypedValue.SetValue MFDatatypeLookup, 194 'OBJET produit fini ID (194)
     
    '' Add the condition to the collection.
    AllSearchConditions.Add -1,  SdSearchCondition
    
    
    
    ' Invoke the search operation.
    Dim oResults 'As ObjectSearchResults
    	  	Set oResults = Vault.ObjectSearchOperations.SearchForObjectsByCondition(AllSearchConditions, False)
    
    Dim oRes
    Set oRes = oResults.Item(1)
    
    'output = oResults.ObjVer.ID
    
    
    
    'Dim oSearchResults : Set oSearchResults = CreateObject("MFilesAPI.ObjectSearchResults")
    
    'oSearchResults.SearchForObjectsByConditions AllSearchConditions, MFSearchFlags.MFSearchFlagNone, true
    
    Call Console.WriteLine("Title of " + oRes.Name + ": " + oRes.Title)
    
    ' Simply process the search results.
    'For Each oObjectVersion As MFilesAPI.ObjectVersion In oObjectVersions
    
        ' Resolve the object type.
        'Dim oObjType As MFilesAPI.ObjType
       ' oObjType = oVault.ObjectTypeOperations.GetObjectType(oObjectVersion.ObjVer.Type)
    
        ' Output the result.
       ' Call Console.WriteLine("Title of " + oObjType.NameSingular + ": " + oObjectVersion.Title)
    
    'Next
    
    

  • It is probably quite simple. You used SearchForObjectsByCondition (singular) but you have specified a set of 2 conditions. You should use SearchForObjectsByConditions (plural)

Reply Children
No Data