how to create new document event for MS-Word in shared-add-in created in visual studio 2005?

Add-in Express™ Support Service
That's what is more important than anything else

how to create new document event for MS-Word in shared-add-in created in visual studio 2005?
 
ashwin upadhyay




Posts: 5
Joined: 2010-02-08
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();
}
Posted 19 Feb, 2010 05:40:49 Top
Andrei Smolin


Add-in Express team


Posts: 18791
Joined: 2006-05-11
Hi Ashwin,

Can you post some code here?


Andrei Smolin
Add-in Express Team Leader
Posted 22 Feb, 2010 07:03:36 Top
ashwin upadhyay




Posts: 5
Joined: 2010-02-08
The code is like:


namespace MyNewAddin
{
#region Read me for Add-in installation and setup information.
// When run, the Add-in wizard prepared the registry for the Add-in.
// At a later time, if the Add-in becomes unavailable for reasons such as:
// 1) You moved this project to a computer other than which is was originally created on.
// 2) You chose 'Yes' when presented with a message asking if you wish to remove the Add-in.
// 3) Registry corruption.
// you will need to re-register the Add-in by building the MyNewAddinSetup project,
// right click the project in the Solution Explorer, then choose install.
#endregion

/// <summary>
/// The object for implementing an Add-in.
/// </summary>
/// <seealso class='IDTExtensibility2' />
[GuidAttribute("C11CEFEB-BF8C-4DE0-B32C-102F51C9FB5C"), ProgId("MyNewAddin.Connect")]
public class Connect : Object, Extensibility.IDTExtensibility2
{
Word.Application wb;
DateTime openingTime, closingTime;
TimeSpan timeSpent;
MySqlConnection con = new MySqlConnection("server=localhost;user Id=root;Password=console123;database=record");
MySqlCommand cmd;
ulong index;
string name;

/// <summary>
/// Implements the constructor for the Add-in object.
/// Place your initialization code within this method.
/// </summary>
///


public void ins ert(DateTime openingTime, DateTime closingTime, TimeSpan timeSpent, string name,ulong index)
{
con.Open();
cmd = new MySqlCommand("insert into time(start_time,end_time,spend_time,name,unique_id) val ues(@dt1,@dt2,@dt3,@name,@index)", con);
cmd.Parameters.AddWithValue("@dt1", openingTime);
cmd.Parameters.AddWithValue("@dt2", closingTime);
cmd.Parameters.AddWithValue("@dt3", timeSpent);
cmd.Parameters.AddWithValue("@name", name);
cmd.Parameters.AddWithValue("@index", index);
cmd.ExecuteNonQuery();
con.Close();
}
public Connect()
{
}

/// <summary>
/// Implements the OnConnection method of the IDTExtensibility2 interface.
/// Receives notification that the Add-in is being loaded.
/// </summary>
/// <param term='application'>
/// Root object of the host application.
/// </param>
/// <param term='connectMode'>
/// Describes how the Add-in is being loaded.
/// </param>
/// <param term='addInInst'>
/// Object representing this Add-in.
/// </param>
/// <seealso class='IDTExtensibility2' />
public void OnConnection(object application, Extensibility.ext_ConnectMode connectMode, object addInInst, ref System.Array custom)
{
applicationObject = application;
addInInstance = addInInst;

}

/// <summary>
/// Implements the OnDisconnection method of the IDTExtensibility2 interface.
/// Receives notification that the Add-in is being unloaded.
/// </summary>
/// <param term='disconnectMode'>
/// Describes how the Add-in is being unloaded.
/// </param>
/// <param term='custom'>
/// Array of parameters that are host application specific.
/// </param>
/// <seealso class='IDTExtensibility2' />
public void OnDisconnection(Extensibility.ext_DisconnectMode disconnectMode, ref System.Array custom)
{

}

/// <summary>
/// Implements the OnAddInsUpdate method of the IDTExtensibility2 interface.
/// Receives notification that the collection of Add-ins has changed.
/// </summary>
/// <param term='custom'>
/// Array of parameters that are host application specific.
/// </param>
/// <seealso class='IDTExtensibility2' />
public void OnAddInsUpdate(ref System.Array custom)
{
}

/// <summary>
/// Implements the OnStartupComplete method of the IDTExtensibility2 interface.
/// Receives notification that the host application has completed loading.
/// </summary>
/// <param term='custom'>
/// Array of parameters that are host application specific.
/// </param>
/// <seealso class='IDTExtensibility2' />
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.Startup += new Microsoft.Office.Interop.Word.ApplicationEvents4_StartupEventHandler(Application_NewDocument);
//Word.applDocumentChange += new Microsoft.Office.Interop.Word.ApplicationEvents4_DocumentChangeEventHandler(Application_NewDocument);
((Word.ApplicationEvents4_Event)wb).NewDocument += new Word.ApplicationEvents4_NewDocumentEventHandler(Application_NewDocument);
//wb.Startup += new Microsoft.Office.Interop.Word.ApplicationEvents4_StartupEventHandler(Application_NewDocument);
wb.DocumentOpen += new Microsoft.Office.Interop.Word.ApplicationEvents4_DocumentOpenEventHandler(Application_DocumentOpen);

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;
FileInfo fi = new FileInfo(name);
index = bo.ApproachA(fi);


}



/// <summary>
/// Implements the OnBeginShutdown method of the IDTExtensibility2 interface.
/// Receives notification that the host application is being unloaded.
/// </summary>
/// <param term='custom'>
/// Array of parameters that are host application specific.
/// </param>
/// <seealso class='IDTExtensibility2' />
public void OnBeginShutdown(ref System.Array custom)
{
insert(openingTime, closingTime, timeSpent, name, index);
}

private object applicationObject;
private object addInInstance;
}
}
Posted 22 Feb, 2010 07:08:03 Top
Andrei Smolin


Add-in Express team


Posts: 18791
Joined: 2006-05-11
Hi Ashwin,

I'm sorry, we can provide support for Add-in Express customers omly.


Andrei Smolin
Add-in Express Team Leader
Posted 22 Feb, 2010 07:41:27 Top