I need to add new item to the hyperlink menu in Outlook 2007 (now it contains items like Copy, Select Hyperlink, Open Hyperlink, Copy Hyperlink, Who is).
How can I do it? (it should be a VBA macro added 开发者_StackOverflowto Outlook)
Looks like there is no way to do the same with VBA. Even MS doesn't have an answer.
The definitive article on Outlook and CommandBar objects is here.
The code at the end of the article that you can run in Excel to get a listing of all the CommandBar objects is following. Note that you have to have the Outlook Library in the References in VBA to get this to run:
Option Explicit
Dim oOutApp As Outlook.Application
Dim I As Long
Dim iRowCount As Long
Dim oItm As Object ' so it'll handle varying item types
Dim oSheet As Excel.Worksheet
Dim oNS As Outlook.NameSpace
Dim oFld As Outlook.MAPIFolder
Sub GetOutlookCommandBarIDs()
If MsgBox("This will clear the current worksheet, OK to continue?", vbOKCancel) = 1 Then
Cells.Select
Selection.ClearContents
iRowCount = 0
Set oSheet = ActiveSheet
Set oOutApp = New Outlook.Application
Set oNS = oOutApp.Session
Set oItm = oOutApp.CreateItem(olMailItem)
GetInspectorIDs oItm, "Mail Message"
Set oItm = oOutApp.CreateItem(olPostItem)
GetInspectorIDs oItm, "Post"
Set oItm = oOutApp.CreateItem(olContactItem)
GetInspectorIDs oItm, "Contact"
Set oItm = oOutApp.CreateItem(olDistributionListItem)
GetInspectorIDs oItm, "Distribution List"
Set oItm = oOutApp.CreateItem(olAppointmentItem)
GetInspectorIDs oItm, "Appointment"
Set oItm = oOutApp.CreateItem(olTaskItem)
GetInspectorIDs oItm, "Task"
Set oItm = oOutApp.CreateItem(olJournalItem)
GetInspectorIDs oItm, "Journal Entry"
Set oFld = oNS.GetDefaultFolder(olFolderInbox)
GetExplorerIDs oFld, "Mail Folder"
Set oFld = oNS.GetDefaultFolder(olFolderContacts)
GetExplorerIDs oFld, "Contact Folder"
Set oFld = oNS.GetDefaultFolder(olFolderCalendar)
GetExplorerIDs oFld, "Calendar Folder"
Set oFld = oNS.GetDefaultFolder(olFolderTasks)
GetExplorerIDs oFld, "Task Folder"
Set oFld = oNS.GetDefaultFolder(olFolderJournal)
GetExplorerIDs oFld, "Journal Folder"
Set oFld = oNS.GetDefaultFolder(olFolderNotes)
GetExplorerIDs oFld, "Notes Folder"
Selection.AutoFilter
Cells.Select
Cells.EntireColumn.AutoFit
Range("A1").Select
MsgBox "The spreadsheet is complete."
End If
End Sub
Sub GetInspectorIDs(oItm, sType As String)
Dim oCBs As Office.CommandBars
Dim oCtl As Office.CommandBarControl
Set oCBs = oItm.GetInspector.CommandBars
For I = 1 To 35000
Set oCtl = oCBs.FindControl(, I)
If Not (oCtl Is Nothing) Then
iRowCount = iRowCount + 1
oSheet.Cells(iRowCount, 1) = "Inspector"
oSheet.Cells(iRowCount, 2) = sType
oSheet.Cells(iRowCount, 3) = oCtl.Parent.Name
oSheet.Cells(iRowCount, 4) = oCtl.Caption
oSheet.Cells(iRowCount, 5) = CStr(I)
End If
Next
End Sub
Sub GetExplorerIDs(oFld As Outlook.MAPIFolder, sType As String)
Dim oCBs As Office.CommandBars
Dim sFilter As String
Dim oCtl As Office.CommandBarControl
Set oCBs = oFld.GetExplorer.CommandBars
For I = 1 To 35000
Set oCtl = oCBs.FindControl(, I)
If Not (oCtl Is Nothing) Then
iRowCount = iRowCount + 1
oSheet.Cells(iRowCount, 1) = "Explorer"
oSheet.Cells(iRowCount, 2) = sType
oSheet.Cells(iRowCount, 3) = oCtl.Parent.Name
oSheet.Cells(iRowCount, 4) = oCtl.Caption
oSheet.Cells(iRowCount, 5) = CStr(I)
End If
Next
End Sub
The excerpt of the output that will help you is:
Inspector Mail Message Insert Te&xt Box 139
Inspector Mail Message Insert &Symbol... 308
Inspector Mail Message Insert &Tip Wizard 5 345
Inspector Mail Message Insert &Object... 546
Inspector Mail Message Insert Boo&kmark... 758
Inspector Mail Message Insert Date and &Time... 768
Inspector Mail Message Insert &Field... 772
Inspector Mail Message Insert Attach 1079
Inspector Mail Message Insert &Hyperlink... 1576
Inspector Mail Message Insert New Co&mment 1589
Inspector Mail Message Insert It&em... 2505
Inspector Mail Message Insert &Remove Hyperlink 3626
Inspector Mail Message Insert &Calendar... 11496
Inspector Mail Message Insert &Picture 30180
So you'd access it something like:
Dim oItm As Object
Dim oExp As Outlook.Explorer
Dim oBar As Office.CommandBar
Dim oOutApp As Object
Set oOutApp = CreateObject("Outlook.Application")
Set oItm = oOutApp.CreateItem(olMailItem)
Set oExp = Outlook.ActiveExplorer
Set oBar = oItm.GetInspector.CommandBars.Item("Insert")
oBar.Controls.Add (blahblahblah)
Note: I was only able to test things in Outlook 2010.
looked at http://msdn.microsoft.com/en-us/library/bb206754(v=office.12).aspx ?
Sorry I don't have a definite answer for you, but hopefully I can point you in the right direction.
This forum will hopefully be useful, it looks like they're doing the same thing there (albeit with a different menu).
And if you need some basics help, here are two MSDN blogs talking about Outlook and VBA macros in general:
http://blogs.msdn.com/b/synergist/archive/2007/05/23/adding-a-vba-macro-to-outlook.aspx
http://blogs.msdn.com/b/swiss_dpe_team/archive/2007/12/11/office-2007-outlook2007-macros-vba-how-to-work-better-with-categories.aspx
Hope this helps!
精彩评论