C# .NET - how to fire new document event for MS-Word in c#.net?

Asked By Ashwin Upadhyay on 19-Feb-10 06:34 AM
i created one shared Add-ins in visual studio 2005
all events working fine but new document event is not working i don't knw why
my code for new document is

((Word.ApplicationEvents4_Event)wb).NewDocument += new Word.ApplicationEvents4_NewDocumentEventHandler(Application_NewDocument);
and
void Application_NewDocument(Word.Document Doc)
        {
            MessageBox.Show("New: " + wb.ActiveDocument.Name);
            openingTime = DateTime.Now.ToLocalTime();
        }
Adil Saud replied to Ashwin Upadhyay on 19-Feb-10 07:28 AM
Hi,

Please check the Copy to Output Directory of your AddIn it should be set to :  "Copy Always"
Also try to test the things in the debug mode by putting a breeak point on strtup and New document event.

I guess this should solve your problem Or you will be able to find the cause  in debug mode, in my case New event is working perfect.

Thanx,
Adil...:))
Ashwin Upadhyay replied to Adil Saud on 19-Feb-10 08:00 AM
actually i m using office 2007 and shared add-in created in VS-2005
the code provided by you is working fine. but when if i opend MS-word for to create new document then this event is not fire but document before closing event is fired for this document.

and the value stored in database for thi is

opening_time                 closing_time                     time_spant        name_of_document
0001-01-01 00:00:00       2010-02-19 18:00:54          18:00:54            Document1
 

Adil Saud replied to Ashwin Upadhyay on 23-Feb-10 03:17 AM
Hi Ashwin,

You tried by putting a break point on the AddIn Startup event where you might have added the event handler line of code for New Document open.

Also put a break point in your New document event and try it in the debug mode.

I guess some issue is with your declaration of event handler, pls check NewDocument event handler is in the AddIn startup event, so that whenever you AddIn is started this event is fired.

When you run your shared AddIn in the debug mode it should open the New document and the New document event should fire, please cross check.

Also when you created the shared ADD in, it loads the Five step Wizard:
  1. Step 1 is programming language selection.
  1. Step 2 is Application Selection.
  2. Step 3 is Provide Name and description of your Add-In
  3. Step 4 is AddIn Options (This is the mos important point please cross check that both the options are selected).
This Will surely solve your problem.

Let me know if you need any further information on the same.

Thanx,
Adil...:))






Ashwin Upadhyay replied to Adil Saud on 25-Feb-10 04:43 AM
sorry sir for distrubence
but how i debug using break point becaz in visual studio, there is no facility to debug add-in project using break point
i just build the solution and check it by opening MS-word file. In shared-add-in code we gave some messege box like opening document and closing document so by this messege box i know the add-in is working or not. But in case of new document event there is no messege box is shown like new document. i tried to check using break point but break poitn is not working because i can not run add-in in visual studio i build it only not run.
Adil Saud replied to Ashwin Upadhyay on 26-Feb-10 01:38 AM
Hi Ashwin,

Visual studio 2008 has the feature to debug the ADDIns also (unfortunately VS2005 does not have it :( ), my friend if you want to debug or find the exact cause of not raising the New document event you have to debug it.

Also shared AddIn has this problem of (event mismatch - specifically New  Event), one more try may be there are three event handlers(2,3 and 4) for documents in AddIns, you can give it a try using the old handlers(2,3) they might work with VS2005.

Three more event handlers are :

  1. ((Word.ApplicationEvents3_Event)wb).NewDocument += new Word.ApplicationEvents3_NewDocumentEventHandler(Application_NewDocument);
  2. ((Word.ApplicationEvents2_Event)wb).NewDocument += new Word.ApplicationEvents2_NewDocumentEventHandler(Application_NewDocument

This is hit and try and I guess this will work for sure, another perfect solution is using VS2008 you can build MS Word AddIn, I tested this and for the new documents also it is working fine.

Let me know if you need any further information on the same.

Thanx,
Adil...:))
Ashwin Upadhyay replied to Adil Saud on 26-Feb-10 06:35 AM

Thank You sir for replying
sir i also tested both the new document event(2&3) also but its not working.
sir i just found some resion because of this new document event is not fired which is:
add-in is load after office startup completion and when add-in is load how the event is specified the document is opend is new or not.
i just think may be it is wrong
but if m i rite then why open document event is fired
so i think i m wrong.

i just give you some parts my code :

public

void OnStartupComplete(ref System.Array custom)
{

wb = (Word.

Application)System.Runtime.InteropServices.Marshal.GetActiveObject("Word.Application");


if (wb.Documents.Count > 0)

{


object docIndex = 1;
 MessageBox.Show(wb.Documents.Count.ToString() + "\r" + wb.Documents.get_Item(ref docIndex).Name);

}


wb.DocumentOpen += new Microsoft.Office.Interop.Word.ApplicationEvents4_DocumentOpenEventHandler(Application_DocumentOpen);




((Word.

ApplicationEvents4_Event)wb).NewDocument += new Word.ApplicationEvents4_NewDocumentEventHandler(Application_NewDocument);

wb.DocumentBeforeClose +=

new Word.ApplicationEvents4_DocumentBeforeCloseEventHandler(Application_BeforeClose);
}

void Application_NewDocument(Word.Document Doc)
{
MessageBox.Show("New: " + wb.ActiveDocument.Name);
openingTime =
DateTime.Now.ToLocalTime();
}

void Application_DocumentOpen(Word.Document Doc)
{
MessageBox.Show("Opened: " + wb.ActiveDocument.Name);
openingTime =
DateTime.Now.ToLocalTime();
}
void Application_BeforeClose(Word.Document Doc, ref bool cancel){
MessageBox.Show("Closing Doc: " + wb.ActiveDocument.Name);closingTime = DateTime.Now.ToLocalTime();timeSpent = closingTime.TimeOfDay - openingTime.TimeOfDay;MessageBox.Show("Time spent by user on doc " + wb.ActiveDocument.Name + " is : " + timeSpent);name = wb.ActiveDocument.Name;

}

Adil Saud replied to Ashwin Upadhyay on 28-Feb-10 09:48 AM

Hi Ashwin,

You are absolutely right,the issue is,you are registering the event(defining event handler)on the event
void OnStartupComplete(ref System.Array custom)

That means on startup complete of AddIn, also in one of my previous posts I mentioned about the wizard that is fired when you first Add the AddIn project type in VS 2005,in that wizard on one of the steps there are two check boxes,please make sure both of them are selected(they areforloading the addin when the MS word starts.

Rest you know, try tomakeit in VS2008,I guarantee that it will work in VS2008.

Let me know if you need any further information on the same.

Thanx,
Adil...:))
Ashwin Upadhyay replied to Adil Saud on 02-Mar-10 12:00 AM
yaa sir i also checked both the check boxes.
i want to find why new document event is not fired through the shared add-in created in visual studio2005.
if i get the satisfactory reasion then i move onto the visual studio2008 but still why this event is not working i just find that.

Thanks sir
i also convey you the reasion if i'll get.
Adil Saud replied to Ashwin Upadhyay on 03-Mar-10 02:06 AM
Hi Ashwin,

In VS 2005 we have only StartupComplete event and that event is fired when the AddIn is completely loaded, and that occurs after the New document is loaded, when you open the instance of MS word for the first time using shortcut or from windows program menu.

When you double click on a document in windows explorer at that time AddIn is first loaded then MS Word opens the document, so OpenDocument event is fired.

Suppose a document is already open in your MS word and you want to open a new document using the file menu, at that time you will find that NewDocument event is fired, since the AddIn is already loaded.

In VS2008 we have InternalStartup method where by default Startup (Not StartupComplete), so in VS2008 AddIn is loaded before the New Document is created in the MS Word instance.

I guess now you have good reasons to try your objective in VS2008.

Let me know if you need any further information on the same.

Thanx,
Adil...:))
Ashwin Upadhyay replied to Adil Saud on 06-Mar-10 12:38 AM
yaa sir now i also found that answer
so now i moved on visual studio 2008 for add-in
thanx alot sir
Ashwin Upadhyay replied to Adil Saud on 05-Apr-10 03:06 AM
i have another qus related to add-in installation..........
qus is:
i want to combine this addin to my project that means if i install my product then addin is also install with this........
and if i uninstall my product add-in is also ininstall...........
and i also have another qus other than addin
i want to give language choosen option at installation time that means if user want to select hindi then our product install in hindi language and if select english than install in english...
how this facility give in setup creation plzzz discribe in detail
The addin which i created is not working on MSoffice2003. - Ashwin Upadhyay replied to Adil Saud on 10-Jun-10 01:00 AM
hello sir
the shared addin which i created is not working for all the ms office versions. can u plz have any solution that the addin which is created by us is working for all the msoffice version.
thanx