我需要做两件事:
使用 Outlook 2013 VBA 将按钮添加到 Outlook 2013 功能区的“主页”选项卡上的自定义组。
- 我在网上找到的所有内容都是指 Excel 或 Word。
在单击每个按钮时运行的宏中,我希望能够告诉单击的按钮的名称。
- 我希望有可变数量的按钮,例如“Do 1”、“Do 2”、“Do 3”、...、“Do X”,它们中的每一个都将运行相同的宏/子并在宏/子内我可以看到按钮的名称,所以我知道该怎么做。否则我必须为每个按钮创建一个子/宏,我试图避免这种情况。
我需要做两件事:
使用 Outlook 2013 VBA 将按钮添加到 Outlook 2013 功能区的“主页”选项卡上的自定义组。
在单击每个按钮时运行的宏中,我希望能够告诉单击的按钮的名称。
唯一可能的方法是开发一个插件。Outlook 不允许使用 VBA 自定义 UI。
您可能会发现演练:使用功能区设计器创建自定义选项卡页面很有帮助。
我想我找到了!
对于 #1,请参见: 如何:仅使用 VBA 操作 Office Ribbon Bar。
对于#2,您需要在功能区定义 XML 文件中添加onAction子例程。
<mso:button id="MyButtonIdentifier1" label="MyMacroLabel" imageMso="HyperlinksVerify" onAction="NameOfMyMacro" visible="true"/>
NameOfMyMacro的定义应如下所示:
Sub NameOfMyMacro(control As IRibbonControl)
'here your logic
Select Case control.Id
Case "MyButtonIdentifier1"
'call another subroutine ;)
Case "MyButtonIdentifier2"
End Select
End Sub
有一种方法可以在 ADODB.Stream 的帮助下将自定义功能区添加到 Outlook 2013
多年来,我一直在工作中使用此解决方案 - 但我也无法在家中应用它。
首先,我正在准备一个包含 XML 结构的文本文件:
Dim Stream As Object
Dim FSO As FileSystemObject
Dim tsZwischenspeicher As TextStream
Set Stream = CreateObject("ADODB.Stream")
Set FSO = CreateObject("Scripting.FileSystemObject")
strPfad = "C:\Users\" & (Environ("username")) & "\AppData\Local\Microsoft\Office\"
strSpeicherpfad = strPfad & "olkexplorer.officeUI"
strTempSpeicherpfad = strPfad & "olkexplorer.temp"
...
tsZwischenspeicher.WriteLine Anführungszeichen("<mso:customUI xmlns:x1='http://schemas.microsoft.com/office/2009/07/customui/macro' xmlns:mso='http://schemas.microsoft.com/office/2009/07/customui'>")
tsZwischenspeicher.WriteLine Anführungszeichen("<mso:ribbon>") & vbCrLf
tsZwischenspeicher.WriteLine Anführungszeichen("<mso:qat>")
tsZwischenspeicher.WriteLine Anführungszeichen("<mso:sharedControls>")
tsZwischenspeicher.WriteLine Anführungszeichen("<mso:control idQ='mso:FilePrint' visible='false'/>")
然后生成的 XML 文件可以通过 ADODB.Stream 传输到 Outlook:
'Eine neue Fußzeile erstellen
tsZwischenspeicher.WriteLine Anführungszeichen("</mso:tabs>")
tsZwischenspeicher.WriteLine Anführungszeichen("</mso:ribbon>")
tsZwischenspeicher.WriteLine Anführungszeichen("</mso:customUI>")
'Zwischengespeicherte Datei schließen
tsZwischenspeicher.Close
Stream.Open
Stream.Type = 2 'text
Stream.Charset = "utf-8"
Stream.LoadFromFile strTempSpeicherpfad
FSO.OpenTextFile(strSpeicherpfad, 2, True, True).Write Stream.ReadText
Stream.Close
Outlook 必须重新启动,并且将显示新的功能区。
在 Outlook 2010、14.0.7232.5000 中工作:
在这个 Outlook 会话中:
Private WithEvents Button As Office.CommandBarButton
Private Sub Application_Startup()
Dim oExplorer As Outlook.Explorer
Set oExplorer = Application.ActiveExplorer
' Dynamically create button at Outlook startup (no need for XML file)
Set Button = CreateCommandBarButton(oExplorer.CommandBars)
end sub
Private Sub Button_Click(ByVal Ctrl As Office.CommandBarButton, CancelDefault As Boolean)
' Code to be executed upon clicking the button.
' The name of the function MUST be "buttonname_Click", with "buttonname"
' defined in Application_Startup().
MsgBox "Click: " & Ctrl.Caption
End Sub
Private Function CreateCommandBarButton(oBars As Office.CommandBars) As Office.CommandBarButton
On Error Resume Next
Dim oMenu As Office.CommandBar
Dim oBtn As Office.CommandBarButton
Const BAR_NAME As String = "YourCommandBarName"
Const CMD_NAME As String = "YourButtonName"
Set oMenu = oBars(BAR_NAME)
If oMenu Is Nothing Then
Set oMenu = oBars.Add(BAR_NAME, msoBarTop, , True)
Set oBtn = oMenu.Controls.Add(msoControlButton, , CMD_NAME, , True)
oBtn.Caption = CMD_NAME
oBtn.Tag = CMD_NAME
Else
Set oBtn = oMenu.FindControl(, , CMD_NAME)
If oBtn Is Nothing Then
Set oBtn = oMenu.Controls.Add(msoControlButton, , CMD_NAME, , True)
End If
End If
oMenu.Visible = True
Set CreateCommandBarButton = oBtn
End Function