Catia VBA Automation错误运行时80010005-选择错误

时间:2018-07-13 13:34:37

标签: vba automation catia

我的用户表单有问题。在catpart中进行选择时,它应该自动切换到另一个TextBox。我收到自动化错误消息:在消息过滤器内进行调出是非法的。 运行时错误'-2147418107(80010005)

img

Sub Auswahl_Click()
    Dim sel As Object, Objekt As Object, ObjektTyp(0)
    Dim b, Auswahl, i As Integer
    ObjektTyp(0) = "Body"
    Set sel = CATIA.ActiveDocument.Selection
    For i = 1 To 6
        sel.Clear
        UserFormNow.Controls("Textbox" & i).SetFocus
        Auswahl = sel.SelectElement2(ObjektTyp, "Wähle ein Body aus...", False)
        Set b = CATIA.ActiveDocument.Selection.Item(i)
        If Auswahl = "Normal" Then
            Set Objekt = sel.Item(i)
            UserFormNow.ActiveControl = Objekt.Value.Name
            sel.Clear
        End If
        i = i + 1
    Next
    sel.Clear
End Sub

' EXCEL DATEI ÖFFNEN____________________________________
Sub Durchsuchen1_Click()
    Dim FPath As String
    FPath = CATIA.FileSelectionBox("Select the Excel file you wish to put the value in", "*.xlsx", CatFileSelectionModeOpen)
    If FPath = "" Then

        Else
            DurchsuchenFeld.AddItem FPath
            ListBox1.Clear
            ListBox1.AddItem "Bitte wählen Sie das Panel"
            TextBox1.SetFocus
    End If
End Sub
' FORMULAR SCHLIEßEN____________________________________
Sub ButtonEnd_Click()
    ButtonEnd = True
    Unload UserFormNow
End Sub

1 个答案:

答案 0 :(得分:0)

首先,您必须知道,当您使用UI并仍想与CATIA交互时,您必须选择:

  1. 以NoModal模式启动用户界面:UserFormNow.Show 0
  2. 每次要与CATIA交互时,请隐藏UI:Me.HideUserFormNow.Hide

然后,我强烈建议您避免查找带有名称的项目:

UserFormNow.Controls("Textbox" & i).SetFocus

如果您想对控件进行分组并在它们之间循环,请使用Frame,然后使用For Each循环。

For Each currentTextBox In MyFrame.Controls
    MsgBox currentTextBox.Text
Next

关于您的代码,可以做很多简化:

Private Sub Auswahl_Click()

    Dim sel As Object

    Dim currentTextBox As TextBox
    Dim Filter As Variant

    ReDim Filter(0)
    Filter(0) = "Body"

    Set sel = CATIA.ActiveDocument.Selection

    'Loop through each textbox
    For Each currentTextBox In MyFrame.Controls

        sel.Clear

        'Ask for the selection and test the result at the same time
        If sel.SelectElement2(Filter, "Wahle ein Body aus...", False) = "Normal" Then

            'Get the name without saving the object
            currentTextBox.Text = sel.Item2(1).Value.Name

        Else
            'allow the user to exit all the process if press Escape
            Exit Sub
        End If

    Next

    sel.Clear

End Sub