Controlling when a toolbar button is enabled/visible?

G

Guest

Hello,

I'm creating a toolbar button for use with contacts. I planned to just add
it to the standard toolbar which looks like it shows up no matter what window
you have open. Via it Mail, contacts Tasks etc.

I was cool with that but now when I opent a contact Item in it's own window,
the standard toolbar does contain my button anymore.

Basically what I need to accomplish is the same as what happens with the MS
dialer icon. It appears when you open a contact folder and it's still
present when you open an individual contact.

I assume I'm going to have add events to my code to enable and disable this
button, correct?

Anybody have any concise sample code?

Thanks.

--
(e-mail address removed).<Remove This Before Emailing>

Network & Software Integration
www.n-sv.com

"Helping put the pieces of your IT puzzle together"
 
G

Guest

Hello Thank you Eric,

Looking at that doesn't make much sense to me and is kind of hard to read on
a page like that. I'd hate to have to install VB 6 just to try and make
sense of what's on that page.

Anybody have anything in VB.Net or C#. Closest thing I've found is an
OutlookEvents example but it's setting events on items. I haven't seen
anything of how to set events on a window opening?

Thanks.


--
(e-mail address removed).<Remove This Before Emailing>

Network & Software Integration
www.n-sv.com

"Helping put the pieces of your IT puzzle together"
 
G

Guest

It doesn't matter though if you use VB6 or .NET. You still have to implement
Explorer and Inspector wrapper collections and objects so that you can trap
the individual events for Outlook windows.

The code on Ken Slovak's page explaining Inspector wrappers can be ported to
using Explorer object using the same concepts (if you don't want to download
the ItemsCB add-in to view the Explorer examples in Visual Basic 6).

Essentially, you need global collection variables that can be added to as
soon as the collection object itself is added to. For example, generally the
AddIn class hooks into the Explorers collection:

Private WithEvents colExpl As Outlook.Explorers

This allows you to hook into the NewExplorer event:

Private Sub colExpl_NewExplorer(ByVal Explorer As Outlook.Explorer)
On Error Resume Next
gblnNewExpl = True
AddExpl Explorer
End Sub

There, you can then call the AddExpl method and pass the newly loaded
Explorer object. This method lives in a kind of maintenance module for
Explorer objects, which maintains the global collection variable for all
Explorer objects that get created.

What that module then does when the AddExpl method is called is create an
instance of an ExplWrap class. This class has all of the Outlook.Explorer
object events wired in, so you can trap a new Outlook window as soon as it
opens – which is exactly where you want to instantiate (or create for the
first time) your custom toolbars and buttons. If a user creates yet another
Outlook window (to view their Calendar separate from their e-mail for
example), the AddExpl method will be called *again*, and a NEW ExplWrap class
will be added to the Explorer collection. Now the Outlook.Explorer object
related events in those classes will fire independently of each other.

If wrappers weren’t used, then the LAST object created is the one whose
events will be handled. This is the important point to understand the
requirement for using wrappers. Otherwise, with no wrappers, after the user
cycles back to their Inbox window after looking at their Calendar in another
Window, any events bound to your custom toolbars or buttons will NOT fire.

The exact same concept applies to e-mail item or appointment item Windows
(Inspectors).

Let me know if I haven’t thoroughly muddled the waters too much with my airy
explanations.
 
G

Guest

:) Thanks Eric,

I did download the zip but like I said I don't have anything to open the
files other than notepad. Plus I'm trying to finish up something I'm
doodling with and don't want to spend 40 hours trying to go through VB 6 code
and get it into something that I'm comfortable reading. :)

I kind of understand the concepts but I'm an examples kind of guy. Think I
may have found what I need though. Or at least a good start because it's
firing everytime I change folders. Bad thing it's firing everytime I click
anything in Outlook.

Like I said, I could live with the button installing in the standard toolbar
and being visible in any window. The only problem it's not showing up in the
contact item window.

private void ThisApplication_Startup(object sender, System.EventArgs e)
{
currentExplorer = this.ActiveExplorer();
currentExplorer.SelectionChange += new
Outlook.ExplorerEvents_10_SelectionChangeEventHandler(CurrentExplorer_Event);
}

private void CurrentExplorer_Event()
{
Outlook.MAPIFolder selectedFolder =
this.ActiveExplorer().CurrentFolder;
String expMessage = "Your current folder is "
+ selectedFolder.Name + ".\n"
}

Pretty decent example in VB.Net and C#
http://msdn2.microsoft.com/en-us/library/ms268994(en-US,VS.80).aspx

But again as I stated this fires everytime you click on anything in Outlook,
I'm going to play around and see if I can find an event that just fires when
the window changes.

Thanks.

--
(e-mail address removed).<Remove This Before Emailing>

Network & Software Integration
www.n-sv.com

"Helping put the pieces of your IT puzzle together"
 
G

Guest

This sample is far more appropriate to your situation:

How to: Add Custom Toolbars and Toolbar Items to Outlook:
http://msdn2.microsoft.com/en-us/library/ms268864(en-US,VS.80).aspx

Just replace trapping Explorers with Inspectors to trap the opening of a new
item window. But when you trap the Inspectors.NewInspector event, check that
the Class property is equal to olContact before you create the button, or
hide/disable it if it is not a Contact Item.

If you can live with the originally opened item window not responding to
your toolbar button clicks if subsequent windows are opened, then you're good
to go. Otherwise you do need to use wrapper collections to have every open
window respond to the click event.

--
Eric Legault (Outlook MVP, MCDBA, old school WOSA MCSD, B.A.)
Try Picture Attachments Wizard for Outlook:
http://www.collaborativeinnovations.ca
Blog: http://blogs.officezealot.com/legault/
 
G

Guest

Hmm, still doesn't seem to work.

Office.CommandBarButton dialButton;
Outlook.Explorers selectExplorers;
Outlook.Inspectors selectInspectors;

private void ThisApplication_Startup(object sender, System.EventArgs e)
{

Outlook.ExplorerEvents_10_SelectionChangeEventHandler(CurrentExplorer_Event);
selectExplorers = this.Explorers;
selectExplorers.NewExplorer += new
Microsoft.Office.Interop.Outlook.ExplorersEvents_NewExplorerEventHandler(selectExplorers_NewExplorer);

selectInspectors = this.Inspectors;
selectInspectors.NewInspector += new
Microsoft.Office.Interop.Outlook.InspectorsEvents_NewInspectorEventHandler(selectInspectors_NewInspector);
}

void selectExplorers_NewExplorer(Microsoft.Office.Interop.Outlook.Explorer
Explorer)
{
((Outlook._Explorer)Explorer).Activate();
}

void
selectInspectors_NewInspector(Microsoft.Office.Interop.Outlook.Inspector
Inspector)
{
((Outlook._Inspector)Inspector).Activate();
}

Everything compliles but the events never fire.
 
G

Guest

Ok, I was mistaken, the Inspector portion of the previous code works. In
order to create a toolbar item on a contact Item window, the following works.

Office.CommandBarButton dialButton;

selectInspectors = this.Inspectors;
selectInspectors.NewInspector += new
Microsoft.Office.Interop.Outlook.InspectorsEvents_NewInspectorEventHandler(selectInspectors_NewInspector);

void
selectInspectors_NewInspector(Microsoft.Office.Interop.Outlook.Inspector
Inspector)
{

dialButton =
(Office.CommandBarButton)Inspector.CommandBars["Standard"].Controls.Add(Office.MsoControlType.msoControlButton, 1, missing, missing, true);
dialButton.Style = Office.MsoButtonStyle.msoButtonCaption;
dialButton.Caption = "Dial Vonage";
dialButton.FaceId = 24;
dialButton.TooltipText = "Dial through Vonage";
dialButton.Click += new
Office._CommandBarButtonEvents_ClickEventHandler(OnDialButtonClick);
dialButton.Visible = true;

Still for some reason though the explorer event isn't firing when I click on
different folders such as contacts, tasks, mail etc.
 
G

Guest

Explorer objects need to be handled a little differently. See the comments
in the ExplWrap.cls file from the ItemsCB sample for considerations on
various Explorer object events.
 
G

Guest

Hello Eric,

I made out last night by using the object explorer and browsing through the
explorer events. I ended up finding the BeforeFolderSwitch and it appears to
be exactly what I need. Everythings working the way I need it to up until
now. I also found that VS will in the event method for you if you double tab
after your declaration. Something like this.

Say you want to setup the BeforeFolderSwitch event, you type the follwing
lines:
Outlook.Explorer actExp;
actExp = this.ActiveExplorer();
actExp.BeforeFolderSwitch +=

After entering the = above, hit the tab key twice and it will add the
following code.
Microsoft.Office.Interop.Outlook.ExplorerEvents_10_BeforeFolderSwitchEventHandler(actExp_BeforeFolderSwitch);

void actExp_BeforeFolderSwitch(object NewFolder, ref bool Cancel)
{

}

Here's the entire code which will fire every time you click on a new folder
and the code to check to see if the folder you are going to contains contacts.

void actExp_BeforeFolderSwitch(object NewFolder, ref bool Cancel)
{
Outlook.MAPIFolder mfolder = NewFolder as Outlook.MAPIFolder;

if (mfolder.DefaultItemType.Equals(Outlook.OlItemType.olContactItem))
enableDialButtonMain(true);
else
enableDialButtonMain(false);
}

One thing I am curious about is in another example or two I see the authors
put in an Activate statement which the automation does not.

The one I saw it in was for the NewExplorer event and looked something like
this.

void selectExplorers_NewExplorer(Microsoft.Office.Interop.Outlook.Explorer
Explorer)
{
(Outlook._Explorer)Explorer).Activate();
}

Kind of curious as to why some of the examples have that statment in them.

Thanks for your help.
 
G

Guest

Are you referring to the sample with the Explorer wrapper class? Depending
on what you need to do, there are certain Explorer events that are more
suited to doing certain things, depending on what objects you need to work
with (like Toolbars, the MAPIFolder object, etc.). Some of these objects are
only accessible at a certain event, but not events prior.

Best suggestion is to put stubs for most of those events and set breakpoints
on them, and access the objects your code needs to use when they are first
completely available.
 
G

Guest

Just needed to catch an event when folders are clicked on so I would know
when a contacts folder was clicked. The BeforeFolderSwitch seemed to do just
that and setting the break points is what I did to figure it out.

Thanks.


--
(e-mail address removed).<Remove This Before Emailing>

Network & Software Integration
www.n-sv.com

"Helping put the pieces of your IT puzzle together"
 

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