Losing menu events

D

David Thielen

Hi;

We have a problem where Word stops calling our menu event handlers after
awhile. The most common case where it happens is we open document 1, then
open document 2, then close document 1.

Our AddIn is still loaded and the DoubleClick handler still works as do the
Open/Close/etc events. But the menu events are gone.

The menu objects I get from Word are stored in an object that is created in
my Connect.cs class:
public class Connect : Object, IDTExtensibility2, IDisposable,
IRibbonExtensibility

So it is in the Connect object. This object lasts the lifetime of the AddIn
- correct? So the member object that contains the menu object will still
exist after a document is closed - correct?

I create the menus using:
private readonly CommandBarPopup[] popupMenus = new CommandBarPopup[5];
private readonly CommandBarControl[] buttonMenus = new CommandBarControl[26];
....
buttonMenus[ind] =
popupMenus[mainInd].Controls.Add(MsoControlType.msoControlButton,
Type.Missing, Type.Missing, Type.Missing, false);
buttonMenus[ind].Caption = buttonOn.caption;
buttonMenus[ind].TooltipText = buttonOn.tooltip;
buttonMenus[ind].Tag = buttonOn.tag;
buttonMenus[ind].Visible = true;
buttonMenus[ind].BeginGroup = buttonOn.beginGroup;
if (buttonOn.handler != null)
((CommandBarButton)buttonMenus[ind]).Click += buttonOn.handler;

What are my menu event handlers suddenly not being called?

--
thanks - dave
david_at_windward_dot_net
http://www.windwardreports.com

Cubicle Wars - http://www.windwardreports.com/film.htm
 
J

Jialiang Ge [MSFT]

Hello Dave,

From your post, my understanding on this issue is: you word add-in's menu
events are gone after you open 2 documents and close the first one. If I'm
off base, please feel free to let me know.

Firstly, would you help me to confirm if it is the same issue as you posted
in 2007/07/28 "Losing Word Events"? In that post, you also mentioned that
the add-in loses the events when the first of the 2 instances of Word 2003
is closed.
So it is in the Connect object. This object lasts the lifetime of the
AddIn - correct? So the member object that contains the menu
object will still exist after a document is closed - correct?
Yes, you are right. It lasts the lifetime of the Add-In and the menu object
should still exist after a document is closed.

I notice that you are implementing IRibbonExtensibility interface in your
Connect class. IRibbonExtensibility is for Office 2007. In your last post,
you said the add-in is for Word 2003. Would you help to confirm if the
issue exists when Office 2007 features are not introduced, so that we could
narrow the focus.

You said that the add-in is still loaded and the double click handler still
works. What is the double click handler for? Is it also bound to a menu
button (open/close/etc)? What do you mean by the menu events? Are they
referring to the Click event of the menu buttons?
(((CommandBarButton)buttonMenus[ind]).Click += buttonOn.handler;) or do you
mean that the new menu button could not even be shown?

You also said that "the menu objects I get from Word are stored in an
object that is created in my Connect.cs class", what do you mean by the
'object'? Is the the popupMenus and buttonMenus?

The last question, you implement IDisposable interface. How do you write
the Dispose method?

Please let me know the information above so that I could have a clearer
picture of the issue.

Sincerely,
Jialiang Ge ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
For MSDN subscribers whose posts are left unanswered, please check this
document: http://blogs.msdn.com/msdnts/pages/postingAlias.aspx

Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications. If you are using Outlook Express/Windows Mail, please make sure
you clear the check box "Tools/Options/Read: Get 300 headers at a time" to
see your reply promptly.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
D

David Thielen

Hi;

Yes, this is the same problem I posted on earlier. I'm hoping that there is
more information about this now available.

The IRibbonExtensibility is only in there fore the Word 2007 build - sorry.
It is not in the Word 2000, 2002, & 2003 builds. The problem only exists with
Word 2000/2002/2003.

Yes, the events that are no longer firing are the ones set with
(((CommandBarButton)buttonMenus[ind]).Click += buttonOn.handler; The menus
are still there, but clicking on the menu selections does nothing.

For the double click I do:
ApplicationEvents3_Event aee = (ApplicationEvents3_Event)applicationObject;
aee.WindowBeforeDoubleClick += Event_WindowBeforeDoubleClick;
which is a different event handler than the menu ones.

The variables:
private readonly CommandBarPopup[] popupMenus = new CommandBarPopup[5];
private readonly CommandBarControl[] buttonMenus = new CommandBarControl[26];

are instance variables in an object (called CommandMenu). That object is
instantiated in the Connect.cs constructor.

The dispose code just gets rid of our licensing thread - nothing to do with
the menu:
public void Dispose()
{
Trap.trap();
license.Dispose();
}

--
thanks - dave
david_at_windward_dot_net
http://www.windwardreports.com

Cubicle Wars - http://www.windwardreports.com/film.htm




Jialiang Ge said:
Hello Dave,

From your post, my understanding on this issue is: you word add-in's menu
events are gone after you open 2 documents and close the first one. If I'm
off base, please feel free to let me know.

Firstly, would you help me to confirm if it is the same issue as you posted
in 2007/07/28 "Losing Word Events"? In that post, you also mentioned that
the add-in loses the events when the first of the 2 instances of Word 2003
is closed.
So it is in the Connect object. This object lasts the lifetime of the
AddIn - correct? So the member object that contains the menu
object will still exist after a document is closed - correct?
Yes, you are right. It lasts the lifetime of the Add-In and the menu object
should still exist after a document is closed.

I notice that you are implementing IRibbonExtensibility interface in your
Connect class. IRibbonExtensibility is for Office 2007. In your last post,
you said the add-in is for Word 2003. Would you help to confirm if the
issue exists when Office 2007 features are not introduced, so that we could
narrow the focus.

You said that the add-in is still loaded and the double click handler still
works. What is the double click handler for? Is it also bound to a menu
button (open/close/etc)? What do you mean by the menu events? Are they
referring to the Click event of the menu buttons?
(((CommandBarButton)buttonMenus[ind]).Click += buttonOn.handler;) or do you
mean that the new menu button could not even be shown?

You also said that "the menu objects I get from Word are stored in an
object that is created in my Connect.cs class", what do you mean by the
'object'? Is the the popupMenus and buttonMenus?

The last question, you implement IDisposable interface. How do you write
the Dispose method?

Please let me know the information above so that I could have a clearer
picture of the issue.

Sincerely,
Jialiang Ge ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
For MSDN subscribers whose posts are left unanswered, please check this
document: http://blogs.msdn.com/msdnts/pages/postingAlias.aspx

Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications. If you are using Outlook Express/Windows Mail, please make sure
you clear the check box "Tools/Options/Read: Get 300 headers at a time" to
see your reply promptly.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
J

Jialiang Ge [MSFT]

Hello Dave,

Thanks for the information. Here is a quick note to let you know that I am
doing researches on it though I still cannot reproduce the issue in my
side. I tried to build a Shared add-in project according to your given
codes, but the add-in worked well in my side.

Here is a suggestion that you may consider checking in your side:
CommandBarButton.Tag property should be unique to the events. If the
CommandBarButton.Tag duplicate, the event may be lost when uses clicks on
the buttons. In your code, I notice that you assign a buttonOn.tag instance
to the property. What is the buttonOn? Is it unique for each
commandBarbutton?

Sincerely,

Sincerely,
Jialiang Ge ([email protected], remove 'online.')
Microsoft Online Community Support

=================================================
When responding to posts, please "Reply to Group" via your newsreader
so that others may learn and benefit from your issue.
=================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
A

Andrei Smolin [Add-in Express]

Hello David,
We have a problem where Word stops calling our menu event handlers after
awhile. The most common case where it happens is we open document 1, then
open document 2, then close document 1.

Add-in Express team assumes that Word re-creates command bar controls when
you open a new document. Accordingly, in the code of our command bar control
components, we find custom controls and re-connect event handlers.

Regards from Belarus,

Andrei Smolin
Add-in Express Team Leader
www.add-in-express.com


David Thielen said:
Hi;

We have a problem where Word stops calling our menu event handlers after
awhile. The most common case where it happens is we open document 1, then
open document 2, then close document 1.

Our AddIn is still loaded and the DoubleClick handler still works as do
the
Open/Close/etc events. But the menu events are gone.

The menu objects I get from Word are stored in an object that is created
in
my Connect.cs class:
public class Connect : Object, IDTExtensibility2, IDisposable,
IRibbonExtensibility

So it is in the Connect object. This object lasts the lifetime of the
AddIn
- correct? So the member object that contains the menu object will still
exist after a document is closed - correct?

I create the menus using:
private readonly CommandBarPopup[] popupMenus = new CommandBarPopup[5];
private readonly CommandBarControl[] buttonMenus = new
CommandBarControl[26];
...
buttonMenus[ind] =
popupMenus[mainInd].Controls.Add(MsoControlType.msoControlButton,
Type.Missing, Type.Missing, Type.Missing, false);
buttonMenus[ind].Caption = buttonOn.caption;
buttonMenus[ind].TooltipText = buttonOn.tooltip;
buttonMenus[ind].Tag = buttonOn.tag;
buttonMenus[ind].Visible = true;
buttonMenus[ind].BeginGroup = buttonOn.beginGroup;
if (buttonOn.handler != null)
((CommandBarButton)buttonMenus[ind]).Click += buttonOn.handler;

What are my menu event handlers suddenly not being called?

--
thanks - dave
david_at_windward_dot_net
http://www.windwardreports.com

Cubicle Wars - http://www.windwardreports.com/film.htm
 
D

David Thielen

We tried that and we were getting 2 events. Is there anything special in how
you re-connect?

--
thanks - dave
david_at_windward_dot_net
http://www.windwardreports.com

Cubicle Wars - http://www.windwardreports.com/film.htm




Andrei Smolin said:
Hello David,
We have a problem where Word stops calling our menu event handlers after
awhile. The most common case where it happens is we open document 1, then
open document 2, then close document 1.

Add-in Express team assumes that Word re-creates command bar controls when
you open a new document. Accordingly, in the code of our command bar control
components, we find custom controls and re-connect event handlers.

Regards from Belarus,

Andrei Smolin
Add-in Express Team Leader
www.add-in-express.com


David Thielen said:
Hi;

We have a problem where Word stops calling our menu event handlers after
awhile. The most common case where it happens is we open document 1, then
open document 2, then close document 1.

Our AddIn is still loaded and the DoubleClick handler still works as do
the
Open/Close/etc events. But the menu events are gone.

The menu objects I get from Word are stored in an object that is created
in
my Connect.cs class:
public class Connect : Object, IDTExtensibility2, IDisposable,
IRibbonExtensibility

So it is in the Connect object. This object lasts the lifetime of the
AddIn
- correct? So the member object that contains the menu object will still
exist after a document is closed - correct?

I create the menus using:
private readonly CommandBarPopup[] popupMenus = new CommandBarPopup[5];
private readonly CommandBarControl[] buttonMenus = new
CommandBarControl[26];
...
buttonMenus[ind] =
popupMenus[mainInd].Controls.Add(MsoControlType.msoControlButton,
Type.Missing, Type.Missing, Type.Missing, false);
buttonMenus[ind].Caption = buttonOn.caption;
buttonMenus[ind].TooltipText = buttonOn.tooltip;
buttonMenus[ind].Tag = buttonOn.tag;
buttonMenus[ind].Visible = true;
buttonMenus[ind].BeginGroup = buttonOn.beginGroup;
if (buttonOn.handler != null)
((CommandBarButton)buttonMenus[ind]).Click += buttonOn.handler;

What are my menu event handlers suddenly not being called?

--
thanks - dave
david_at_windward_dot_net
http://www.windwardreports.com

Cubicle Wars - http://www.windwardreports.com/film.htm
 
D

David Thielen

J

Jialiang Ge [MSFT]

Hello Dave,

As you said, I get the exception "Exception from HRESULT: 0x800A01A8" when
it tries to enable/disable the menu items. The exception occurses at
popupMenus[menuOn].Enabled = enable;
It is because just holding onto a global variable of the CommandBarButton
or CommandBarPopup object and using that to enable/disable doesn't
guarantee that the operation is done successfully. We should find the
object in the bar object to get the correct reference.

CommandBarButton tcbb =(CommandBarButton
)wdApp.CommandBars.FindControl(oMissing,oMissing,"QWER",oMissing); //find
tcbb.Enabled = true ; //set

Please try the method above and let me know the result.

Regards,
Jialiang Ge ([email protected], remove 'online.')
Microsoft Online Community Support

=================================================
When responding to posts, please "Reply to Group" via your newsreader
so that others may learn and benefit from your issue.
=================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
A

Andrei Smolin [Add-in Express]

Hello David and Jialiang,

We get the same exception, too. This can be fixed in the way suggested by
Jialiang.

One more thing. You don't use Marshal.ReleaseComObject in your code. From
our experience, this can be a source of other hardly-reproducible problems.
In Add-in Express documentation
(http://www.add-in-express.com/docs/net.php), we insist on using this method
for every COM object you acquire from Office applications. For instance,
before you set the value returned by FindControl to a command bar button
variable, you release the variable:

if (theVariable != null) Marshal.ReleaseComObject(theVariable);
theVariable = theCommandBars.FindControl(....)

In the code above, you see that you need to have theCommandBars acquired at
startup and released (ReleaseComObject) at shutdown. That's because you
should never chain Office interfaces in the
officeApp.SomeObjects.Item[1].SomeObjectProperty.AnotherObjectProperty way.
Instead, you should have separate variables for every such object and
release the variable when you are done with it.

Regards from Belarus,

Andrei Smolin
Add-in Express Team Leader
www.add-in-express.com
 
D

David Thielen

Hi;

I didn't understand what you meant before - now I do.

Ok, good news first - adding this and Andrei's suggestion to call
ReleaseComObject has fixed all of our menu problems. THANK YOU.

But I have a question. We have to enable/disable our menu items a lot - we
check every time the caret moves. So we are calling this a lot. And each time
we need to call ReleaseComObject() on each menu, find it, and set it's Click
handler again. I assume all this takes time.

Any more efficient way to do this? Or is this as good as it gets?

--
thanks - dave
david_at_windward_dot_net
http://www.windwardreports.com

Cubicle Wars - http://www.windwardreports.com/film.htm
 
D

David Thielen

Andrei;

I don't know much about Belarus but I do know that you have some incredible
music there -

--
thanks - dave
david_at_windward_dot_net
http://www.windwardreports.com

Cubicle Wars - http://www.windwardreports.com/film.htm




Andrei Smolin said:
Hello David and Jialiang,

We get the same exception, too. This can be fixed in the way suggested by
Jialiang.

One more thing. You don't use Marshal.ReleaseComObject in your code. From
our experience, this can be a source of other hardly-reproducible problems.
In Add-in Express documentation
(http://www.add-in-express.com/docs/net.php), we insist on using this method
for every COM object you acquire from Office applications. For instance,
before you set the value returned by FindControl to a command bar button
variable, you release the variable:

if (theVariable != null) Marshal.ReleaseComObject(theVariable);
theVariable = theCommandBars.FindControl(....)

In the code above, you see that you need to have theCommandBars acquired at
startup and released (ReleaseComObject) at shutdown. That's because you
should never chain Office interfaces in the
officeApp.SomeObjects.Item[1].SomeObjectProperty.AnotherObjectProperty way.
Instead, you should have separate variables for every such object and
release the variable when you are done with it.

Regards from Belarus,

Andrei Smolin
Add-in Express Team Leader
www.add-in-express.com


Jialiang Ge said:
Hello Dave,

As you said, I get the exception "Exception from HRESULT: 0x800A01A8" when
it tries to enable/disable the menu items. The exception occurses at
popupMenus[menuOn].Enabled = enable;
It is because just holding onto a global variable of the CommandBarButton
or CommandBarPopup object and using that to enable/disable doesn't
guarantee that the operation is done successfully. We should find the
object in the bar object to get the correct reference.

CommandBarButton tcbb =(CommandBarButton
)wdApp.CommandBars.FindControl(oMissing,oMissing,"QWER",oMissing); //find
tcbb.Enabled = true ; //set

Please try the method above and let me know the result.

Regards,
Jialiang Ge ([email protected], remove 'online.')
Microsoft Online Community Support

=================================================
When responding to posts, please "Reply to Group" via your newsreader
so that others may learn and benefit from your issue.
=================================================
This posting is provided "AS IS" with no warranties, and confers no
rights.
 

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