gunns256
04-27-2005, 03:16 PM
Assumes existence of toolbar "Advanced"
Don't have one? Create one. Tools/Customize/Toolbars...New...
The code I am posting is unconventional: I'm using Outlook 2000 on an old machine, and I find that Outlook has become a memory pig with my 370 tasks and so on... I find that fewer object variables improves performance.
Alt-F11 to enter VB editor
double-click on left pane: folder Microsoft Outlook Objects
double-click on "This Outlook Session"
Paste this code (3 lines) which will run at every Outlook Startup:
Private Sub Application_Startup()
TestAddComboboxtoCommandBar
End Sub
Insert/Module if you've never coded before.
Paste this code (here to end) into the new module:
Sub UpdateContext(objSel As Selection)
Dim objItem As Object
Dim mycategory As String
mycategory = Application.ActiveExplorer.CommandBars.Item("Advanced").Controls.Item("Context").Text
'MsgBox (mycategory)
Application.ActiveExplorer.CommandBars.Item("Advanced").Controls.Item("Context").Text = "Context"
For Each objItem In objSel
If objItem.Class = olTask Then
objItem.Categories = ""
objItem.Categories = mycategory
objItem.Save
Else
MsgBox (objItem.Class)
End If
Next
Set objItem = Nothing
End Sub
Sub UpdateDefer(objSel As Selection)
Dim objItem As Object
Dim strdeferdate As String
Dim mydate As Date
strdeferdate = Application.ActiveExplorer.CommandBars.Item("Advanced").Controls.Item("Defer").Text
Application.ActiveExplorer.CommandBars.Item("Advanced").Controls.Item("Defer").Text = "Defer"
'MsgBox (strdeferdate)
For Each objItem In objSel
mydate = objItem.DueDate
If mydate = #1/1/4501# Then mydate = Date
If objItem.Class = olTask Then
If strdeferdate = "None" Then
objItem.DueDate = #1/1/4501# 'esoterica. look it up.
Else
'these are good for me because I'm a teacher
If strdeferdate = "1 week" Then mydate = DateAdd("ww", 1, mydate)
If strdeferdate = "1 month" Then mydate = DateAdd("m", 1, mydate)
If strdeferdate = "End June" Then mydate = DateValue("6/25")
If strdeferdate = "End August" Then mydate = DateValue("8/25")
If mydate < Date Then mydate = mydate + 365
objItem.DueDate = mydate
End If
objItem.Save
Else
MsgBox (objItem.Class)
End If
Next
Set objItem = Nothing
End Sub
Private Function SelectionAction()
Dim objSelection As Selection
Dim blnDoIt As Boolean
Dim intMaxItems As Integer
Dim intOKToExceedMax As Integer
Dim strMsg As String
intMaxItems = 5
Set objSelection = Application.ActiveExplorer.Selection
Select Case objSelection.Count
Case 0
strMsg = "No items were selected"
MsgBox strMsg, , "No selection"
blnDoIt = False
Case Is > intMaxItems
strMsg = "You selected " & _
objSelection.Count & " items. " & _
"Do you really want to process " & _
"that large a selection?"
intOKToExceedMax = MsgBox( _
Prompt:=strMsg, _
Buttons:=vbYesNo + vbDefaultButton2, _
Title:="Selection exceeds maximum")
If intOKToExceedMax = vbYes Then
blnDoIt = True
Else
blnDoIt = False
End If
Case Else
blnDoIt = True
End Select
If blnDoIt = True Then
If Application.ActiveExplorer.CommandBars.Item("Advanced").Controls.Item("Projects").Text <> "Projects" Then
Call UpdateProject(objSelection)
End If
If Application.ActiveExplorer.CommandBars.Item("Advanced").Controls.Item("Defer").Text <> "Defer" Then
Call UpdateDefer(objSelection)
End If
If Application.ActiveExplorer.CommandBars.Item("Advanced").Controls.Item("Context").Text <> "Context" Then
Call UpdateContext(objSelection)
End If
End If
Set objSelection = Nothing
'Set objApp = Nothing
End Function
Private Function AddComboBoxToCommandBar(ByVal strCommandBarName As String, _
ByVal strComboBoxCaption As String, _
ByRef strChoices() As String) As Boolean
' Purpose: Adds a combo box to a command bar.
' Accepts:
' strCommandBarName: The name of the command bar to add the combo box.
' strChoices(): An array of combo box choices.
' Returns: True if the combo box was successfully added to the command bar.
Dim objCommandBarControl As Office.CommandBarControl
Dim objCommandBarComboBox As Office.CommandBarComboBox
Dim varChoice As Variant
On Error GoTo AddComboBoxToCommandBar_Err
Application.ActiveExplorer.CommandBars.Item(strCom mandBarName).Visible = True
' Delete any previously-added instances of this combo box.
' Replace the next line of code with:
For Each objCommandBarControl In _
Application.ActiveExplorer.CommandBars.Item(strCom mandBarName).Controls
_
'<- For Outlook
' For Each objCommandBarControl In _
' Application.VBE.CommandBars.Item(strCommandBarName ).Controls _
<- For Visual Basic Editor
'For Each objCommandBarControl In Application.CommandBars.Item(strCommandBarName).Co ntrols
If objCommandBarControl.Caption = strComboBoxCaption Then
objCommandBarControl.Delete
End If
Next objCommandBarControl
' Create the combo box.
' Replace the next line of code with:
' Set objCommandBarComboBox = _
' Application.CommandBars.Item(strCommandBarName).Co ntrols.Add(msoControlComboBox) _
<- For Outlook
' Set objCommandBarComboBox = _
' Application.CommandBars.Item(strCommandBarName).Co ntrols.Add(msoControlComboBox) _
<- For Visual Basic Editor
Set objCommandBarComboBox = _
Application.ActiveExplorer.CommandBars.Item(strCom mandBarName).Controls.Add(msoControlComboBox)
objCommandBarComboBox.Caption = strComboBoxCaption
For Each varChoice In strChoices
objCommandBarComboBox.AddItem varChoice
Next varChoice
If strComboBoxCaption = "Defer" Then
objCommandBarComboBox.Text = "Defer"
objCommandBarComboBox.Width = 100
objCommandBarComboBox.OnAction = "SelectionAction"
End If
If strComboBoxCaption = "Context" Then
objCommandBarComboBox.Text = "Context"
objCommandBarComboBox.Width = 100
objCommandBarComboBox.OnAction = "SelectionAction"
End If
AddComboBoxToCommandBar_End:
AddComboBoxToCommandBar = True
Exit Function
AddComboBoxToCommandBar_Err:
AddComboBoxToCommandBar = False
MsgBox ("addcomboboxtocommandbar.error")
End Function
'called from This Outlook Session, adds comboboxes by calling 'AddComboBoxToCommandBar
Public Sub TestAddComboBoxToCommandBar()
Set mynamespace = Application.GetNamespace("MAPI")
Set myfolders = mynamespace.Folders
Set myfolder = myfolders.Item(1).Folders.Item("Projects")
Dim projcollection As Collection
Dim strChoices() As String
ReDim strChoices(6)
'change number if you want more/less items
strChoices(1) = "Defer"
strChoices(2) = "1 week"
strChoices(3) = "1 month"
strChoices(4) = "End June"
strChoices(5) = "End August"
strChoices(6) = "None"
Call AddComboBoxToCommandBar("Advanced", "Defer", strChoices)
ReDim strChoices(11)
strChoices(1) = "Context"
strChoices(2) = "@Agenda"
strChoices(3) = "@Computer"
strChoices(4) = "@Errand"
strChoices(5) = "@Home"
strChoices(6) = "@Phone"
strChoices(7) = "@School"
strChoices(8) = "@Transfer"
strChoices(9) = "@Waiting"
strChoices(10) = "<Someday"
strChoices(11) = ">Goals"
Call AddComboBoxToCommandBar("Advanced", "Context", strChoices)
Set myfolder = Nothing
Set myfolders = Nothing
Set mynamespace = Nothing
End Sub
Don't have one? Create one. Tools/Customize/Toolbars...New...
The code I am posting is unconventional: I'm using Outlook 2000 on an old machine, and I find that Outlook has become a memory pig with my 370 tasks and so on... I find that fewer object variables improves performance.
Alt-F11 to enter VB editor
double-click on left pane: folder Microsoft Outlook Objects
double-click on "This Outlook Session"
Paste this code (3 lines) which will run at every Outlook Startup:
Private Sub Application_Startup()
TestAddComboboxtoCommandBar
End Sub
Insert/Module if you've never coded before.
Paste this code (here to end) into the new module:
Sub UpdateContext(objSel As Selection)
Dim objItem As Object
Dim mycategory As String
mycategory = Application.ActiveExplorer.CommandBars.Item("Advanced").Controls.Item("Context").Text
'MsgBox (mycategory)
Application.ActiveExplorer.CommandBars.Item("Advanced").Controls.Item("Context").Text = "Context"
For Each objItem In objSel
If objItem.Class = olTask Then
objItem.Categories = ""
objItem.Categories = mycategory
objItem.Save
Else
MsgBox (objItem.Class)
End If
Next
Set objItem = Nothing
End Sub
Sub UpdateDefer(objSel As Selection)
Dim objItem As Object
Dim strdeferdate As String
Dim mydate As Date
strdeferdate = Application.ActiveExplorer.CommandBars.Item("Advanced").Controls.Item("Defer").Text
Application.ActiveExplorer.CommandBars.Item("Advanced").Controls.Item("Defer").Text = "Defer"
'MsgBox (strdeferdate)
For Each objItem In objSel
mydate = objItem.DueDate
If mydate = #1/1/4501# Then mydate = Date
If objItem.Class = olTask Then
If strdeferdate = "None" Then
objItem.DueDate = #1/1/4501# 'esoterica. look it up.
Else
'these are good for me because I'm a teacher
If strdeferdate = "1 week" Then mydate = DateAdd("ww", 1, mydate)
If strdeferdate = "1 month" Then mydate = DateAdd("m", 1, mydate)
If strdeferdate = "End June" Then mydate = DateValue("6/25")
If strdeferdate = "End August" Then mydate = DateValue("8/25")
If mydate < Date Then mydate = mydate + 365
objItem.DueDate = mydate
End If
objItem.Save
Else
MsgBox (objItem.Class)
End If
Next
Set objItem = Nothing
End Sub
Private Function SelectionAction()
Dim objSelection As Selection
Dim blnDoIt As Boolean
Dim intMaxItems As Integer
Dim intOKToExceedMax As Integer
Dim strMsg As String
intMaxItems = 5
Set objSelection = Application.ActiveExplorer.Selection
Select Case objSelection.Count
Case 0
strMsg = "No items were selected"
MsgBox strMsg, , "No selection"
blnDoIt = False
Case Is > intMaxItems
strMsg = "You selected " & _
objSelection.Count & " items. " & _
"Do you really want to process " & _
"that large a selection?"
intOKToExceedMax = MsgBox( _
Prompt:=strMsg, _
Buttons:=vbYesNo + vbDefaultButton2, _
Title:="Selection exceeds maximum")
If intOKToExceedMax = vbYes Then
blnDoIt = True
Else
blnDoIt = False
End If
Case Else
blnDoIt = True
End Select
If blnDoIt = True Then
If Application.ActiveExplorer.CommandBars.Item("Advanced").Controls.Item("Projects").Text <> "Projects" Then
Call UpdateProject(objSelection)
End If
If Application.ActiveExplorer.CommandBars.Item("Advanced").Controls.Item("Defer").Text <> "Defer" Then
Call UpdateDefer(objSelection)
End If
If Application.ActiveExplorer.CommandBars.Item("Advanced").Controls.Item("Context").Text <> "Context" Then
Call UpdateContext(objSelection)
End If
End If
Set objSelection = Nothing
'Set objApp = Nothing
End Function
Private Function AddComboBoxToCommandBar(ByVal strCommandBarName As String, _
ByVal strComboBoxCaption As String, _
ByRef strChoices() As String) As Boolean
' Purpose: Adds a combo box to a command bar.
' Accepts:
' strCommandBarName: The name of the command bar to add the combo box.
' strChoices(): An array of combo box choices.
' Returns: True if the combo box was successfully added to the command bar.
Dim objCommandBarControl As Office.CommandBarControl
Dim objCommandBarComboBox As Office.CommandBarComboBox
Dim varChoice As Variant
On Error GoTo AddComboBoxToCommandBar_Err
Application.ActiveExplorer.CommandBars.Item(strCom mandBarName).Visible = True
' Delete any previously-added instances of this combo box.
' Replace the next line of code with:
For Each objCommandBarControl In _
Application.ActiveExplorer.CommandBars.Item(strCom mandBarName).Controls
_
'<- For Outlook
' For Each objCommandBarControl In _
' Application.VBE.CommandBars.Item(strCommandBarName ).Controls _
<- For Visual Basic Editor
'For Each objCommandBarControl In Application.CommandBars.Item(strCommandBarName).Co ntrols
If objCommandBarControl.Caption = strComboBoxCaption Then
objCommandBarControl.Delete
End If
Next objCommandBarControl
' Create the combo box.
' Replace the next line of code with:
' Set objCommandBarComboBox = _
' Application.CommandBars.Item(strCommandBarName).Co ntrols.Add(msoControlComboBox) _
<- For Outlook
' Set objCommandBarComboBox = _
' Application.CommandBars.Item(strCommandBarName).Co ntrols.Add(msoControlComboBox) _
<- For Visual Basic Editor
Set objCommandBarComboBox = _
Application.ActiveExplorer.CommandBars.Item(strCom mandBarName).Controls.Add(msoControlComboBox)
objCommandBarComboBox.Caption = strComboBoxCaption
For Each varChoice In strChoices
objCommandBarComboBox.AddItem varChoice
Next varChoice
If strComboBoxCaption = "Defer" Then
objCommandBarComboBox.Text = "Defer"
objCommandBarComboBox.Width = 100
objCommandBarComboBox.OnAction = "SelectionAction"
End If
If strComboBoxCaption = "Context" Then
objCommandBarComboBox.Text = "Context"
objCommandBarComboBox.Width = 100
objCommandBarComboBox.OnAction = "SelectionAction"
End If
AddComboBoxToCommandBar_End:
AddComboBoxToCommandBar = True
Exit Function
AddComboBoxToCommandBar_Err:
AddComboBoxToCommandBar = False
MsgBox ("addcomboboxtocommandbar.error")
End Function
'called from This Outlook Session, adds comboboxes by calling 'AddComboBoxToCommandBar
Public Sub TestAddComboBoxToCommandBar()
Set mynamespace = Application.GetNamespace("MAPI")
Set myfolders = mynamespace.Folders
Set myfolder = myfolders.Item(1).Folders.Item("Projects")
Dim projcollection As Collection
Dim strChoices() As String
ReDim strChoices(6)
'change number if you want more/less items
strChoices(1) = "Defer"
strChoices(2) = "1 week"
strChoices(3) = "1 month"
strChoices(4) = "End June"
strChoices(5) = "End August"
strChoices(6) = "None"
Call AddComboBoxToCommandBar("Advanced", "Defer", strChoices)
ReDim strChoices(11)
strChoices(1) = "Context"
strChoices(2) = "@Agenda"
strChoices(3) = "@Computer"
strChoices(4) = "@Errand"
strChoices(5) = "@Home"
strChoices(6) = "@Phone"
strChoices(7) = "@School"
strChoices(8) = "@Transfer"
strChoices(9) = "@Waiting"
strChoices(10) = "<Someday"
strChoices(11) = ">Goals"
Call AddComboBoxToCommandBar("Advanced", "Context", strChoices)
Set myfolder = Nothing
Set myfolders = Nothing
Set mynamespace = Nothing
End Sub