0

I am wondering is there any way to shorten my current code. I am running one process in a lot of my macros and was thinking about subroutine. I have tried several approaches but can't get it work.

I am performing one search several times:

Sub AddFileToDC()

    '... I am getting oDCSearchConditions from the search, then

    ' Execute the search
    Dim oDCObjectSearchResults As MFilesAPI.ObjectSearchResults
    Set oDCObjectSearchResults = oVault.ObjectSearchOperations.SearchForObjectsByConditions(oDCSearchConditions, MFSearchFlagNone, False)

    ' Get a reference to the existing document collection
    Set oDocumentCollectionOVAP = oVault.ObjectOperations.GetObjectVersionAndProperties(oDCObjectSearchResults.Item(1).ObjVer)

    Set oOldDocumentOVAP = oVault.ObjectOperations.GetObjectVersionAndProperties(oObjectSearchResults.Item(1).ObjVer)

End Sub

In all other macros I am performing another search from where I get oObjectSearchResults.

Instead of copying code above to all my macros, is it possible to do something like:

Sub Code1()

    '... I am getting oSearchConditions from the search, then

        ' Execute the search
        Dim oObjectSearchResults As MFilesAPI.ObjectSearchResults
        Set oObjectSearchResults = oVault.ObjectSearchOperations.SearchForObjectsByConditions(oSearchConditions, MFSearchFlagNone, False)

    AddFileToDC

End Sub

Now while running Code1 I am getting error message saying variable oDCObjectSearchResults not defined as it is in another sub AddFileToDC...

1
  • You can't have the same variable name in both sets of code as the memory is already allocated for that specific variable name; your call for the macro AddFileToDC needs to have the dimensioning/setting removed for that variable being dimensioned/set in Code1. You will most likely need to dimension the variable globally so both subroutines can utilize it. Commented Sep 25, 2019 at 15:44

1 Answer 1

1

Making your like-named variable a global variable, while removing the setting/dimensioning from the called macro after you have set the variable:

Public oObjectSearchResults As MFilesAPI.ObjectSearchResults

Sub Code1()

        ' Execute the search
        Set oObjectSearchResults = oVault.ObjectSearchOperations.SearchForObjectsByConditions(oSearchConditions, MFSearchFlagNone, False)

    AddFileToDC

End Sub

Sub AddFileToDC()

    ' Get a reference to the existing document collection
    Set oDocumentCollectionOVAP = oVault.ObjectOperations.GetObjectVersionAndProperties(oDCObjectSearchResults.Item(1).ObjVer)

    Set oOldDocumentOVAP = oVault.ObjectOperations.GetObjectVersionAndProperties(oObjectSearchResults.Item(1).ObjVer)

End Sub
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.