vba 运行时错误“1004”:无法获取 OLEObjects 类的 Add 属性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25291577/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Run-Time Error '1004': Unable to get the Add property of the OLEObjects class
提问by neogeek23
I have a worksheet that upon opening it makes sure that every sheet has a button. So when a new sheet is present that doesn't have a button, it is set to add it. A few months ago I'm pretty sure this worked, but now (after not using this sheet for several months) I'm getting error '1004': "Unable to get the Add property of the OLEObjects class." The error occurs on the "set btn" line. What is causing this and how can I fix it?
我有一个工作表,在打开时确保每张表都有一个按钮。因此,当存在没有按钮的新工作表时,它会被设置为添加它。几个月前,我很确定这行得通,但现在(几个月未使用此表后)我收到错误“1004”:“无法获取 OLEObjects 类的 Add 属性。” 错误发生在“set btn”行上。这是什么原因造成的,我该如何解决?
Private btn As OLEObject
Public Const sButtonName1 As String = "btnTorqueCurveFit"
Public Const sBtnMessage1 As String = "Calculate Constant Torque Constants"
Public Const sButtonName2 As String = "btnESPCurveFit"
Public Const sBtnMessage2 As String = "Calculate Constant ESP Constants"
Public Const sButtonLeft1 As Long = 302.25
Public Const sButtonLeft2 As Long = 364.25
Private Sub AddTorqueButton(n As Worksheet)
'Add a Button to the Sheet
Set btn = n.OLEObjects.Add(ClassType:="Forms.CommandButton.1", Link:=False, DisplayAsIcon:=False, Left:=" & sButtonLeft1 &", Top:=3.75, Width:=60, Height:=57.75)
btn.Name = sButtonName1
btn.Object.Caption = sBtnMessage1
btn.Object.Font.Bold = True
btn.Object.WordWrap = True
'Modify the sheet's code to have newly added button call the general code in the module
Dim codeblock As CodeModule
Dim vbComp As VBComponent
Dim lineC As Integer
Dim Ap As String, _
Lf As String, _
Tabs As String, _
inputStr As String
Set vbComp = ActiveWorkbook.VBProject.VBComponents(n.CodeName)
Set codeblock = vbComp.CodeModule
Tabs = Chr(9)
Lf = Chr(10)
Ap = Chr(34)
inputStr = "Private Sub " & sButtonName1 & "_Click()" & Lf & Tabs & _
"ConstTorqueButtonAction ActiveSheet" & Lf & _
"End Sub"
With codeblock
lineC = .CountOfLines + 1
.InsertLines lineC, inputStr
End With
End Sub
Macro settings = 'Enable All,' Active X settings = 'Enable All,' the document is networked, but network documents are set to be trusted. It seem to be an issue with this workbook specifically as I have another workbook that use the same "set btn" style of code and it work on this machine, but the code displayed above produces an error. Any help or insight is appreciated.
宏设置 = 'Enable All',Active X 设置 = 'Enable All',文档已联网,但网络文档设置为受信任。这似乎是这个工作簿的一个问题,因为我有另一个工作簿使用相同的“set btn”风格的代码并且它在这台机器上工作,但上面显示的代码产生了错误。任何帮助或见解表示赞赏。
回答by djikay
The reason for the error is the way that the Left
argument is specified when calling OLEObjects.Add
. There is no need to be passing it as a string and the &
characters around it are unnecessary. In fact, that whole thing causes the error.
错误的原因Left
是调用时指定参数的方式OLEObjects.Add
。没有必要将它作为字符串传递,并且&
它周围的字符是不必要的。事实上,整个事情导致了错误。
Since the constant sButtonLeft1
is already of type Long
, you should just be passing it directly. Therefore, instead of passing a string like this:
由于常量sButtonLeft1
已经是 type Long
,你应该直接传递它。因此,而不是传递这样的字符串:
... , Left:=" & sButtonLeft1 &", ...
you should be calling it with the long parameter directly like this:
您应该像这样直接使用 long 参数调用它:
... , Left:=sButtonLeft1, ...
This should resolve the problem.
这应该可以解决问题。