Word Add_in problem

M

Medes

Hi everybody,

I have created an Add-in for MS Word. It is a toolbar that contains a button
when you click on button it loads a Form.

My version of office is 2003,

It works well, but with a few problems. if you assume that we have 4 cases
it works at two of them. here is the 4 cases:

1.if you double click on a .doc file to open it, everything goes well (my
button works).

2.if you open a new instance of Word, and use the document as a new document
everything goes well here too.

3. if you have already opened a new instance of word and go to file -> open
(as usual when you open an exsiting .doc file) it does not work, my button
does not opens the form.

4.if you have uploaded a .doc file to Shared Documents on a Sharepoint Site
and want to open it, my Add-in does not work.

here is a bit of my code:

[GuidAttribute("79CC5244-EADC-4F4B-ACF1-9387460BEDF4"),
ProgId("IgnitoAddIn.Connect")]
public class Connect : Object, Extensibility.IDTExtensibility2
{
//-------
my variables
//-------
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;

toolBar = AddWordToolbar(wordApp, "Ignito Toolbar");

MyButton = this.MakeANewButton(toolBar, "View Tree", 1000, new
_CommandBarButtonEvents_ClickEventHandler(MyButton_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)
{
MyButton.Enabled = false;
}

private void SetApplicationFields(object application)
{
wordApp = (Word.Application)application;
}

private CommandBar AddWordToolbar(Word.Application word, string toolbarName)
{
CommandBar toolBar = null;
try
{
object missing = System.Reflection.Missing.Value;
toolBar = (CommandBar)wordApp.CommandBars.Add(toolbarName,
MsoBarPosition.msoBarTop, missing, true);
toolBar.Visible = true;
return toolBar;
}
catch
{
return null;
}
}

private CommandBarButton MakeANewButton(CommandBar commandBar, string
caption,int faceID, _CommandBarButtonEvents_ClickEventHandler clickHandler)
{
object missing = System.Reflection.Missing.Value;
try
{
CommandBarButton newButton;
newButton =
(CommandBarButton)commandBar.Controls.Add(MsoControlType.msoControlButton,missing, missing, missing, missing);

newButton.Caption = caption;
newButton.FaceId = faceID;
newButton.Click += clickHandler;
return newButton;
}
catch
{
return null;
}
}

public void MyButton_Click(CommandBarButton SomeButton, ref bool SomeBool)
{
TaxForm = new Form1(this);
TaxForm.Show();
}
}
 
B

Bernd Schend

Hi Medes,

do you get a runtime error?

Maybe the reason is that "MyButton" got 'null' by what reasons
soever.
Where and how did you declare the MyButton variable?
Are you shure that it stays alive till you exit Word?

Hope you got some ideas
Bernd
Hi everybody,

I have created an Add-in for MS Word. It is a toolbar that contains a button
when you click on button it loads a Form.

My version of office is 2003,

It works well, but with a few problems. if you assume that we have 4 cases
it works at two of them. here is the 4 cases:

1.if you double click on a .doc file to open it, everything goes well (my
button works).

2.if you open a new instance of Word, and use the document as a new document
everything goes well here too.

3. if you have already opened a new instance of word and go to file -> open
(as usual when you open an exsiting .doc file) it does not work, my button
does not opens the form.

4.if you have uploaded a .doc file to Shared Documents on a Sharepoint Site
and want to open it, my Add-in does not work.

here is a bit of my code:

[GuidAttribute("79CC5244-EADC-4F4B-ACF1-9387460BEDF4"),
ProgId("IgnitoAddIn.Connect")]
public class Connect : Object, Extensibility.IDTExtensibility2
{
//-------
my variables
//-------
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;

toolBar = AddWordToolbar(wordApp, "Ignito Toolbar");

MyButton = this.MakeANewButton(toolBar, "View Tree", 1000, new
_CommandBarButtonEvents_ClickEventHandler(MyButton_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)
{
MyButton.Enabled = false;
}

private void SetApplicationFields(object application)
{
wordApp = (Word.Application)application;
}

private CommandBar AddWordToolbar(Word.Application word, string toolbarName)
{
CommandBar toolBar = null;
try
{
object missing = System.Reflection.Missing.Value;
toolBar = (CommandBar)wordApp.CommandBars.Add(toolbarName,
MsoBarPosition.msoBarTop, missing, true);
toolBar.Visible = true;
return toolBar;
}
catch
{
return null;
}
}

private CommandBarButton MakeANewButton(CommandBar commandBar, string
caption,int faceID, _CommandBarButtonEvents_ClickEventHandler clickHandler)
{
object missing = System.Reflection.Missing.Value;
try
{
CommandBarButton newButton;
newButton =
(CommandBarButton)commandBar.Controls.Add(MsoControlType.msoControlButton,missing, missing, missing, missing);

newButton.Caption = caption;
newButton.FaceId = faceID;
newButton.Click += clickHandler;
return newButton;
}
catch
{
return null;
}
}

public void MyButton_Click(CommandBarButton SomeButton, ref bool SomeBool)
{
TaxForm = new Form1(this);
TaxForm.Show();
}
}
 
M

Medes

Thank you Bernd Schend,

I think the problem does not lie in my code because I created another Add-In
for Excel that was exact the same for Word. I just changed to
ExcelApplication and it works very well.
the problem is Word, I think i must make some configurations that i don't
know what thay are.
I dont get any error the button just does not answer when i click it. and
this problem occurs just when i have already opened an instance of word and i
click on File -> open to open new .doc file, when that new .doc file is
opened the button still is there but it does not answers.
I can send you my code if you want to, thank again
Bernd Schend said:
Hi Medes,

do you get a runtime error?

Maybe the reason is that "MyButton" got 'null' by what reasons
soever.
Where and how did you declare the MyButton variable?
Are you shure that it stays alive till you exit Word?

Hope you got some ideas
Bernd
Hi everybody,

I have created an Add-in for MS Word. It is a toolbar that contains a button
when you click on button it loads a Form.

My version of office is 2003,

It works well, but with a few problems. if you assume that we have 4 cases
it works at two of them. here is the 4 cases:

1.if you double click on a .doc file to open it, everything goes well (my
button works).

2.if you open a new instance of Word, and use the document as a new document
everything goes well here too.

3. if you have already opened a new instance of word and go to file -> open
(as usual when you open an exsiting .doc file) it does not work, my button
does not opens the form.

4.if you have uploaded a .doc file to Shared Documents on a Sharepoint Site
and want to open it, my Add-in does not work.

here is a bit of my code:

[GuidAttribute("79CC5244-EADC-4F4B-ACF1-9387460BEDF4"),
ProgId("IgnitoAddIn.Connect")]
public class Connect : Object, Extensibility.IDTExtensibility2
{
//-------
my variables
//-------
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;

toolBar = AddWordToolbar(wordApp, "Ignito Toolbar");

MyButton = this.MakeANewButton(toolBar, "View Tree", 1000, new
_CommandBarButtonEvents_ClickEventHandler(MyButton_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)
{
MyButton.Enabled = false;
}

private void SetApplicationFields(object application)
{
wordApp = (Word.Application)application;
}

private CommandBar AddWordToolbar(Word.Application word, string toolbarName)
{
CommandBar toolBar = null;
try
{
object missing = System.Reflection.Missing.Value;
toolBar = (CommandBar)wordApp.CommandBars.Add(toolbarName,
MsoBarPosition.msoBarTop, missing, true);
toolBar.Visible = true;
return toolBar;
}
catch
{
return null;
}
}

private CommandBarButton MakeANewButton(CommandBar commandBar, string
caption,int faceID, _CommandBarButtonEvents_ClickEventHandler clickHandler)
{
object missing = System.Reflection.Missing.Value;
try
{
CommandBarButton newButton;
newButton =
(CommandBarButton)commandBar.Controls.Add(MsoControlType.msoControlButton,missing, missing, missing, missing);

newButton.Caption = caption;
newButton.FaceId = faceID;
newButton.Click += clickHandler;
return newButton;
}
catch
{
return null;
}
}

public void MyButton_Click(CommandBarButton SomeButton, ref bool SomeBool)
{
TaxForm = new Form1(this);
TaxForm.Show();
}
}
 
B

Bernd Schend

Hi Medes,

quite a long time ago, I had a similar problem:
I added a menu to Word's commandbar, but sometimes the
buttons of the menu didn't react to a click anymore.

I implemented a workaround by refreshing the button variables
(in your case MyButton only) upon activating a document.
I used the DocumentChange event.

It wasn't really the solution I liked, but it worked.

Regards
Bernd
 
M

Medes

Thanks again,

My Toolbar does not appears when i open a .doc file ( that has been uploaded
to Shared Documents on Sharepoint) when i go to toolbar customize i can see
my toolbar but it is not checked when i check it, it appears. why?
 
B

Bernd Schend

Hi Medes,

please check it the OnConnection-event is fired if you
open a shared doc.

Another idea:
If not instructed otherwise, Word stores user defined toolbars etc.
in the CustomizationContext = Normal.dot.
Just create your own Dot-file, say, ToolContainer.dot and tell
Word to use this file as a CustomizationContext.
Here's some VB code:

' Define customization context (for storing menus etc.)
privateMyGlobalTemplate As Word.Template
Dim FName As String = "...\ToolContainer.dot"
If IO.File.Exists(FName) Then
With WordApplication
.AddIns.Add(FileName:=FName, Install:=True)
MyGlobalTemplate = .Templates(FName)
MyGlobalTemplate.Saved = True
End With
WordApplication.CustomizationContext = MyGlobalTemplate
End If

Good luck
Bernd
 

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