工作表上的容器框架中的表单对象处于非活动状态,除非手动切换设计模式。为什么?

[英]Form object in container frame on worksheet inactive unless Design Mode manually toggled. Why?


I want to create ActiveX objects directly on a worksheet. I can do this programmatically quite easily.

我想直接在工作表上创建ActiveX对象。我可以很容易地以编程方式完成此操作。

I also want several controls grouped together with a particular background. I decided that the easiest way to do this was to create them within a Frame object: ie the controls would be "child objects" of the frame.

我还想要将几个控件与特定背景组合在一起。我决定最简单的方法是在Frame对象中创建它们:即控件将是框架的“子对象”。

The following sample code does the job:

以下示例代码完成了这项工作:

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

The problem is: at the end of code execution, MyButton acts as if it's "locked", or disabled. In other words, the user can not click on it. There is no "button press" animation, of the sort that accompanies CommandButton objects.

问题是:在代码执行结束时,MyButton就像是“锁定”或禁用一样。换句话说,用户无法点击它。 CommandButton对象附带的排序没有“按下按钮”动画。

Adding .Enabled = True does not fix this: it is already enabled. It just acts like it's not.

添加.Enabled = True并不能解决这个问题:它已经启用了。它只是表现得不像。

Strangely however, if I manually enter "Design Mode" - and then exit again - the button enables itself!

然而,奇怪的是,如果我手动进入“设计模式” - 然后再次退出 - 按钮自动启用!

Does anyone know what's going on here? Moreover, what should I do to fix it?

有谁知道这里发生了什么?而且,我该怎么做才能修复它?

I found out how to programmatically enable/disable Design Mode:

我发现了如何以编程方式启用/禁用设计模式:

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

... however if I add the lines:

...但是,如果我添加行:

testEnter
DoEvents
testExit

... to the end of my Sub, the problem remains. Besides, even if it had worked, that solution seems like a hack anyway. I'd much rather understand what's going on here, and apply a proper solution.

......到我的Sub结束时,问题仍然存在。此外,即使它有效,该解决方案似乎也是一个黑客攻击。我更愿意了解这里发生了什么,并采用适当的解决方案。

1 个解决方案

#1


3  

I think this is an known issue with adding OLEObjects, workaround is to toggle between not visible and visible. In this case for your Frame. (or method mentioned in comment above)

我认为这是添加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

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

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

It is also not possible to step through with F8

使用F8也是不可能的


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.itdaan.com/blog/2018/07/23/1833fca9c0d7a8312c301ddc9fb3c477.html



 
© 2014-2018 ITdaan.com 粤ICP备14056181号