Handling builtin commandbar buttons events

B

Bubu

Hello there!

I'm developing Office COM addin for Word, PP, and Excel. I use
C++/ATL 7 and _IDTExtensibility2 stuff. My problem is how to handle
click events from built in buttons and commandbars , for example I
would like to handle click from "Document Map" button on Standard
command bar. I thought that there is CommandBar onlcick event - alas
there is no one. I have no problem to handle events from my own-created
buttons. I use IDispEventSimpleImpl<id ,CConnect
,&__uuidof(Office::_CommandBarButtonEvents) and
SINK_ENTRY_INFO and
CommandButtonXXXXeEvents::DispEventAdvise((IDispatch*)myButton)
and define my own event handler function. I tried get specific built
in button and subscribe to its event source in same manner
CommandButtonXXXXeEvents::DispEventAdvise , however I get
E_NOINTERFACE error.
May be somebody knows how to handle click from Line color dialog - this
nice popup dialog form Drawing commandbar? There is a color palette
there with small colored rectangles.
There is nothing in MSDN about this problem. Any input or/and ideas
will be highly appreciated.
Thanks David.
 
A

Alex

Bubu said:
My problem is how to handle
click events from built in buttons and commandbars , for example I
would like to handle click from "Document Map" button on Standard
command bar.

I am also interested in this question.
 
A

Alex Korchemniy

If you are developing against the newer version of office then you can sink
_CommandBarButtonEvents... I can post code if you need it. I ran into a
problem where I need to handle events in 97 version. If you find a solution
please help me out.

Alex Korchemniy
alexmessanger#hotmail.c0m
 
A

Alex Korchemniy

I might as well post the code that works for me (add more error handling if u
need):

// Try to find the bar
CComPtr <MSOffice::CommandBar> spCmdBar;
CComVariant vName(L"Standard");
hr = spCmdBars->get_Item(vName, &spCmdBar);
if (hr == S_OK)
{
// Get controls collection for the command bar
CComPtr <MSOffice::CommandBarControls> spBarControls;
spBarControls = spCmdBar->GetControls();
// Find the bar item
CComPtr <MSOffice::CommandBarControl> spBarItem;
spBarItem = spBarControls->GetItem(L"Save"); // Save button for example
// QI for button
CComQIPtr <MSOffice::_CommandBarButton> spCmdButton(spBarItem);
m_spCmdButton = spCmdButton; // save ref so that you can unadvise later
// Sink events
CommandButton1Events::DispEventAdvise(m_spCmdButton);
}

***
To make sure things are clear here is the typedef of CommandButton1Events:
typedef IDispEventSimpleImpl<1, CAddin,
&__uuidof(MSOffice::_CommandBarButtonEvents)> CommandButton1Events;

***
Note this will handle the click event. However it will not remove any other
handlers for the event (ie. the application define handlers).
 
B

Bubu

HI Alex, thanks for your input, I will try your suggestion, though I've
tried somethign very similar before (see my question) . I will inform
you about results.
Appreciate this, David
 
B

Bubu

Well I got it!
Your code (and my code as well) work on Command Bar controls of type
msoControlButton (like "Save" button). Im trying to hook on click event
of "Drawing"->"Line Color" button which is of msoControlButtonPopup
type, actually varible creation :
CComQIPtr <MSOffice::_CommandBarButton> spCmdButton(spBarItem)
fails , spCmdButton is NULL !
I tried to play with this variable (like casting ), but nothing good
happens. I guess waht is needed is somthing like
"Office::_CommandlButtonPopupEvents" to handle this correctly.
Any ideas ??
David
 
A

Alex Korchemniy

If you want to handle a specific item in the popup you can get the control
collection and then get the control bar item and then QI for button. If you
want to handle the popup itself then you can probably just remove the
original bar item and replace with your own CommandBarButton.

Alex Korchemniy
 

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