【发布时间】:2014-01-29 10:57:02
【问题描述】:
我想显示一个新的功能区选项卡,该选项卡仅在我选择所需的形状时出现。我知道使用 Microsoft Office 的自定义 UI 编辑器或 VBA 使用以下示例制作普通选项卡:
Dim oToolbar As CommandBar
Dim oButton As CommandBarButton
Dim MyToolbar As String
' Give the toolbar a name
MyToolbar = "Kewl Tools"
On Error Resume Next
' so that it doesn't stop on the next line if the toolbar's already there
' Create the toolbar; PowerPoint will error if it already exists
Set oToolbar = CommandBars.Add(Name:=MyToolbar, _
Position:=msoBarFloating, Temporary:=True)
If Err.Number <> 0 Then
' The toolbar's already there, so we have nothing to do
Exit Sub
End If
On Error GoTo ErrorHandler
' Now add a button to the new toolbar
Set oButton = oToolbar.Controls.Add(Type:=msoControlButton)
' And set some of the button's properties
With oButton
.DescriptionText = "This is my first button"
'Tooltip text when mouse if placed over button
.Caption = "Do Button1 Stuff"
'Text if Text in Icon is chosen
.OnAction = "Button1"
'Runs the Sub Button1() code when clicked
.Style = msoButtonIcon
' Button displays as icon, not text or both
.FaceId = 52
' chooses icon #52 from the available Office icons
End With
' Repeat the above for as many more buttons as you need to add
' Be sure to change the .OnAction property at least for each new button
' You can set the toolbar position and visibility here if you like
' By default, it'll be visible when created. Position will be ignored in PPT 2007 and later
oToolbar.top = 150
oToolbar.left = 150
oToolbar.Visible = True
NormalExit:
Exit Sub ' so it doesn't go on to run the errorhandler code
ErrorHandler:
'Just in case there is an error
MsgBox Err.Number & vbCrLf & Err.Description
Resume NormalExit:
但我想让它在某些时刻出现和消失。例如,在 Powerpoint 中,当您选择一个视频时,会出现 2 个带有视频选项的新标签(格式和播放)。当您选择另一个不是视频的形状时,此选项卡会消失,其他选项卡会显示您选择的形状的正确选项,当您不选择任何形状时,这些特殊选项卡就会消失。
是否可以使用 VBA 制作?
【问题讨论】:
-
以上代码适用于 PPT 2003 及之前版本。它将在 2007 年及以后工作,但会在功能区的“加载项”选项卡中添加一个类似工具栏的东西,而不是在您可能需要的功能区上的新组。 CubeChase 在这里做了一个非常完整的答案。正如你所看到的,现在把东西放在丝带上已经完全不同了。 OTOH,如果功能区栏的加载项选项卡上的工具栏/按钮非常适合您,您可以应用一些相同的事件/选择更改代码来使加载项上的工具栏可见或不可见。不过,我怀疑你是否愿意。
标签: vba tabs powerpoint office-addins