首页 > 解决方案 > 使用循环在文件管理系统中搜索对象

问题描述

谁能解释为什么这段代码不能正常工作?如果我分别使用每个单元格运行它,它工作正常。但是,当我在循环中运行它时,它适用于第一个对象,但随后它继续给出 0 作为结果。

我的代码正在我们的文件管理系统中搜索对象。

因此,如果我删除循环功能,请添加单元格值而不是cel.Value使用 ThisWorkbook.Sheets("Sheet").Range("B3").ValueorThisWorkbook.Sheets("Sheet").Range("B4").ValueThisWorkbook.Sheets("Sheet").Range("B5").Value并在每次正常工作时单击按钮运行代码(每次单击时我都会收到一条消息“有 1 个对象”)。当我按原样运行它时,我得到:“有 1 个对象”然后“有 0 个对象”,“有 0 个对象”

这是我的代码:

'set range
Dim myrng: Set myrng = ThisWorkbook.Sheets("Sheet").Range("B3:B5")

Dim cel
'loop through each cell in range
For Each cel In myrng

        ' ObjectType
        oExp.DataStatusValueType = MFilesAPI.MFStatusTypeObjectTypeID
        oTV.SetValue MFDatatypeLookup, 305
        oSearchCondition.Set oExp, MFConditionTypeEqual, oTV
        oSearchConditions.Add -1, oSearchCondition

        ' Class
        Set oSearchCondition = CreateObject("MFilesAPI.SearchCondition")
        oExp.DataPropertyValuePropertyDef = MFilesAPI.MFBuiltInPropertyDefClass
        oTV.SetValue MFDatatypeLookup, 177
        oSearchCondition.Set oExp, MFConditionTypeEqual, oTV
        oSearchConditions.Add -1, oSearchCondition

        ' Object has the same name
        Set oSearchCondition = CreateObject("MFilesAPI.SearchCondition")
        oExp.DataPropertyValuePropertyDef = MFilesAPI.MFBuiltInPropertyDefNameOrTitle
        oTV.SetValue MFDatatypetext, cel.Value ' Here it is
        oSearchCondition.Set oExp, MFConditionTypeEqual, oTV
        oSearchConditions.Add -1, oSearchCondition

        ' Object is not deleted
        Set oSearchCondition = CreateObject("MFilesAPI.SearchCondition")
        oExp.DataStatusValueType = MFilesAPI.MFStatusType.MFStatusTypeDeleted
        oTV.SetValue MFDatatypeBoolean, False
        oSearchCondition.Set oExp, MFConditionTypeEqual, oTV
        oSearchConditions.Add -1, oSearchCondition

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

        MsgBox ("There were " & oObjectSearchResults.Count & " objects")

Next

标签: excelvba

解决方案


好的,所以在 PeterT 的提示下,我设法通过将其添加到代码末尾来使其工作:

        Set oObjectSearchResults = Nothing
        Set oSearchConditions = Nothing
        Set oSearchCondition = Nothing
        Set oExp = Nothing
        Set oTV = Nothing

推荐阅读