我的代码在行
上发出错误 Set VM = AP.VBProject.VBComponents("ViewManager").Designer.Controls
我查了很多工作代码的例子,无法弄清楚我的设置是如何以不同方式导致错误的。
错误为Run-time error '91': Object variable or With block variable not set
感谢您的帮助。
Private Sub btnAdd_Click()
Dim View As String
Dim FField As String
Dim TField As String
View = cmbView.Value
FField = cmbFrmFld.Value
TField = cmbToFld.Value
'if it is the first add change one way, if after the first add change another
If ViewManager.Height = 116 Then
ViewManager.Height = ViewManager.Height + 64.5
ElseIf frmViews.Height > 116 Then
ViewManager.Height = ViewManager.Height + 30
End If
Dim AP As Project
Set AP = ActiveProject
Dim lbl As MSForms.Label
Dim VM As Object
Set VM = AP.VBProject.VBComponents("ViewManager").Designer.Controls
With VM
Set lbl = .Add("Forms.Label.1")
End With
With lbl
.Left = 6
.Top = ViewManager.Height - 32
.Width = 156
.Caption = View
End With
End Sub
答案 0 :(得分:1)
我相信您的问题是您正在使用VBIDE Forms Designer在加载时将控件添加到ViewManager表单中,这是不可能的。您可以看到这将以下代码添加到模块中,并在鼠标停止的“VM”VBComponent上添加监视(右键单击并添加监视)。您将看到'Designer'属性为'Nothing',而如果在未加载表单时运行相同的代码,您将能够访问Designer属性及其所有属性。
Sub CheckVBComponent()
Dim AP As Project, VM as VBIDE.VBComponent
Set AP = ActiveProject
'Note also this assumes that you've named your ViewManager correctly in the VBProject
Set VM = AP.VBProject.VBComponents("ViewManager")
'Add watch here
Stop
End Sub
这里简单的解决方法是在运行时直接将控件添加到表单而不使用VBIDE。例如:
Sub AddLabeltoMSProject
Dim frmLbl As MSForms.Label
Set frmLbl = ViewManager.Controls.Add("Forms.Label.1")
With frmLbl
.Caption = "I really love labels"
.Top = ViewManager.Height - 32
.Left = 6
.Width = 156
End With
End Sub