private Word.DocumentEvents2_OpenEventHandler openEvent; private Word.DocumentEvents2_CloseEventHandler closeEvent; private Word.ApplicationEvents4_DocumentBeforeSaveEventHandler beforesaveEvent;
/// <summary> /// Called before the user see any dialog in the save process /// </summary> protected void ThisDocument_BeforeSave(Word.Document document, ref bool b, ref bool q) { DialogResult result = MessageBox.Show("Do you want to save as XML also?","Tech Head Brothers - Article", MessageBoxButtons.YesNo); if ( result == DialogResult.Yes) { SaveTransformedXml(document); ... } else { result = MessageBox.Show("Do you want to pack all the files in a zip to publish ?","Tech Head Brothers - Article", MessageBoxButtons.YesNo); if ( result == DialogResult.Yes) { SaveTransformedXml(document); ... } } }
/// <summary> /// Required procedure. Do not modify. /// </summary> /// <param name="application">Application object.</param> /// <param name="document">Document object.</param> public void _Startup(object application, object document) { this.thisApplication = application as Word.Application; this.thisDocument = document as Word.Document;
beforesaveEvent = new Word.ApplicationEvents4_DocumentBeforeSaveEventHandler(ThisDocument_BeforeSave); thisApplication.DocumentBeforeSave += beforesaveEvent;
thisApplication.TaskPanes[Word.WdTaskPanes.wdTaskPaneXMLStructure].Visible = true;
void SaveTransformedXml(Word.Document document) { string xmlContents = document.XMLNodes[1].get_XML(true); string path = document.Path + @"\" + document.Name.Replace(".doc", ".xml"); System.IO.StreamWriter writer = new System.IO.StreamWriter(path, false, System.Text.Encoding.UTF8); writer.Write(xmlContents); writer.Close(); }