我想直接在工作表上创建ActiveX对象。我可以以编程方式执行此操作。
我还希望将多个控件与特定的背景分组在一起。我是在Frame对象中创建它们的:即控件将是框架的“子对象”。
以下示例代码完成了该工作:

Sub CreateFormOnSheet()
    With ActiveSheet
        ' Add the frame background:
        .OLEObjects.Add(ClassType:="Forms.Frame.1", Left:=10, Top:=10, Width:=300, Height:=300).Name = "container_frame"
        With .OLEObjects("container_frame")
            With .Object
                .Caption = "This is the frame caption"
                .BackColor = RGB(150, 0, 100)
                .BorderColor = RGB(255, 255, 255)
                .Controls.Add("Forms.CommandButton.1").Name = "MyButton"
                With .Controls("MyButton")
                    .Left = 10
                    .Top = 10
                    .Width = 100
                    .Height = 50
                    .BackColor = RGB(0, 0, 100)
                    .ForeColor = RGB(255, 255, 255)
                    .Caption = "My Button"
                    .FontName = "Arial"
                    .Font.Bold = True
                    .Font.Size = 10
                    .WordWrap = True
                End With
            End With
        End With
    End With
End Sub
问题是:在代码执行结束时,MyButton就像“锁定”或禁用一样。用户无法单击它。没有CommandButton对象随附的那种“按钮按下”动画。
添加.Enabled = True不能解决此问题。它已经启用,只是好像没有启用。
如果我手动输入“设计模式”-然后再次退出-该按钮将启用。
我发现了如何以编程方式启用/禁用设计模式:
Sub testEnter()
    EnterExitDesignMode True
End Sub

Sub testExit()
    EnterExitDesignMode False
End Sub

Sub EnterExitDesignMode(bEnter As Boolean)
Dim cbrs As CommandBars
Const sMsoName As String = "DesignMode"

    Set cbrs = Application.CommandBars
        If Not cbrs Is Nothing Then
        If cbrs.GetEnabledMso(sMsoName) Then
            If bEnter <> cbrs.GetPressedMso(sMsoName) Then
                cbrs.ExecuteMso sMsoName
                Stop
            End If
        End If
    End If
End Sub
...但是,如果我添加以下行:
testEnter
DoEvents
testExit
...到我的Sub的末尾,问题仍然存在。即使有效,这似乎也很容易。我宁愿了解这里发生的事情,并应用适当的解决方案。

最佳答案

我认为这是添加OLEObjects的已知问题,解决方法是在不可见和可见之间切换。在这种情况下适合您的相框。 (或上面评论中提到的方法)

Sub CreateFormOnSheet()
    With ActiveSheet
        ' Add the frame background:
        .OLEObjects.Add(ClassType:="Forms.Frame.1", Left:=10, Top:=10, Width:=300, Height:=300).Name = "container_frame"
        With .OLEObjects("container_frame")
            With .Object
                .Caption = "This is the frame caption"
                .BackColor = RGB(150, 0, 100)
                .BorderColor = RGB(255, 255, 255)
                .Controls.Add("Forms.CommandButton.1").Name = "MyButton"
                With .Controls("MyButton")
                    .Left = 10
                    .Top = 10
                    .Width = 100
                    .Height = 50
                    .BackColor = RGB(0, 0, 100)
                    .ForeColor = RGB(255, 255, 255)
                    .Caption = "My Button"
                    .FontName = "Arial"
                    .Font.Bold = True
                    .Font.Size = 10
                    .WordWrap = True
                 End With
            End With
            .Visible = False 'toggle the Frame
            .Visible = True
        End With
      'or Sheets(1).Activate
      'or .Activate
     End With

End Sub

也可以看看:
https://www.excelforum.com/excel-programming-vba-macros/679211-cant-enter-break-mode-at-this-time-error.html#post2073900

也无法单步执行F8

关于excel - 除非手动切换“设计模式”,否则工作表上容器框架中的表单对象处于非事件状态,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/51480459/

10-12 12:43
查看更多