I have a very similiar problem. I've been bangin my head against a wall on it
for 4 days now. I have a .NET COM Interop Addin written in C#. When it loads,
it adds a CommandBarPopup and two CommandBarButtons's to the main 'Menu Bar'.
One allows you to open a document from our in-house doc-repository which
lives in Access. The buttons work fine as far as fireing their respective
events, etc. But, once you say "open document" and, that actually happens,
the menu no longer works. My first thought was that once the document is
loaded as a "new" document, a new window objet was being created and the
event handler registrations where being wiped out. However, I tested that
theory by hooking into to the document open event. Once that fires I take the
newly opened document, grab it's Application interface and to that, re-wire
the event handlers. Still nothing. Here is the code I use to activate the
document:
public void ActivateDocument(Document slDoc, ServerConnection psc)
{
try
{
Document slDocDeep = psc.DocumentManager.getDocument(slDoc.id);
FileStream fs = new FileStream(slDocDeep.filename, FileMode.Create);
BinaryWriter bw = new BinaryWriter(fs);
bw.Write(slDocDeep.document, 0, slDocDeep.document.Length);
bw.Flush();
bw.Close();
fs.Close(); // Already done by bw?
object missing = Type.Missing;
object filepath = Path.GetFullPath(slDoc.filename);
wdDoc = wordApp.Documents.Open(ref filepath,
ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing);
// Lets try re-wiring the event handlers...
// if(menuBar == null)
// menuBar = wdDoc.Application.CommandBars["Menu Bar"];
// foreach (CommandBarControl menuItem in menuBar.Controls)
// {
// if (menuItem.Caption == MENU_ITEM_DOCMGRMENU)
// {
// popup = (CommandBarPopup)menuItem;
// foreach (CommandBarControl cbc in popup.Controls)
// {
// try
// {
// CommandBarButton bn = (CommandBarButton)cbc;
// if(bn.Caption == MENU_ITEM_SEND_TO_DOCMGR)
// bn.Click += new
_CommandBarButtonEvents_ClickEventHandler(bnSendToDocMgr_Click);
// else if (bn.Caption == MENU_ITEM_OPEN_FROM_DOCMGR)
// bn.Click += new
_CommandBarButtonEvents_ClickEventHandler(bnOpenFromDocMgr_Click);
// else
// MessageBox.Show("Unable to wire it up: " + bn.Caption);
// }
// catch(Exception ex)
// {
// MessageBox.Show(ex.Message);
// }
// }
// }
// }
//wdDoc.Activate();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
// try
// {
// if (wdDoc != null)
// while(Marshal.ReleaseComObject(wdDoc) > 0);
// }
// catch(Exception ex)
// {
// MessageBox.Show(ex.Message);
// }
}
}
once the call to wordApp.Documents.Open() occurs and the document is loaded,
all menu activity for the menu I added seems to be ignored. Any ideas?
Thank you,
Brian