Excel VBA多页页面选项卡焦点异常

问题描述 投票:0回答:0

我有一个非模态用户窗体(名为UF1),多页(MP1)在第1页(Pg1)上有一个命令按钮(CB1),
和空白页 2 (Pg2)

然后我执行以下操作(“iwc:”表示立即窗口命令);

iwc: UF1.Show

Select CB1                                 'it gets the Focus
iwc: ? UF1.MP1.Page1.ActiveContol.Name     'CB1

Press <Tab>                                'the Pg1 Tab gets the Focus
iwc: ? UF1.MP1.Page1.ActiveContol.Name     'still CB1

Press <RightArrow>                         'the Pg2 Tab gets the Focus; if Page1.CB1 still  
                                           'had the Focus the RightArrow would have acted   
                                           'on it (and done nothing)   

iwc: ? UF1.MP2.Pg2.ActiveContol.Name       'Runtime Error `91: Object Variable Not Set  
                                           'Selecting a Multipage Page moves the Focus to  
                                           'the Control with the Lowest TagIndex;  
                                           'in this case Pg2 has No Controls
  

以下函数 (ufActConCSV) 以 CSV 格式返回用户窗体的 ActiveControl 的“路径”。
对于上面的示例,当 Pg1 选项卡具有焦点时,它返回“UF1,MP1,Pg1”。
它使用 SendKeys() 解决异常情况

    Function _
ufActConCSV(iUF As UserForm) As String

    'Returns iUF's ActiveControl's "Path" as a CSV
    '   eg "MultiPage1,Page1,Frame1,CommandButton1"
    '   Returns Null if iUF is Not Visible, or is a Blank UserForm

    Dim zChi As Object
    Dim zMP As Boolean
    Dim zMPPg As Object

    Set zChi = iUF.ActiveControl
    If Not zChi Is Nothing Then
NChiL:
        ufActConCSV = ufActConCSV & zChi.Name & ","
        zMP = TypeOf zChi Is MSForms.MultiPage
        If zMP Or TypeOf zChi Is MSForms.TabStrip Then
            ufActConCSV = ufActConCSV & zChi.SelectedItem.Name & ","
            Set zChi = zChi.SelectedItem
            If zMP Then Set zMPPg = zChi
        End If
        If zMP Or TypeOf zChi Is MSForms.Frame Then
            If Not zChi.ActiveControl Is Nothing Then
                Set zChi = zChi.ActiveControl
                GoTo NChiL
            End If
        End If
    End If
    If Not ufActConCSV = "" Then _
        ufActConCSV = Left(ufActConCSV, Len(ufActConCSV) - 1)
    If Not zMPPg Is Nothing Then
        Debug.Print UF1.MP2.Pages(0).ActiveControl.Name
        SendKeys "{RIGHT}", True
        DoEvents
        If zMPPg.Parent.SelectedItem.Name <> zMPPg.Name Then
            zMPPg.Parent.Value = zMPPg.Index
            ufActConCSV = Left(ufActConCSV, InStr(ufActConCSV, zMPPg.Name) _
                + Len(zMPPg.Name) - 1)
        End If
        On Error Resume Next
        zChi.SetFocus
    End If
    
    Debug.Print "ufActConCSV{}=" & ufActConCSV
    End Function

很乱;有没有更简单的方法来查明 MultiPage 的 Tab 是否有焦点?

excel vba focus multipage
© www.soinside.com 2019 - 2024. All rights reserved.