[英]How to create and set a ActiveX Control CommandButton as a variable Excel VBA
I am trying to Create a New ActiveX Control CommandButton with Excel VBA.我正在尝试使用 Excel VBA 创建一个新的 ActiveX 控件命令按钮。 I have a loop VBA which has worked in the past, theFile1.1.xlsm has the master list of the workbooks.我有一个过去工作过的循环 VBA,theFile1.1.xlsm 有工作簿的主列表。 I need to add a CommandButton to ~3200 workbooks, so I will be using the Do-Loop macro.我需要向 ~3200 个工作簿添加一个 CommandButton,因此我将使用 Do-Loop 宏。 Here is the Loop code for reference.这是循环代码供参考。
Sub Macro2()
Application.ScreenUpdating = False
Dim sFile As String
Dim wb As Workbook
Dim FileName1 As String
Dim FileName2 As String
Dim wksSource As Worksheet
Const scWkbSourceName As String = "theFILE 1.1.xlsm"
Set wkbSource = Workbooks(scWkbSourceName)
Set wksSource = wkbSource.Sheets("Sheet1") ' Replace 'Sheet1' w/ sheet name of SourceSheet
Const wsOriginalBook As String = "theFILE 1.1.xlsm"
Const sPath As String = "E:\ExampleFolder\"
SourceRow = 5
Do While Cells(SourceRow, "D").Value <> ""
Sheets("Sheet1").Select
FileName1 = wksSource.Range("A" & SourceRow).Value
FileName2 = wksSource.Range("K" & SourceRow).Value
sFile = sPath & FileName1 & "\" & FileName2 & ".xlsm"
Set wb = Workbooks.Open(sFile)
''insert code for loop operation
'''CLOSE WORKBOOK W/O BEFORE SAVE
Application.EnableEvents = False
ActiveWorkbook.Save
ActiveWorkbook.Close
Application.EnableEvents = True
SourceRow = SourceRow + 1
Loop
End Sub
I would like to have the button set as a Variable (i think), so I can edit the formatting/properties and hopefully add a macro to the button later.我想将按钮设置为变量(我认为),以便我可以编辑格式/属性,并希望稍后向按钮添加宏。
Dim buttonControl As MSForms.CommandButton
Set buttonControl = _
ActiveSheet.OLEObjects.Add(ClassType:="Forms.CommandButton.1", _
Link:=False, _
DisplayAsIcon:=False, _
Left:=1464, Top:=310, Width:=107.25, Height:=30)
With buttonControl.Opject
.Caption = "OPEN FOLDER"
.Name = "cmd_OPEN_FOLDER"
End With
I have a 'Run-time error 13: Type Mismatch' error.我有一个“运行时错误 13:类型不匹配”错误。 I am unsure why, because a 'CommandButton1' is created in the correct place.我不确定为什么,因为在正确的位置创建了“CommandButton1”。
OLEObjects.Add
creates an OLEObject
and adds it to the OLEObjects
collection; OLEObjects.Add
创建一个OLEObject
并将其添加到OLEObjects
集合中; the object returned by the Add
function is OLEObject
, not MSForm.CommandButton
. Add
函数返回的对象是OLEObject
,而不是MSForm.CommandButton
。 That's the underlying type of OLEObject.Object
- so, set your buttonControl
to the .Object
property of the returned object:这是OLEObject.Object
的基础类型 - 因此,将buttonControl
设置为返回对象的.Object
属性:
Set buttonControl = _
ActiveSheet.OLEObjects.Add(ClassType:="Forms.CommandButton.1", _
Link:=False, _
DisplayAsIcon:=False, _
Left:=1464, Top:=310, Width:=107.25, Height:=30).Object
The button is created in the correct place, because the Add
function works and returns - what's failing with a type mismatch is the assignment of the returned OLEObject
into a CommandButton
variable, immediately after that operation.该按钮是在正确的位置创建的,因为Add
函数工作并返回 - 类型不匹配的失败是在该操作之后立即将返回的OLEObject
分配到CommandButton
变量。
The subsequent With
block can then be just With buttonControl
.随后的With
块可以只是With buttonControl
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.