excel - 使用循环在文件管理系统中搜索对象
问题描述
谁能解释为什么这段代码不能正常工作?如果我分别使用每个单元格运行它,它工作正常。但是,当我在循环中运行它时,它适用于第一个对象,但随后它继续给出 0 作为结果。
我的代码正在我们的文件管理系统中搜索对象。
因此,如果我删除循环功能,请添加单元格值而不是cel.Value
使用 ThisWorkbook.Sheets("Sheet").Range("B3").Value
orThisWorkbook.Sheets("Sheet").Range("B4").Value
或ThisWorkbook.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
解决方案
好的,所以在 PeterT 的提示下,我设法通过将其添加到代码末尾来使其工作:
Set oObjectSearchResults = Nothing
Set oSearchConditions = Nothing
Set oSearchCondition = Nothing
Set oExp = Nothing
Set oTV = Nothing
推荐阅读
- windows-runtime - 是否可以为 Windows 运行时组件(C++/WinRT)添加版本?
- amazon-web-services - 为 Autoscaling Group 中的实例分配静态弹性 IP
- javascript - 我可以使用返回加权布尔值的函数来实现加权随机性吗?
- python-3.x - ImportError:无法从“countstring”导入名称“count_hi”
- pandas - 如何从 Google 表格上传到 BigQuery?我可以通过 Pandas DF 来完成吗?
- swagger-2.0 - 嵌套异常是 java.lang.NoClassDefFoundError: com/fasterxml/jackson/databind/ser/std/ToStringSerializerBase
- javascript - Angular - 如何禁用保留值的字段
- psycopg2 - 使用python在Postgresql中提取大数据(最好是数据框格式)
- lit-element - 使用 lit-element 渲染多个页面之一
- amazon-web-services - 使用 Cloudformation 模板更新现有 AWS 资源