从SQL搜索到Excel列出值

时间:2019-07-17 10:59:49

标签: excel vba

我正在从Excel运行搜索,以通过外部ID获取对象的内部ID。在ThisWorkbook.Sheets("Other Data").Range("J30").Value中,我具有外部ID f5f9a21b-9208-de11-995f-005056bb3dfa。搜索后代码应显示内部ID。

这是可行的,我收到一条消息: 有3个对象的显示ID为f5f9a21b-9208-de11-995f-005056bb3dfa

例如,如何从单元格A1开始使这些代码实际显示这些ID?

因此,不仅是一条消息:

MsgBox(“有&结果。计数&“对象的显示ID为&&ThisWorkbook.Sheets(“ Other Data”)。Range(“ J30”)。Value)

我会在Excel中获得ID吗?基本上,我不需要results.Count来不计算项目,而是将它们输入到Excel。

Private Sub SurroundingSub()
    Set oVault = oMFClientApp.BindToVault(szVaultName, 0, True, True)
    ' Create the condition.
    Dim condition As New SearchCondition
    Dim oScs: Set oScs = CreateObject("MFilesAPI.SearchConditions")
    Dim oVaultConnections As MFilesAPI.VaultConnections

    ' Login to the vault
    Set oVaultConnections = oMFClientApp.GetVaultConnectionsWithGUID(szVaultGUID)

    If oVaultConnections.Count = 0 Then
        MsgBox "No vaults found with the GUID: " + szVaultGUID
        End
    Else
        On Error Resume Next

        Set oVault = oMFClientApp.BindToVault(oVaultConnections.Item(1).Name, 0, True, True)
        oVault.TestConnectionToVault

        If Err.Number <> 0 Then
            MsgBox "Can't connect to M-Files"
            End
        End If

        On Error GoTo 0
    End If

    ' Set the expression.
    condition.Expression.DataStatusValueType = MFStatusType.MFStatusTypeExtID

    ' Set the condition type.
    condition.ConditionType = MFConditionType.MFConditionTypeEqual

    ' Set the value.
    ' In this case "MyExternalObjectId" is the ID of the object in the remote system.
    condition.TypedValue.SetValue MFDataType.MFDatatypeText, ThisWorkbook.Sheets("Other Data").Range("J30").Value

            'Add the condition to the collection.
            oScs.Add -1, condition

            'Search.
            Dim results 'As ObjectSearchResults
            Set results = oVault.ObjectSearchOperations.SearchForObjectsByConditions(oScs, MFSearchFlags.MFSearchFlagNone, False) ' False = SortResults

            'Output the number of items matching (should be one in each object type, at a maximum).
            MsgBox ("There were " & results.Count & " objects with the display Id of" & ThisWorkbook.Sheets("Other Data").Range("J30").Value)

    'results = ThisWorkbook.Sheets("Other Data").Range("A1").Value

End Sub

编辑

根据建议编辑了代码,但调试器指向ThisWorkbook.Sheets("Start").Cells(i, 1).Value = results(i)

Private Sub SurroundingSub()
    Set oVault = oMFClientApp.BindToVault(szVaultName, 0, True, True)
    ' Create the condition.
    Dim condition As New SearchCondition
    Dim oScs: Set oScs = CreateObject("MFilesAPI.SearchConditions")
    Dim oVaultConnections As MFilesAPI.VaultConnections
    Dim i As Integer

    ' Login to the vault
    Set oVaultConnections = oMFClientApp.GetVaultConnectionsWithGUID(szVaultGUID)

    If oVaultConnections.Count = 0 Then
        MsgBox "No vaults found with the GUID: " + szVaultGUID
        End
    Else
        On Error Resume Next

        Set oVault = oMFClientApp.BindToVault(oVaultConnections.Item(1).Name, 0, True, True)
        oVault.TestConnectionToVault

        If Err.Number <> 0 Then
            MsgBox "Can't connect to M-Files"
            End
        End If

        On Error GoTo 0
    End If

    ' Set the expression.
    condition.Expression.DataStatusValueType = MFStatusType.MFStatusTypeExtID

    ' Set the condition type.
    condition.ConditionType = MFConditionType.MFConditionTypeEqual

    ' Set the value.
    ' In this case "MyExternalObjectId" is the ID of the object in the remote system.
    condition.TypedValue.SetValue MFDataType.MFDatatypeText, ThisWorkbook.Sheets("Other Data").Range("J30").Value

            'Add the condition to the collection.
            oScs.Add -1, condition

            'Search.
            Dim results 'As ObjectSearchResults
            Set results = oVault.ObjectSearchOperations.SearchForObjectsByConditions(oScs, MFSearchFlags.MFSearchFlagNone, False) ' False = SortResults

            'Output the number of items matching (should be one in each object type, at a maximum).
            'MsgBox ("There were " & results.Count & " objects with the display Id of" & ThisWorkbook.Sheets("Other Data").Range("J30").Value)

            For i = 1 To results.Count
            ThisWorkbook.Sheets("Start").Cells(i, 1).Value = results[i]
            Next i

End Sub

enter image description here


编辑2

也()不起作用:

enter image description here

1 个答案:

答案 0 :(得分:1)

如果您正在寻找一种快速直接的方法,请尝试以下操作:

For i = 1 To results.Count Cells(i, 1).Value = results(i - 1) Next i

提示:单元格(行,列)