jimzhoujim 发表于 2008-11-1 11:04:00

动态添加控件

<p>Private Sub CommandButton1_Click()<br/>Dim jim As Control<br/>Set jim = Me.Controls.Add("vb.commandbutton")<br/>End Sub</p><p>我想在VBA里面动态添加控件,可是老是提示“无效的类别字符串”,请大家帮我看看,谢谢</p>

王咣生 发表于 2008-11-1 14:11:00

我在网上查了一下,试验结果:<br/><br/>Private WithEvents btn1 As CommandButton<br/><br/>Private Sub CommandButton1_Click()<br/>    Dim jim As CommandButton<br/>    Set jim = Me.Controls.Add("Forms.CommandButton.1", "btn1")<br/>    <br/>    With jim<br/>        .Left = 20<br/>        .top = 50<br/>        <br/>    End With<br/>    <br/>    Set btn1 = jim<br/>End Sub<br/><br/>Private Sub btn1_Click()<br/>    MsgBox "hi"<br/>End Sub<br/>

jimzhoujim 发表于 2008-11-1 14:42:00

<p>谢谢,我刚才发现是帮助文档里的例子有错误</p><p>Set jim = Me.Controls.Add("Forms.CommandButton.1", "btn1")</p><p>帮助里是Set jim = Me.Controls.Add("MSForms.CommandButton.1", "btn1")</p>
页: [1]
查看完整版本: 动态添加控件