Get a list of all the toolbars currently displaying in Outlook

G

George Hester

I would like to make a Sub that will return all the toolbars in Outlook
currently displaying with VBA. The reason why I'd like to do this is
because I have a COM called SpamBayes whose toolbar appears in the Outlook
User Interface. The way this works is it inspects each New Mail and if it
satisfies the criterion that the New Mail is spam it sends the e-mail to a
folder called Junk E-Mail. I have set up some code so that the ItemAdd
event for that folder will delete it entirely if the e-mail goes to that
folder. Works well.

But if some e-mail comes in that SpamBayes is unsure about the e-mail will
go to a folder called Junk Suspects. Then in the toolbar provided by
SpamBayes with the suspect e-mail selected, I can hit a buttom called Spam
which sends the e-mail to the Junk E-Mail folder and the Bayesian criteria
is satisfied. If it is not Spam I hit the button in the toolbar called Not
Spam and the selected e-mail is put back in the Inbox. Both these buttons
appear when the e-mail is in the Junk Suspects and is in that current folder
but otherwise the button Spam always appears.

What I would like to do is push the button Spam programatically that the
selected e-mail (the item) is Junk E-Mail. It's ok this thing works pretty
good so that 99% of all suspected e-mail is in fact Spam.

But I think I need to know what the caption is for this toolbar. If I know
that maybe I can hit the button programatically which will set the e-mail as
spam and my other program will get rid of it. So is there a way to get a
description (the caption) of each of the toolbars currently displaying in
Outlook? Thanks.
 
M

Michael Bauer [MVP - Outlook]

Open one e-mail and look for the comandbars name. In code you can then
access the bar with:

Dim cb as Office.Commandbar

set cb=Application.ActiveExplorer.Selection(1).Commandbars("TheName")

If the bar exists and is visible then get its button 1 or 2 and call its
Execute method.

--
Viele Gruesse / Best regards
Michael Bauer - MVP Outlook
Keep your Outlook categories organized!
http://www.shareit.com/product.html?productid=300120654&languageid=1
(German: http://www.VBOffice.net/product.html?pub=6)

Am Wed, 24 Jan 2007 17:41:11 -0500 schrieb George Hester:
 
G

George Hester

Thanks Michael for the response. The toolbar appears in the Outlook main
window itself not in the toolbars that appear when an e-mail is opened.
Will that make a difference? SpamBayes allows me to assign an e-mail is or
is not Spam without even opening the e-mail itself. In fact what I have
been doing is just looking at the Internet Headers to determine the
Spamlihood of the e-mail. Sometines not even Internet Headers. Like for
example if there is an attachment in the e-mail. Anything sent to me with
an attachment that I have not arranged for is 99% likely Spam. I am
familiar with what you provided I just can't seem to get a listing
programitcally of the toolbars in the main Outlook window for that is where
the SpamBayes toolbar is.
 
M

Michael Bednarek

I would like to make a Sub that will return all the toolbars in Outlook
currently displaying with VBA.
[snip]

I had this lying around:

Sub EnumerateControls()

Dim myCB As CommandBar
Dim myCtrl As CommandBarControl

For Each myCB In ActiveExplorer.CommandBars
Debug.Print "+++ CommandBar: " + myCB.Name + "+++"
For Each myCtrl In myCB.Controls
Debug.Print " --- Control: " + myCtrl.Caption + "---"
Next myCtrl
Next myCB
Debug.Print "End of CommandBars and Controls for ActiveExplorer"

For Each myCB In ActiveInspector.CommandBars
Debug.Print "+++ CommandBar: " + myCB.Name + "+++"
For Each myCtrl In myCB.Controls
Debug.Print " --- Control: " + myCtrl.Caption + "---"
Next myCtrl
Next myCB
Debug.Print "End of CommandBars and Controls for ActiveInspector"

End Sub

The second part obviously works only if you have an Inspector open.
 
G

George Hester

thanks again Michael. I found this page at Microsoft:

http://www.microsoft.com/technet/prodtechnol/office/office2000/proddocs/opg/
part2/ch06.mspx

this talks about the CommandBar near the middle in the function
CBPrintCBarInfo. But it uses two functions not in the page; CBGetCBType and
another CBGetCBCtlType. Maybe I am making this more complicated than it has
to be but if you know these functions it would help me immensely if you
could post them. Thanks.
 
M

Michael Bauer [MVP - Outlook]

Yes, you make it much to complicated :) You could also right click on the
toolbar and see the bars' names, they won't change. Then use the code I've
provided, either with ActiveExplorer.Commandbars or
ActiveInspector.Commandbars.


--
Viele Gruesse / Best regards
Michael Bauer - MVP Outlook
Keep your Outlook categories organized!
http://www.shareit.com/product.html?productid=300120654&languageid=1
(German: http://www.VBOffice.net/product.html?pub=6)


Am Sat, 27 Jan 2007 14:10:56 -0500 schrieb George Hester:
 
G

George Hester

yes I got it all going. Actually the typing helped me to identify the
buttons Popup and such. It is always a good idea to know the names of the
things we are working with.

Anyway I have run into a snag. The first time I tried it I got a messige
"No filterd item was selected." What this means is although I am using the
ItemAdd event in the folder Junk Suspects and you would think the argument
of the ItemAdd event which is Item As Object you would think that implies
the item is Selected. But no.

So I did fix that but this is what happened. I move the spam from the
more_spam folder to Junk Suspects to initiate the Item_Add event for
JunkSuspects. Then I use the Selection Collection to select that added
MailItem in Jiunk Suspects. But when I do that remember I am still in the
folder more_spam. When I move a MailItem out of there the next message in
that folder gets selected for moving. Hence it doesn't work right. This is
what happens\.

The message I move to Junk Suspects stays in the folder Junk Suspects and
the message in more_spam that is selected by default gets selected and moved
to Junk E-Mail. Remember I do not want to open these e-mails at all. So
now I am stuck selecting two messages at once and I fon't want that. Here
is the sub:

Option Explicit
Public Const ERR_INVALID_CMDBARNAME As Long = &H5
Public Sub CBPrintCBarInfo(strCBarName As String)
On Error GoTo CBPrintCBarInfo_Err
Dim oOLApp As Outlook.Application
Dim oeExplorer As Outlook.Explorer
Dim oscSelection As Outlook.Selection
Dim cbrBar As Office.CommandBar
Dim ctlCBarControl As Office.CommandBarControl
Dim oMS1 As Outlook.MailItem
Dim iFor As Integer
Set oOLApp = CreateObject("Outlook.Application")
Set oeExplorer = oOLApp.ActiveExplorer
Set oscSelection = oeExplorer.Selection
Set cbrBar = oeExplorer.CommandBars(strCBarName)

For iFor = 1 To oscSelection.Count 'I think the problem is here and 2
lines below it.
'If I use 1 here
then I have no control over what gets selected first
If oscSelection.Item(iFor).Class = olMail Then
Set oMS1 = oscSelection.Item(iFor)
For Each ctlCBarControl In cbrBar.Controls
If ctlCBarControl.Caption = "Spam" Then
ctlCBarControl.Execute
End If
Next ctlCBarControl
End If
Next iFor
CBPrintCBarInfo_End:
Set cbrBar = Nothing
Set oMS1 = Nothing
Set oscSelection = Nothing
Set oeExplorer = Nothing
Set oOLApp = Nothing
Exit Sub
CBPrintCBarInfo_Err:
Select Case Err.Number
Case ERR_INVALID_CMDBARNAME
MsgBox "'" & strCBarName & _
"' is not a valid commad bar name!"
Case Else
MsgBox "Error: " & Err.Number _
& " - " & Err.Description
End Select
Err.Clear
Resume CBPrintCBarInfo_End
End Sub

As I was typing this I tried to use EntryID of the mail that was moved but
it seems that when it is selected it gets changed.
 
G

George Hester

ok I got it I think. I wasn't using the Explorer object corrrectly. Here
is the New and Improved version:

Option Explicit
Public Const ERR_INVALID_CMDBARNAME As Long = &H5
Public Sub CBPrintCBarInfo(oFld As MAPIFolder, oMS As MailItem, strCBarName
As String)
On Error GoTo CBPrintCBarInfo_Err
Dim oOLApp As Outlook.Application
Dim oeExplorer As Outlook.Explorer
Dim oscSelection As Outlook.Selection
Dim cbrBar As Office.CommandBar
Dim ctlCBarControl As Office.CommandBarControl
Dim oMS1 As Outlook.MailItem
Dim sEntID As String
Dim iFor As Integer
sEntID = oMS.EntryID
Set oOLApp = CreateObject("Outlook.Application")
Set oeExplorer = oFld.GetExplorer(olFolderDisplayNoNavigation)
Set oscSelection = oeExplorer.Selection
Set cbrBar = oeExplorer.CommandBars(strCBarName)

For iFor = 1 To oscSelection.Count
If oscSelection.Item(iFor).Class = olMail Then
Set oMS1 = oscSelection.Item(iFor)
If oMS1.EntryID = sEntID Then
For Each ctlCBarControl In cbrBar.Controls
If ctlCBarControl.Caption = "Spam" Then
ctlCBarControl.Execute
End If
Next ctlCBarControl
End If
End If
Next iFor
CBPrintCBarInfo_End:
Set cbrBar = Nothing
Set oMS1 = Nothing
Set oscSelection = Nothing
Set oeExplorer = Nothing
Set oOLApp = Nothing
Exit Sub
CBPrintCBarInfo_Err:
Select Case Err.Number
Case ERR_INVALID_CMDBARNAME
MsgBox "'" & strCBarName & _
"' is not a valid commad bar name!"
Case Else
MsgBox "Error: " & Err.Number _
& " - " & Err.Description
End Select
Err.Clear
Resume CBPrintCBarInfo_End
End Sub
 
G

George Hester

Splat. Worked once now it don't. All I want to do is select the item I put
in Spam Suspects have the command bar button execute and send it off to Junk
E-mail. Without navigating to Spam Suspects at all.
 
G

George Hester

I chucked the Selection collection and just arranged the correct MAPIFolder
in the Sub. New one works so far.
Oh I also put the Sub in the Class.

Private Sub CBPrintCBarInfo(oMS As MailItem, strCBarName As String)
On Error GoTo CBPrintCBarInfo_Err
Dim olApp As Outlook.Application
Dim oNS1 As Outlook.NameSpace
Dim oeExplorer As Outlook.Explorer
Dim cbrBar As Office.CommandBar
Dim ctlCBarControl As Office.CommandBarControl
Dim oPersonFolders As Outlook.Folders
Dim oFolder As Outlook.MAPIFolder
Dim sEntID As String
Dim sSubject As String

Set olApp = CreateObject("Outlook.Application")
Set oNS1 = olApp.GetNamespace("MAPI")
Set oPersonFolders = oNS1.Folders.GetFirst.Folders
Set oFolder = oPersonFolders.GetFirst
Do Until oFolder Is Nothing
If oFolder.Name = "Junk Suspects" Then Exit Do
Set oFolder = oPersonFolders.GetNext
Loop
Set oPersonFolders = Nothing

sEntID = oMS.EntryID

Set oeExplorer = oFolder.GetExplorer
Set cbrBar = oeExplorer.CommandBars(strCBarName)

sSubject = "oMS.Subject = " & oMS.Subject
For Each ctlCBarControl In cbrBar.Controls
If ctlCBarControl.Caption = "Spam" Then
ctlCBarControl.Execute
End If
Next ctlCBarControl
CBPrintCBarInfo_End:
Set cbrBar = Nothing
Set oFolder = Nothing
Set oNS1 = Nothing
Set oeExplorer = Nothing
Set olApp = Nothing
Exit Sub
CBPrintCBarInfo_Err:
Select Case Err.Number
Case ERR_INVALID_CMDBARNAME
MsgBox "'" & strCBarName & _
"' is not a valid commad bar name!"
Case Else
MsgBox "Error: " & Err.Number _
& " - " & Err.Description
End Select
Err.Clear
Resume CBPrintCBarInfo_End
End Sub

Now the only problem with this so far is Outlook loses focus on the desktop.
That sucks but I guess it is a result of switching explorer.
--

George Hester
_________________________________
George Hester said:
Splat. Worked once now it don't. All I want to do is select the item I put
in Spam Suspects have the command bar button execute and send it off to Junk
E-mail. Without navigating to Spam Suspects at all.

--

George Hester
_________________________________
George Hester said:
ok I got it I think. I wasn't using the Explorer object corrrectly. Here
is the New and Improved version:

Option Explicit
Public Const ERR_INVALID_CMDBARNAME As Long = &H5
Public Sub CBPrintCBarInfo(oFld As MAPIFolder, oMS As MailItem, strCBarName
As String)
On Error GoTo CBPrintCBarInfo_Err
Dim oOLApp As Outlook.Application
Dim oeExplorer As Outlook.Explorer
Dim oscSelection As Outlook.Selection
Dim cbrBar As Office.CommandBar
Dim ctlCBarControl As Office.CommandBarControl
Dim oMS1 As Outlook.MailItem
Dim sEntID As String
Dim iFor As Integer
sEntID = oMS.EntryID
Set oOLApp = CreateObject("Outlook.Application")
Set oeExplorer = oFld.GetExplorer(olFolderDisplayNoNavigation)
Set oscSelection = oeExplorer.Selection
Set cbrBar = oeExplorer.CommandBars(strCBarName)

For iFor = 1 To oscSelection.Count
If oscSelection.Item(iFor).Class = olMail Then
Set oMS1 = oscSelection.Item(iFor)
If oMS1.EntryID = sEntID Then
For Each ctlCBarControl In cbrBar.Controls
If ctlCBarControl.Caption = "Spam" Then
ctlCBarControl.Execute
End If
Next ctlCBarControl
End If
End If
Next iFor
CBPrintCBarInfo_End:
Set cbrBar = Nothing
Set oMS1 = Nothing
Set oscSelection = Nothing
Set oeExplorer = Nothing
Set oOLApp = Nothing
Exit Sub
CBPrintCBarInfo_Err:
Select Case Err.Number
Case ERR_INVALID_CMDBARNAME
MsgBox "'" & strCBarName & _
"' is not a valid commad bar name!"
Case Else
MsgBox "Error: " & Err.Number _
& " - " & Err.Description
End Select
Err.Clear
Resume CBPrintCBarInfo_End
End Sub

--

George Hester
_________________________________
on
the
http://www.microsoft.com/technet/prodtechnol/office/office2000/proddocs/opg/ than
 
M

Michael Bauer [MVP - Outlook]

For the focus you could adapt the following sample. Copy FindWindow,
SetForegroundWindow and GetInspectorHandle:
http://www.vboffice.net/sample.html?mnu=2&smp=20&cmd=showitem

Call GetInspectorHandle with the caption of the Explorer and then call with
that result (if it's not 0) SetForegroundWindow.

--
Viele Gruesse / Best regards
Michael Bauer - MVP Outlook
Keep your Outlook categories organized!
http://www.shareit.com/product.html?productid=300120654&languageid=1
(German: http://www.VBOffice.net/product.html?pub=6)


Am Mon, 29 Jan 2007 21:49:35 -0500 schrieb George Hester:
 
G

George Hester

Oh neat Michael I always like adopting new things. Especially Windows
functions. I will see if I can get that to work to bring the focus back.
So far I am out of Spam right now but hey that's the whole idea eh? I
finally got these Spammers under control. I plan on putting the IP address
of the unsecured SMTP servers in a database and rather than entering them in
manually. I really wish Microsoft would bring back the ability to read the
headers of received e-mail. For me anyway the fact that we can't natively
really keeps me from moving to the latest and greatest versions of Outlook.
 
G

George Hester

Hi Michael I didn't finish getting all <modBodyFocus.bas> in yet I just
wanted to ask you a few questions.

In the function GetBodyHandle under the Case for Outlook 2000 (version 9) at
the bottom of that section the last If lRes Then contains a comment. It
looks similar to what you have for version 11 in the sense that what you
have in the comment, is similar to the second parameters for the GetWindow
function calls in the Case version 11 area. I am just wondering if the
comment in the Case version 9 area is ok just to go by?

The other question is that the GetBodyHandle doesn't do anything for the
Outlook 2002 (version 10) case. Is that right or can we just put Case 9
down as a Case Else instead?

Thanks.
 
M

Michael Bauer [MVP - Outlook]

As I remember it means: in OL 2000 the first child is either a RichEdit20A
class or Internet Explorer_Server, depending on the format. Whereas in OL
2003 there're more siblings and I called FindChildClassName instead. But
frankly, I'm not sure and not willing to start a VPC now with OL 2000 to
verify it. I've never heard that it doesn't work.

Case 9 wouldn't work for OL XP. Once I wrote that sample I only had OL XP
and 2003 installed, so I wasn't able to handle that case. As I remember
know, it's the same case like OL 2003 except that the class name in OL XP is
AfxWndA instead of AfxWndW. If you like to verify it, simply use a tool like
Spy++ and explore the window hierarchy. You can then also add the case for
RTF, which for my needs wasn't neccessary.

--
Viele Gruesse / Best regards
Michael Bauer - MVP Outlook
Keep your Outlook categories organized!
http://www.shareit.com/product.html?productid=300120654&languageid=1
(German: http://www.VBOffice.net/product.html?pub=6)

Am Tue, 30 Jan 2007 18:17:38 -0500 schrieb George Hester:
Hi Michael I didn't finish getting all <modBodyFocus.bas> in yet I just
wanted to ask you a few questions.

In the function GetBodyHandle under the Case for Outlook 2000 (version 9) at
the bottom of that section the last If lRes Then contains a comment. It
looks similar to what you have for version 11 in the sense that what you
have in the comment, is similar to the second parameters for the GetWindow
function calls in the Case version 11 area. I am just wondering if the
comment in the Case version 9 area is ok just to go by?

The other question is that the GetBodyHandle doesn't do anything for the
Outlook 2002 (version 10) case. Is that right or can we just put Case 9
down as a Case Else instead?

Thanks.

--

George Hester
_________________________________
Michael Bauer said:
For the focus you could adapt the following sample. Copy FindWindow,
SetForegroundWindow and GetInspectorHandle:
http://www.vboffice.net/sample.html?mnu=2&smp=20&cmd=showitem

Call GetInspectorHandle with the caption of the Explorer and then call with
that result (if it's not 0) SetForegroundWindow.

--
Viele Gruesse / Best regards
Michael Bauer - MVP Outlook
Keep your Outlook categories organized!
http://www.shareit.com/product.html?productid=300120654&languageid=1
(German: http://www.VBOffice.net/product.html?pub=6)


Am Mon, 29 Jan 2007 21:49:35 -0500 schrieb George Hester:
http://www.microsoft.com/technet/prodtechnol/office/office2000/proddocs/opg/
 
G

George Hester

ok thanks for that. I am not sure how to use Spy++ and the one I have is
defective. The last time I looked at it that is what I read at Microsoft's
site. Something to do with the one in Visual Sturdio 6 and Windows 2000.
But maybe it will work enough for what you suggest. In any case I have no
need for it to work in Outlook XP either. Luckily I am using Outlook 2000
and so it did all I needed it to do.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top