Extensibility project in c# .NET for Word2003

F

Frenchy

Hello,


I'm developing an add-in for Word2003 and I have some troubles. I’m using VS
..NET 2003 ->extensibility project.

The add-in works fine with the first document in Word I create/open but
after It fails, the add-in doesn’t work anymore.
I’m looking for an add-in source code, to see why I always have the same
problem, where is the mistake…

In fact all the events (OnConnection, OnDisconnection, etc) only fires with
the first document created/opened.

All the source codes I found, didn’t worked…So I’m looking for a complete
example source code (in c# .NET)

Do you know where I could find It? Or why does my add-in only works with the
first “instance†of Word.

Thanks a lot,

Frenchy

PS: Sorry for my bad English :p
 
F

Fredrik Wahlgren

Frenchy said:
Hello,


I'm developing an add-in for Word2003 and I have some troubles. Iâ?Tm using VS
.NET 2003 ->extensibility project.

The add-in works fine with the first document in Word I create/open but
after It fails, the add-in doesnâ?Tt work anymore.
Iâ?Tm looking for an add-in source code, to see why I always have the same
problem, where is the mistakeâ?¦

In fact all the events (OnConnection, OnDisconnection, etc) only fires with
the first document created/opened.

All the source codes I found, didnâ?Tt workedâ?¦So Iâ?Tm looking for a complete
example source code (in c# .NET)

Do you know where I could find It? Or why does my add-in only works with the
first â?oinstanceâ? of Word.

Thanks a lot,

Frenchy

PS: Sorry for my bad English :p

The reference to Word is passed to your add-in. I think you use something
similar to VB6's GetObject
/Fredrik
 
F

Frenchy

I have an Application which is parse into an ApplicationObject and then in
Word.Application

I don't thnk it's the same in VB6
 
F

Frenchy

Here's how I work.

I don't directly use the Application object:

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

applicationObject = application;
addInInstance = addInInst;
appWord = (Word.Application)applicationObject;


It's the only way I found to get all the object, methods, .. of the word
object...
 
F

Fredrik Wahlgren

Frenchy said:
Here's how I work.

I don't directly use the Application object:

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

applicationObject = application;
addInInstance = addInInst;
appWord = (Word.Application)applicationObject;


It's the only way I found to get all the object, methods, .. of the word
object...

Why do you need the applicationObject variable? Why not just
appWord = (Word.Application) application

Do you get some kind of error message? I don't understand what you mean when
you say that "I don't directly use the Application object". Do you refer to
the variable that I asked about above?

/Fredrik
 
F

Frenchy

The applicationobject is used to catch all the events (onconnection,...) if I
only use the Word.Application, all my events don't work.

Someone tell me to use "inspectors", I don't know what It is, I'll search
 
F

Frenchy

I'm not sure...am I in the good newsgroup?

It's been a month since I started my add-in, and I'm still on the same
place...

Am I the only one developing an add-in???

The first thing I’ll do when my code will be working is to publish It…I
don’t understand why nobody can help me :-( :-( :-( :-( :-( :-(..why nobody
publish their code…
 
B

Brian

I dont think he understood you french. I am having the same problem. I added
a CommandBarPopup to the main "Menu Bar" with two CommandBarButtons which
allow users in our office to open documents stored in an Access DB document
repository. Users can click the two buttons for eternity and, as long as they
dont actually open a document from the forms that are displayed, everything
will be fine. However, once you select the document and that is opened, my
drop-down menu no longer works. Here it the code that is called 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);
// }
}
}

As you can see, I tried re-wiring the event handlers thinking that might be
a problem however, that did nothing.

Any help on this exceptionally frustrating problem would be greatly
appreciated.

-Brian
 
A

Alex

Hello Brian,

Brian said:
I dont think he understood you french. I am having the same problem. I added
a CommandBarPopup to the main "Menu Bar" with two CommandBarButtons which
allow users in our office to open documents stored in an Access DB document
repository. Users can click the two buttons for eternity and, as long as they
dont actually open a document from the forms that are displayed, everything
will be fine. However, once you select the document and that is opened, my
drop-down menu no longer works.

What is the applicationObject.CustomizationContext property set to?

Do you make sure that your menus and buttons are not GCd?

Best wishes,
Alex.
 
B

Brian

I'm not sure how to tell what the value of CustomizationContext is :-(. I
step through in the debugger and, I only get System.__ComObject (I'm new to
the .NET/Office world).

.... I wrote this code to get a better idea of what object CC is set to

if (wordApp.CustomizationContext is Word.Document)
{
Word.Document wd = (Word.Document)wordApp.CustomizationContext;
MessageBox.Show("DD: " + wd.Name);
}
else if (wordApp.CustomizationContext is Word.Template)
{
Word.Template wt = (Word.Template)wordApp.CustomizationContext;
MessageBox.Show("WT: " + wt.Name);
}

I'm slightly embarassed at posting that as I'm not one for printfs as a
means of debugging but... It came up as a template, "Normal.dot" in the
debugger, which is what I would expect it to be. Should that be manually set
by my addin for each document that is opened? I would imagine word would
handle it on it's own, right?

Concerning the scope of the CommandBarButtons, they are all class member
variables so, I cant imagine them being GC'd but... I suppose how would I
tell? When I initiate the event (with a manual click) no exception is thrown
which makes me think the buttons havent been GC'd as that would throw a
NullPointerException if they had been.

I guess that is another problem... Once the error occurs, I dont have any
way of calling code that runs in my Addin to be able to view the instance
objects in the Debugger but... I'm pretty new to VS so... is there a way to
attach to a loaded class and inspect it? (like JProbe)

I read something earlier about the Tag however, it wasnt completely clear to
me. Do I need to just give each button a Unique ID on "StartupComplete" or,
do I need to listen to the document open events and create additional
new/unique tag ids for those as well. I suppose it is worth a shot to try
both avenues.

I whish there was better documentation on all of this stuff. I read the book
".NET Development for Microsoft Office" by Andrew Whitechapel. That was good
for getting started but, it very much stays away from the many pitfalls I am
finding exist with Office development and tends to be highly glossed.
However, I would say on the whole it is a good book and would recommend it. I
dont know where I would be if I hadnt read it before I started. :p

Thank you for the comments Alex.

Brian
 

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