Visual Studio Development Bookmark and Share   
 index > Visual Studio Tools for Office > Word add-in and multiple instances of Word running...
 

Word add-in and multiple instances of Word running...

I have a nice little word add-in that works great when one instance of Word is running. However, if I open another document and have two copies of Word open at once, then the events in my add-in stop firing! Actually sometimes the events still work in the original instance of Word, but never in the copy opened second. If I close all copies of word and reopen just one document at a time then the events fire just fine.

I'm guessing this is something to do with focus or with the active document. I'm not opening the documents with code or anything that I would expect to cause this.

Thanks for the help!
gbrot  Tuesday, July 08, 2008 5:45 PM

Based on your reply, may we assume that the "event handlers" that stop firing are those for the CommandBar controls?

Looking at your code, the reason for these events not firing are because

1. You need to declare the objects for the CommandBar and the controls you're creating at the CLASS level. Currently, they are declared in the procedure AddToolbar. As soon as this procedure ends, these go out of scope and will eventually be destroyed by the GarbageCollection.

2. In order for the application windows to be able to distinguish which window is linked to which command bar, the Office applications track the TAG property of each commandbar and its controls. you need to explicitly sign a unique value to the Tag property of each object you're creating.

Look carefully at the code sample on this page.

Cindy Meister  Wednesday, July 09, 2008 3:28 PM
Take a look at Ji's answer in this message.

Cindy Meister  Thursday, July 10, 2008 11:18 AM

Which version of Word?

Which version of Visual Studio?

Which Visual Studio template did you use to create the Add-in?

Please show us the code that assigns the event handlers.

Cindy Meister  Wednesday, July 09, 2008 12:37 PM
The problem occurs in both Word 03 and 07. The add-in was created with VS 2005, i'm not sure how to say which template I used, but here's some code:

Code Snippet

private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
GetData();
AddToolbar();
}


private void AddToolbar()

{
try
{
commandBar = Application.CommandBars["Report Toolbar"];
}
catch (ArgumentException e)
{
// Toolbar does not exist so we should create it.
}
if (commandBar == null)
{
// Add a commandbar
commandBar = Application.CommandBars.Add("Report Toolbar", 1, missing, true);
}
try
{
CatCombo = (Office.CommandBarComboBox)commandBar.Controls.Add(
Office.MsoControlType.msoControlDropdown, missing, missing, missing, missing);
CatCombo.Style = Office.MsoComboStyle.msoComboLabel;
for (int i = 0; i < dsFieldList.Tables.Count; i++ )
{ // the category name is stored in the first row of the table
CatCombo.AddItem(dsFieldList.Tables[i].Rows[0]["lname"].ToString(), missing);
}
CatCombo.ListIndex = 1;
CatCombo.DropDownLines = 15;
CatCombo.Width = 115;
CatCombo.Change += new Office._CommandBarComboBoxEvents_ChangeEventHandler(CatComboChange);

commandBar.Visible = true;
}
catch (ArgumentException e)
{
MessageBox.Show(e.Message);
}
}

private void CatComboChange(Office.CommandBarComboBox ctrl)
{
LoadComboItems(ctrl.Text);
}



thanks for your help!

gbrot  Wednesday, July 09, 2008 2:41 PM

Based on your reply, may we assume that the "event handlers" that stop firing are those for the CommandBar controls?

Looking at your code, the reason for these events not firing are because

1. You need to declare the objects for the CommandBar and the controls you're creating at the CLASS level. Currently, they are declared in the procedure AddToolbar. As soon as this procedure ends, these go out of scope and will eventually be destroyed by the GarbageCollection.

2. In order for the application windows to be able to distinguish which window is linked to which command bar, the Office applications track the TAG property of each commandbar and its controls. you need to explicitly sign a unique value to the Tag property of each object you're creating.

Look carefully at the code sample on this page.

Cindy Meister  Wednesday, July 09, 2008 3:28 PM
I actually have my controls declared at the class level, had just left that part out of the snippet.

I did not have the tag property set though. So I added that in for each control (3 total) and it's slightly improved, but not quite fixed.
When I open a second word document, my dropdownlist is still not firing, but I have a button control that is firing now... though when it fires it's supposed to pull data from the dropdown list, and it's pulling the data from the dropdown on the first Word instance, not the second that I'm currently in..

so maybe I need to create a new unique tag for each control for each instance of the toolbar? I'm not sure how to do that as it doesn't look like AddToolbar is being ran when the new document is opened. Do I need to add an event to fire for the new document creation?



gbrot  Wednesday, July 09, 2008 4:36 PM
Take a look at Ji's answer in this message.

Cindy Meister  Thursday, July 10, 2008 11:18 AM
That fixed it. Looks like the windowsactivate event is just what i was looking for.
Thanks
gbrot  Thursday, July 10, 2008 4:10 PM

You can use google to search for other answers

Custom Search

More Threads

• I Want Multiple Word Add-ins to Share Common New Ribbon Tab
• String Length in points for word (VB.net interop)
• Folder.BeforeItemMove Event in VSTO for Outlook
• Open Specific Function Wizard
• Automatically create IMAP accounts in Outlook 2007
• Automatically running Database Query in Excel
• 'name.xls' is not a valid add-in
• Incoming mail problem
• Visio 2007 Addon with Visual Studio 2008
• Word binary format: finding label of OLE embedded document