vba 检查 CommandBarButton 是否存在
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14565912/
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
Check if a CommandBarButton exists
提问by dan
I'm using the following in the ThisOutlookSessionof my VbaProject.OTM file to add 2 custom buttons to the new mail's Standardtoolbar:
我在VbaProject.OTM 文件的ThisOutlookSession中使用以下内容将 2 个自定义按钮添加到新邮件的标准工具栏:
Dim outl As Object
Dim msg As Object
Set outl = CreateObject("Outlook.Application")
Set msg = outl.CreateItem(0)
msg.Display (False)
Dim objBar As Office.CommandBar
Dim objButton As Office.CommandBarButton
Set objBar = Application.ActiveWindow.CommandBars("Standard")
Set objButton = objBar.Controls.Add(msoControlButton)
With objButton
.caption = "button1"
.OnAction = "macro1"
.TooltipText = "Description"
.faceId = 487
.Style = msoButtonIconAndCaption
.BeginGroup = True
End With
Set objButton = objBar.Controls.Add(msoControlButton)
With objButton
.caption = "button2"
.OnAction = "macro2"
.TooltipText = "Description"
.faceId = 2525
.Style = msoButtonIconAndCaption
.BeginGroup = True
End With
msg.Close 1
The problem is that the buttons will be added every time Outlook starts (which is needed for the other computers I'm willing to deploy my OTM file to). Is there any way to check before adding the buttons if it already exists?
问题是每次 Outlook 启动时都会添加这些按钮(这是我愿意将我的 OTM 文件部署到的其他计算机所需要的)。有没有办法在添加按钮之前检查它是否已经存在?
回答by bonCodigo
You buttons is a part of the toolbar
. Hence check for the existance of the toolbar.
您的按钮是toolbar
. 因此检查工具栏是否存在。
If IsToolbar("Standard") Then
'-- do something
Else
'-- create tool bar and add the buttons
End If
Or try this:
或者试试这个:
For Each Contrl in Application.CommandBars("Standard").Controls
If .Caption <> "button1" then
'-- create it
End If
Next Contrl
EDIT as per OP's comment:
根据 OP 的评论编辑:
So let's stick to the error catching... (untested code, so you may have to give it a try in your end for exact correct syntax)
因此,让我们坚持捕获错误...(未经测试的代码,因此您可能最终需要尝试一下以获得准确正确的语法)
Dim ctlCBarControl As CommandBarControl
On Error Resume Next
Set ctlCBarControl = Application.CommandBars("Standard").Controls("button1")
If Err <> 0 Then
'-- no button exists, you may add it
Err = 0
Else
'-- the button is there..
End If
End if
* Reference: CommandBar Controls
* 参考:CommandBar 控件