Herman Solberg

Hi!

I'm writing a Word 2003 addin that includes a new menu in the menubar with some custom commands. When I'm debugging the addin, Word launched perfectly and the first command click executes just fine but when I'm clicking a command on my custom menu the second time, the commands don't execute anymore.

It's really strange since the ThisAddin_Shutdown method executes when I shut down Word, so the addin has not been disabled. Somewhere along the way my callbacks must be ignored.

Does anyone have a theory about what could be causing this problem I sure would appreciate it.

//Herman




Re: Visual Studio Tools for Office Debugging session ends after one command - Word 2003 Addin, VSTO 2005 SE

X4U

Hello Herman,

it seems that you declaring the menu variable within your function,

and so the variable get's disconnected as soon as the Garbage Collector does his job.

Solution:

Declare a classwide variable that keeps a reference to your MenuButton.

Something like that:

Code Snippet

public partial class ThisAddIn

{

private Office.CommandBar _AppCommandBar;

private Office.CommandBarButton _ButtonSaveData;

private string _Docname;

private string _Filename;

private void ThisAddIn_Startup(object sender, System.EventArgs e)

{

BuildFormCommandBar();

Word.Range range = this.Application.ActiveDocument.Content;

int maxPage = (int)range.get_Information(Microsoft.Office.Interop.Word.WdInformation.wdNumberOfPagesInDocument);

Trace.WriteLine(maxPage);

}

static string Data_File_Name(string pathvar) //checks for files in local save folder and decides save name.

{

//initial filename = bla/bla/yabba/yabba/Test01.txt

int fileIndex = 01;

// variable holds the filepath

string filepath;

do

{

// calculate the filename

filepath = CalculateFileName(fileIndex, pathvar, "Test", ".txt");

fileIndex++;

}

while (File.Exists(filepath));

return filepath;

}

static string CalculateFileName(int index, string directoryPath, string fileName, string extensionName)

{

string filename = string.Format ("{0}_{1:00}{2}", fileName, index, extensionName );

return Path.Combine(directoryPath, filename);

}

private void ThisAddIn_Shutdown(object sender, System.EventArgs e)

{

}

private void BuildFormCommandBar()

{

try

{

//Add the command bar to the document.

_AppCommandBar = this.Application.CommandBars.Add("Forms Commands", missing , missing, missing);

_AppCommandBar.Visible = true;

//Add command bar buttons to the command bar

_ButtonSaveData = this.CreateCommandBarButton("Extract and Save", "EXTRACT_SAVE", ref _AppCommandBar);

_ButtonSaveData.FaceId = 1845;

_ButtonSaveData.Click += new Microsoft.Office.Core._CommandBarButtonEvents_ClickEventHandler(_ButtonSaveData_Click);

}

catch (Exception ex)

{

MessageBox.Show(ex.Message, "Error adding Forms Command Bar");

}

}

void _ButtonSaveData_Click(Microsoft.Office.Core.CommandBarButton Ctrl, ref bool CancelDefault)

{

_Filename = Data_File_Name("C:\\Temp");

MessageBox.Show(_Filename);

}

private Office.CommandBarButton CreateCommandBarButton(string Caption, string Tag, ref Office.CommandBar commandBar)

{

//Add the button with the given Caption and Tag properties.

Office.CommandBarButton newButton = (Office.CommandBarButton)(commandBar.Controls.Add(

Office.MsoControlType.msoControlButton,

missing, missing, missing, missing));

newButton.Caption = Caption;

newButton.Tag = Tag;

newButton.Visible = true;

return newButton;

}

Hope this helps,

greets, Helmut






Re: Visual Studio Tools for Office Debugging session ends after one command - Word 2003 Addin, VSTO 2005 SE

Herman Solberg

Thanks Helmut!

That helped plenty. It also solved my deployment issue at the same time. Thanks again...

//Herman