Word Add-In does not work correctly

M

Medes

Hi,

I have created a Shared Add-In for MS Word (2003) but it does not work
correctly,

It works good if you double click on a .doc file and open it directly and it
works good if you open a new instance of word .

the problem is if you have already opened a file or an instance of word and
you go to ( File -> open ) to open an existing file. it stops work. I see my
button but when i click it nothing happed, no Errors . it just does not
answer.
here is a little bit of my code:

public class Connect : Object, Extensibility.IDTExtensibility2
{
public void OnConnection(object application,
Extensibility.ext_ConnectMode connectMode, object addInInst, ref System.Array
custom)
{
applicationObject = application;
addInInstance = addInInst;
wordApp = (Word.Application)application;
CommandBar toolBar = AddWordToolbar(wordApp, "Ignito Toolbar");
}
public void OnDisconnection(Extensibility.ext_DisconnectMode
disconnectMode, ref System.Array custom)
{
}

public void OnAddInsUpdate(ref System.Array custom)
{
}

public void OnStartupComplete(ref System.Array custom)
{
}

public void OnBeginShutdown(ref System.Array custom)
{
MyButton.Enabled = false;
}
}
 
C

Cindy Meister

This sounds very much like you haven't assigned a value to the buttons' TAG
property so that Word. If you don't do that, then the buttons can't work
across multiple document windows. This is documented in the articles on the
MSDN site concerned with creating COM Addins for Office applications.
 
M

Medes

Thank you so much,

I assigned a value to my button and now works across multiple application
but with a little bugg,

my button shows a form when you click it, the form contains the property of
the doc file, now if i have more than one instance of word (more than one
..doc file opened)
when i click my button it shows just shows one form (the first one).
 
C

Cindy Meister

Is this a form contained in your Addin? Or is this a Word dialog box?

Since I don't know how the code the button is executing works, it's
difficult to venture a guess. The first though I have, though, is that you're
not instantiating a new class instance of the form each time the button is
clicked, so there can be only one form.
 
M

Medes

Thank you again,

here is my code (the clic event)

public void Vie MyBTN_Click(CommandBarButton SomeButton, ref bool SomeBool)
{
if (!MyBTNAlreadyClicked)
{
TaxForm = new TaxonomyForm(this);
TaxForm.Show();
MyBTNAlreadyClicked = true;

TreeViewSerializer Serializer = new TreeViewSerializer(this);

Serializer.LoadTreeView(TaxForm.treeView1, TaxForm.listBox1);
}
else
{
TaxForm.BringToFront();
}
TaxForm.Disposed += new EventHandler(TaxForm_Disposed);
}

void TaxForm_Disposed(object sender, EventArgs e)
{
MyBTNAlreadyClicked = false;
}
 
C

Cindy Meister

Indeed: you seem to use only the one form. If the form already exists, you
try to bring it to the front.

if (!MyBTNAlreadyClicked)

else
{
TaxForm.BringToFront();
}

Is MyBTNAlreadyClicked declared as a static member field? Or as an instance
member?

What, more exactly is the object "this"? What kind of class/object are you
passing the TaxonomyForm contsructor? Why do you need to pass this to the
form's constructor? Maybe you ought to try declaring TaxForm at the class
level, not static. In the Click event, check
if (TaxForm==null)
{ TaxForm = new TaxonomyForm; //and generate the TreeView}

TaxForm.Show();
//maybe TaxForm.BringToFront(); if show alone isn't enough
 
M

Medes

MyBTNAlreadyClicked is not static, and the object "this" is the Connect class
and I pass it to TaxonomyForm Constructor because I need check if it is the
Word application or Excel Application like this:
if (MyConn.excelApp != null)
DocProperties =
(DocumentProperties)MyConn.excelApp.ActiveWorkbook.CustomDocumentProperties;

else if (MyConn.wordApp != null)
DocProperties =
(DocumentProperties)MyConn.wordApp.ActiveDocument.CustomDocumentProperties;

I need to do this because if you click on my button it opens a
System.Windows.Forms.Form (TaxonomyForm) there i check if the document
already have Custom properties or not and i show my metadata as a treeview in
the taxonomyForm if my document contains a custom preperty that is equal one
of the nodes of treeview(metadata) it will be another color and you can right
click on a node to add it as custom propery to the document.

here is a bit of the Connect class:

[GuidAttribute("9765FFB3-05C4-43A1-BEB0-7DEDBE0218E1"),
ProgId("IgnitoAddIn.Connect")]
public class Connect : Object, Extensibility.IDTExtensibility2
{
bool MyBTNAlreadyClicked = false;
public CommandBarButton MyBTN;
TaxonomyForm TaxForm = null;
public Connect()
{
}

public void OnConnection(object application,
Extensibility.ext_ConnectMode connectMode, object addInInst, ref System.Array
custom)
{
applicationObject = application;
addInInstance = addInInst;

SetApplicationFields(application);
CommandBar toolBar = null;
if (wordApp != null)
{
toolBar = AddWordToolbar(wordApp, "Ignito Toolbar");
}
else if (excelApp != null)
{
toolBar = AddExcelToolbar(excelApp, "Ignito Toolbar");
}
MyBTN = this.MakeANewButton(toolBar, "View Taxonomy", 1000,
new _CommandBarButtonEvents_ClickEventHandler(MyBTN_Click));
}
public void OnDisconnection(Extensibility.ext_DisconnectMode
disconnectMode, ref System.Array custom)
{
}

public void OnAddInsUpdate(ref System.Array custom)
{
}

public void OnStartupComplete(ref System.Array custom)
{
}

public void OnBeginShutdown(ref System.Array custom)
{
MyBTN.Enabled = false;
}

private object applicationObject;
private object addInInstance;

private void SetApplicationFields(object application)
{
if (application is Word.Application)
{
wordApp = (Word.Application)application;
excelApp = null;
}
else if (application is Excel.Application)
{
excelApp = (Excel.Application)application;
wordApp = null;
}
}
private CommandBar AddWordToolbar(Word.Application word, string
toolbarName)
{
.............
}
private CommandBar AddExcelToolbar(Excel.Application excel, string
toolbarName)
{
.........
}
private CommandBarButton MakeANewButton(CommandBar commandBar, string
caption,
int faceID, _CommandBarButtonEvents_ClickEventHandler
clickHandler)
{
........
}
public void MyBTN_Click(CommandBarButton SomeButton, ref bool SomeBool)
{
if (!MyBTNAlreadyClicked)
{
TaxForm = new TaxonomyForm(this);
TaxForm.Show();
MyBTNAlreadyClicked = true;

TreeViewSerializer Serializer = new TreeViewSerializer(this);

Serializer.LoadTreeView(TaxForm.treeView1, TaxForm.listBox1);
}
else
{
TaxForm.BringToFront();
}
TaxForm.Disposed += new EventHandler(TaxForm_Disposed);
}

void TaxForm_Disposed(object sender, EventArgs e)
{
MyBTNAlreadyClicked = false;
}
}
 

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